Có lẽ tại mình diễn đạt không rõ ý, nếu mình chọn mã bên combobox1 là D và chọn bên combobox2 là V2 thì sẽ gán giá trị của textbox vào ô trống bất kỳ trong vùng D7:F7.Bạn cho cái ví dụ nhập cụ thể xem nào.
Private Sub CommandButton1_Click()
Dim Rng As Range, sRng As Range
Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer
With Sheet1
Rws = .[A3].CurrentRegion.Rows.Count
Set Rng = .[A3].Resize(Rws)
Set sRng = Rng.Find(Me!ComboBox1.Text, , xlFormulas, xlWhole)
If Not sRng Is Nothing Then
Col = CByte(Right(Me!ComboBox2.Text, 1))
Cot = Choose(Col, 2, 4, 7, 12)
Den = Choose(Col, 1, 2, 4)
For W = Cot To Cot + Den
If .Cells(sRng.Row, W).Value = 0 Then
.Cells(sRng.Row, W).Value = Me!TextBox1.Text
Exit For
End If
Next W
End If
End With
End Sub
Thật tuyệt. Chân thành cám ơn bác.Macro bạn cần có thể là vầy:
PHP:Private Sub CommandButton1_Click() Dim Rng As Range, sRng As Range Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer With Sheet1 Rws = .[A3].CurrentRegion.Rows.Count Set Rng = .[A3].Resize(Rws) Set sRng = Rng.Find(Me!ComboBox1.Text, , xlFormulas, xlWhole) If Not sRng Is Nothing Then Col = CByte(Right(Me!ComboBox2.Text, 1)) Cot = Choose(Col, 2, 4, 7, 12) Den = Choose(Col, 1, 2, 4) For W = Cot To Cot + Den If .Cells(sRng.Row, W).Value = 0 Then .Cells(sRng.Row, W).Value = Me!TextBox1.Text Exit For End If Next W End If End With End Sub
Bác có thể giải thích dùm E đoạn này được không. E không hiểuMacro bạn cần có thể là vầy:
PHP:Private Sub CommandButton1_Click() Dim Rng As Range, sRng As Range Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer With Sheet1 Rws = .[A3].CurrentRegion.Rows.Count Set Rng = .[A3].Resize(Rws) Set sRng = Rng.Find(Me!ComboBox1.Text, , xlFormulas, xlWhole) If Not sRng Is Nothing Then Col = CByte(Right(Me!ComboBox2.Text, 1)) Cot = Choose(Col, 2, 4, 7, 12) Den = Choose(Col, 1, 2, 4) For W = Cot To Cot + Den If .Cells(sRng.Row, W).Value = 0 Then .Cells(sRng.Row, W).Value = Me!TextBox1.Text Exit For End If Next W End If End With End Sub
Cot = Choose(Col, 2, 4, 7, 12)
Den = Choose(Col, 1, 2, 4)
Hàm Choose() trong VBA cũng tương tự hàm CHOOSE() trong ExcelBác có thể giải thích dùm E đoạn này được không.
CODE]Cot = Choose(Col, 2, 4, 7, 12)
Den = Choose(Col, 1, 2, 4)[/CODE]
Function GetChoice(Ind As Integer)
GetChoice = Choose(Ind, "Speedy", "United", "Federal")
End Function
Data | Data |
1st | Nails |
2nd | Screws |
3rd | Nuts |
Finished | Bolts |
Formula | Description (Result) |
=CHOOSE(2,A2,A3,A4,A5) | Value from the second argument A3 (2nd) |
=CHOOSE(4,B2,B3,B4,B5) | Value from the fourth argument B5 (Bolts) |
Data | |
23 | |
45 | |
12 | |
10 | |
Formula | Description (Result) |
=SUM(A2:CHOOSE(2,A3,A4,A5)) | Sums the range A2:A4 (80) |
Mình chép fần 'Help' trong chúng í mà!Đọc xong bài viết là giỏi tiếng Anh luôn
Mình chép fần 'Help' trong chúng í mà!
Mình rành tiếng 'Em' hơn, còn tiếng Anh mình chưa có bằng cấp nào sất!
Private Sub CommandButton1_Click()
Dim Rng As Range, sRng As Range
Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer
With Sheet1
Rws = .[b3].CurrentRegion.Rows.Count
Set Rng = .[b3].Resize(Rws)
Set sRng = Rng.Find(Me!ComboBox1.Text, , xlValues, xlWhole) '** '
If Not sRng Is Nothing Then
Col = CByte(Right(Me!ComboBox2.Text, 1))
Cot = Choose(Col, 3, 5, 8, 9, 13, 35)
Den = Choose(Col, 1, 2, 0, 3, 0, 6) '* '
For W = Cot To Cot + Den
If .Cells(sRng.Row, W).Value = 0 Then
.Cells(sRng.Row, W).Value = Me!TextBox1.Text
Exit For
End If
Next W
End If
End With
End Sub
Chúc Bác ăn trưa ngon miệng!PHP:Private Sub CommandButton1_Click() Dim Rng As Range, sRng As Range Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer With Sheet1 Rws = .[b3].CurrentRegion.Rows.Count Set Rng = .[b3].Resize(Rws) Set sRng = Rng.Find(Me!ComboBox1.Text, , xlValues, xlWhole) '** ' If Not sRng Is Nothing Then Col = CByte(Right(Me!ComboBox2.Text, 1)) Cot = Choose(Col, 3, 5, 8, 9, 13, 35) Den = Choose(Col, 1, 2, 0, 3, 0, 6) '* ' For W = Cot To Cot + Den If .Cells(sRng.Row, W).Value = 0 Then .Cells(sRng.Row, W).Value = Me!TextBox1.Text Exit For End If Next W End If End With End Sub
& chúc vui!
DIỄN ĐÀN GIẢI PHÁP EXCEL Group 1
DIỄN ĐÀN GIẢI PHÁP EXCEL Group 2