【问题标题】:Type Mismatch (Error 13) - Looping Through Range And Comparing Cell Value To Textbox Value类型不匹配(错误 13) - 循环遍历范围并将单元格值与文本框值进行比较
【发布时间】:2018-02-15 16:56:29
【问题描述】:

我对 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

【问题讨论】:

    标签: excel excel-2013 vba


    【解决方案1】:

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

    【讨论】:

    • @micmc 如果有帮助,您能否将我的回复标记为答案?谢谢:)
    【解决方案2】:

    在分配 rng 时将其父工作表提供给它:

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

    否则它会得到ActiveSheet,如果ActiveSheet不是ws,你会在循环中得到一个错误。


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多