0

我为命令按钮编写此代码以将用户窗体中的数据插入到 Excel 工作表中。问题出在我的循环上。我想对循环做的是当 的值cells(i,2)等于 的值时strProduct(comboBox),我想在单元格(i,2)处插入新行,然后在新创建的行处插入新数据。如果 cells(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 行开始。

4

1 回答 1

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
于 2013-02-22T10:27:50.233 回答