我为commandbutton编写这段代码,将userform中的数据插入到excel工作表中。问题出在我的循环上。我想要做的循环是当cells(i,2)
的值等于strProduct(comboBox)
的值时,我想在单元格(i,2)处插入新行,然后在新的位置插入新数据创建了一行。如果cell(i,2)的值不等于strProduct的值,则继续下一个i值。这是代码
Private Sub CmdEnter_Click()
Dim strProduct As String
Dim intCountData As Long
strProduct = ComboBox1.Value
intCountData = Worksheets("Input").Range("B31").Rows.Count
For i = 32 To intCountData + 31
If Worksheets("Input").Cells(i, 2) <> strProduct Then
GoTo finish1:
Else
Rows(i).Select
Selection.Insert Shift:=xlDown
ActiveCell.Insert.Cells(i, 2) = ComboBox1
Exit Sub
End If
finish1:
Next
End Sub
我开始i = 32的原因是因为行从第32行开始。
答案 0 :(得分:0)
Private Sub CommandButton1_Click()
Dim strProduct As String
Dim intCountData As Long
strProduct = ComboBox1.Value
lastrow = Range("a1000").End(xlUp).Row
For i = 31 To lastrow
If Worksheets(1).Cells(i, 2) = strProduct Then
Rows(i).Select
Cells(i, 2) = ComboBox1.Value
GoTo finish1
Exit Sub
Else
GoTo finish1
End If
finish1:
Next
End Sub