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

时间:2018-02-15 16:56:29

标签: excel-vba excel-2013 vba excel

我对VBA相当新,我无法弄清楚我做错了什么或错误。我试图循环一定的范围。如果来自userform的文本框值等于该范围内的单元格,则它会插入一个新行并添加该行中userform的输入,然后结束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

2 个答案:

答案 0 :(得分:0)

您应该使用工作表对范围进行限定,以便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

答案 1 :(得分:0)

分配时,将rng的父工作表提供给:

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

否则它将获得ActiveSheet,如果ActiveSheet不是ws,则会出现循环错误。

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