我需要一些关于如何使用Excel VBA代码解决以下问题的帮助。我有2张桌子。表1内容行数据和表2内容变量行。基本上我需要在这两个表之间执行字符串比较,然后一旦匹配,我需要从表2中获取匹配变量,并在相同行号的匹配字符串下写入表1。有时候表2中可能会有更多的匹配。所以我需要提示一个框来向用户显示匹配变量列表,并要求他们选择使用哪一个作为替换。如果它包含1个变量匹配,我可以直接替换
我能够在这两个表之间进行比较并提取信息 (行号,表1中的数据和表2中的变量)分为3个不同的数组。现在的问题是如何使用这3个不同的数组来操作将变量写入excel数据表。
下面显示了3个数组,它们是match1,用于存储数据表1的行号.matchstr1显示表2中的变量,tmpstr是表1中的数据。
match1 [1] = 2
matchstr1 [1] = modelSelectionmadeInAmerica
tmpstr1 [1] = ModelSelectionMadeInAmerica
match1 [2] = 3
matchstr1 [2] = configurationType_A
tmpstr1 [2] = ConfigurationType
match1 [3] = 4
matchstr1 [3] = ctrStationpowerSupply_A
tmpstr1 [3] = PowerSupply
match1 [4] = 4
matchstr1 [4] = confPowerSupply_B
tmpstr1 [4] = PowerSupply
match1 [5] = 4
matchstr1 [5] = KitPowerSupplyRequired_C
tmpstr1 [5] = PowerSupply
match1 [6] = 5
matchstr1 [6] = CHSirenAndLightsKeypadPrinting_D
tmpstr1 [6] = CHSirenandLightsKeypadPrinting
match1 [7] = 6
matchstr1 [7] = DREnableDR_E
tmpstr1 [7] = DREnableDR
过去3天我一直在努力寻找这个问题的解决方案,但是它没有按预期或要求运行。
以下是代码的外观
选项比较文字
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, n As Integer, j As Integer
Dim counter1 As Integer, counter2 As Integer
Dim match1(1 To 500) As Integer
Dim matchstr1(1 To 500) As String
Dim tmpstr1(1 To 500) As String
counter1 = 1
counter2 = 0
j = 0
For i = 1 To 500
tmpstr1(i) = ""
Next i
For i = 1 To 500
matchstr1(i) = ""
Next i
For i = 1 To 500
match1(i) = 0
Next i
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
counter1 = counter1 + 1
item1 = "*" & item1 & "*"
For Each item2 In data1
If item2 = "" Then Exit For
If item2 Like item1 Then
counter2 = counter2 + 1
match1(counter2) = counter1
matchstr1(counter2) = item2.Value
tmpstr1(counter2) = item1
' Debug.Print "match1[" & counter2; "] = " & match1(counter2)
' Debug.Print "matchstr1[" & counter2; "] = " & matchstr1(counter2)
' Debug.Print "tmpstr1[" & counter2; "] = " & tmpstr1(counter2)
End If
Next item2
Next item1
' Need help here how to use the above array to replace the data in table 1 using the var from table 2.
End Sub
由于