我在2列之间进行字符串比较。
我将第一列数据与第二列数据进行比较。如果匹配,那么我需要将第2列的数据写入第1列。
当我执行此代码时,我遇到了
运行时错误' 424'需要对象
在MatchData(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
答案 0 :(得分:0)
尝试将代码更改为:
For Each item1 In attr1
If item1.value = "" Then Exit For
item1.value = "*" & item1 & "*"