类型不匹配(错误 13)- 遍历范围并将单元格值与文本框值进行比较

Type Mismatch (Error 13) - Looping Through Range And Comparing Cell Value To Textbox Value

我是 VBA 的新手,我不知道我做错了什么或错误。我试图在一定范围内循环。如果来自用户窗体的文本框值等于该范围内的单元格,它会插入一个新行并在该行中添加来自用户窗体的输入,然后结束 for 循环。如果它在范围末尾但仍然不相等,它会在最后一行数据后添加到行中。

Dim rng As Range
Set rng = ("F2:F1000")

For Each cell In rng
    If cell.Text = TextBox6.Text Then
        rng.EntireRow.Insert Shift:=xlDown
        ws.Range("A" & rng).Value = TextBox13.Text
        ws.Range("B" & rng).Value = TextBox2.Text
        ws.Range("C" & rng).Value = TextBox3.Text
        ws.Range("D" & rng).Value = TextBox4.Text
        ws.Range("E" & rng).Value = TextBox5.Text
        ws.Range("F" & rng).Value = TextBox6.Text
        ws.Range("G" & rng).Value = TextBox7.Text
        ws.Range("H" & rng).Value = TextBox8.Text
        ws.Range("I" & rng).Value = TextBox9.Text
        ws.Range("J" & rng).Value = TextBox10.Text
        ws.Range("K" & rng).Value = TextBox11.Text
        ws.Range("L" & rng).Value = TextBox12.Text

        Exit For

    ElseIf Cells(1000, "F") And cell.Text <> TextBox6.Text Then
        Dim LastRow As Long, ws As Worksheet
        Set ws = Sheets("Inventory Overview")
        LastRow = ws.Range("A" & Rows.count).End(xlUp).Row + 1 'Finds the last blank row
    '         Inserts Data
            ws.Range("A" & LastRow).Value = TextBox13.Text
            ws.Range("B" & LastRow).Value = TextBox2.Text
            ws.Range("C" & LastRow).Value = TextBox3.Text
            ws.Range("D" & LastRow).Value = TextBox4.Text
            ws.Range("E" & LastRow).Value = TextBox5.Text
            ws.Range("F" & LastRow).Value = TextBox6.Text
            ws.Range("G" & LastRow).Value = TextBox7.Text
            ws.Range("H" & LastRow).Value = TextBox8.Text
            ws.Range("I" & LastRow).Value = TextBox9.Text
            ws.Range("J" & LastRow).Value = TextBox10.Text
            ws.Range("K" & LastRow).Value = TextBox11.Text
            ws.Range("L" & LastRow).Value = TextBox12.Text

    End If

Next cell

您应该使用工作表来限定您的范围,这样 VBA 就不会假定 ActiveSheet,而且我很确定您必须将 rng.EntireRow.Insert Shift:=xlDown 更改为 cell.EntireRow.Insert Shift:=xlDown 这样它插入一行 我相信以下内容应该符合您的期望:

Private Sub CommandButton1_Click()
Dim ws As Worksheet: Set ws = Sheets("Inventory Overview")
'declare and set your worksheet, amend as required
Dim LastRow As Long
Dim bool As Boolean
Dim rng As Range
Set rng = ws.Range("F2:F1000")
bool = False
For Each Cell In rng 'loop to see if you can find Textbox Value in Column F
    If Cell.Text = TextBox6.Text Then
        Cell.EntireRow.Insert Shift:=xlDown
        ws.Range("A" & Cell.Offset(-1, 0).Row).Value = TextBox13.Text
        ws.Range("B" & Cell.Offset(-1, 0).Row).Value = TextBox2.Text
        ws.Range("C" & Cell.Offset(-1, 0).Row).Value = TextBox3.Text
        ws.Range("D" & Cell.Offset(-1, 0).Row).Value = TextBox4.Text
        ws.Range("E" & Cell.Offset(-1, 0).Row).Value = TextBox5.Text
        ws.Range("F" & Cell.Offset(-1, 0).Row).Value = TextBox6.Text
        ws.Range("G" & Cell.Offset(-1, 0).Row).Value = TextBox7.Text
        ws.Range("H" & Cell.Offset(-1, 0).Row).Value = TextBox8.Text
        ws.Range("I" & Cell.Offset(-1, 0).Row).Value = TextBox9.Text
        ws.Range("J" & Cell.Offset(-1, 0).Row).Value = TextBox10.Text
        ws.Range("K" & Cell.Offset(-1, 0).Row).Value = TextBox11.Text
        ws.Range("L" & Cell.Offset(-1, 0).Row).Value = TextBox12.Text
        bool = True 'if found change flag to True
        Exit For
    End If
Next Cell

If bool = False Then 'if not found in the previous loop then add to last row
    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row
        ws.Range("A" & LastRow).Value = TextBox13.Text
        ws.Range("B" & LastRow).Value = TextBox2.Text
        ws.Range("C" & LastRow).Value = TextBox3.Text
        ws.Range("D" & LastRow).Value = TextBox4.Text
        ws.Range("E" & LastRow).Value = TextBox5.Text
        ws.Range("F" & LastRow).Value = TextBox6.Text
        ws.Range("G" & LastRow).Value = TextBox7.Text
        ws.Range("H" & LastRow).Value = TextBox8.Text
        ws.Range("I" & LastRow).Value = TextBox9.Text
        ws.Range("J" & LastRow).Value = TextBox10.Text
        ws.Range("K" & LastRow).Value = TextBox11.Text
        ws.Range("L" & LastRow).Value = TextBox12.Text
End If
End Sub

分配给 rng 其父工作表:

Dim rng As Range
Set rng = Worksheets("OlympicGames2018").Range("F2:F1000")

否则它会得到 ActiveSheet,如果 ActiveSheet 不是 ws,你会在循环时出错。


Cells(1000, "F")也是如此。将它们分配给父工作表。