Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 运行时错误';424';MatchData上需要的对象(item1.Row)=item2.Value_Excel_Vba - Fatal编程技术网

Excel 运行时错误';424';MatchData上需要的对象(item1.Row)=item2.Value

Excel 运行时错误';424';MatchData上需要的对象(item1.Row)=item2.Value,excel,vba,Excel,Vba,我在两列之间执行字符串比较 我将第一列数据与第二列数据进行比较。如果存在匹配项,则需要将第2列中的数据写入第1列 当我执行这段代码时,我发现 需要运行时错误“424”对象 在第行匹配数据(item1.Row)=item2.Value 如果我对这行进行注释,则不会收到此错误消息 Private Sub CommandButton2_Click() Dim attr1 As Range, data1 As Range Dim item1, item2, item3, lastRow, lastRo

我在两列之间执行字符串比较

我将第一列数据与第二列数据进行比较。如果存在匹配项,则需要将第2列中的数据写入第1列

当我执行这段代码时,我发现

需要运行时错误“424”对象

在第
行匹配数据(item1.Row)=item2.Value

如果我对这行进行注释,则不会收到此错误消息

Private Sub CommandButton2_Click()

Dim attr1 As Range, data1 As Range
Dim item1, item2, item3, lastRow, lastRow2
Dim UsrInput, UsrInput2 As Variant
Dim Cnt As Integer, LineCnt As Integer
Dim MatchData(1 To 9000) As String
Dim i As Integer            

For i = 1 To 9000
    MatchData(i) = ""
Next i

UsrInput = InputBox("Enter Atribute Column")
UsrInput2 = InputBox("Enter Column Alphabet to compare")

With ActiveSheet
    lastRow = .Cells(.Rows.Count, UsrInput).End(xlUp).Row
    'MsgBox lastRow
End With

With ActiveSheet
    lastRow = .Cells(.Rows.Count, UsrInput2).End(xlUp).Row
    'MsgBox lastRow
End With

Set attr1 = Range(UsrInput & "2:" & UsrInput & lastRow)
Set data1 = Range(UsrInput2 & "2:" & UsrInput2 & lastRow)

For Each item1 In attr1
    item1.Value = Replace(item1.Value, " ", "")
Next item1

For Each item1 In attr1
    If item1 = "" Then Exit For

    item1 = "*" & item1 & "*"

    For Each item2 In data1                
        If item2 Like item1 Then
            MatchData(item1.Row) = item2.Value
            MatchData(item2.Row) = item2.Value
            Debug.Print "Match"
            'Debug.Print item2.Row
            Debug.Print item1.Row
            Debug.Print item1
            Debug.Print item2
            Debug.Print " "
        End If
    Next item2

Next item1

End Sub

尝试将代码更改为:


    For Each item1 In attr1
         If item1.value = "" Then Exit For

         item1.value = "*" & item1 & "*"

如果将其更改为
Set MatchData(item1.Row)=item2.Value
,问题是否消失?是否确实存在
item1.Row
?当出现错误框时,按
Debug
,并使用本地窗口检查
item1
,因为匹配数据被设置为字符串;您应该将item2.Value转换为字符串,这样可能有助于解决您的问题:MatchData(item2.Row)=CStr(item2.Value)Hi-jsheeran,我尝试了您的建议,但仍然显示相同的错误消息。Hi-Flephal,我尝试了您的建议,但仍然显示相同的错误消息Hi-mkinson,错误消息与代码“MatchData(item1.Row)=item2.value”对应的行有关。我相信它在item1.row上有问题,因为无法识别。是的,但我相信这是因为行“If item2 Like item1”,但是item1没有值,因为您将它指定为item1=”“,而不是item1.value=“”,item1=“/*”而不是item1.value-“/*”。我已经在我的系统上运行了代码,在做了我上面建议的更改后,它似乎工作正常。嗨,mkinson,非常感谢你的帮助。您的解决方案有助于解决此问题。感谢你在这件事上的帮助。