Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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 VBA编码问题_Excel_Vba - Fatal编程技术网

Excel VBA编码问题

Excel VBA编码问题,excel,vba,Excel,Vba,我需要一些关于如何使用Excel VBA代码解决以下问题的帮助。我有两张桌子。表1包含数据行,表2包含变量行。基本上,我需要在这两个表之间执行字符串比较,然后一旦匹配,我需要从表2中获取匹配变量,并在表1上的匹配字符串的相同行号下写入。有时,表2中可能有更多的匹配项。因此,我需要提示一个框,向用户显示匹配变量的列表,并要求他们选择哪一个作为替换。如果它包含1个变量匹配,我可以直接替换 我能够在这两个表之间进行比较并提取信息 (行号、表1中的数据和表2中的变量)放入3个不同的数组中。现在的问题是如

我需要一些关于如何使用Excel VBA代码解决以下问题的帮助。我有两张桌子。表1包含数据行,表2包含变量行。基本上,我需要在这两个表之间执行字符串比较,然后一旦匹配,我需要从表2中获取匹配变量,并在表1上的匹配字符串的相同行号下写入。有时,表2中可能有更多的匹配项。因此,我需要提示一个框,向用户显示匹配变量的列表,并要求他们选择哪一个作为替换。如果它包含1个变量匹配,我可以直接替换

我能够在这两个表之间进行比较并提取信息 (行号、表1中的数据和表2中的变量)放入3个不同的数组中。现在的问题是如何使用这3个不同的数组来处理将变量写入excel数据表的操作

下面显示了3个匹配1的数组,它们存储数据表1的行号。matchstr1显示了表2中的变量,tmpstr是表1中的数据

匹配1[1]=2

matchstr1[1]=型号选择Madeinamerica

tmpstr1[1]=型号选择Madeinamerica

匹配1[2]=3

matchstr1[2]=配置类型_A

tmpstr1[2]=配置类型

匹配1[3]=4

matchstr1[3]=ctrStationpowerSupply\u A

tmpstr1[3]=电源

匹配1[4]=4

matchstr1[4]=确认电源

tmpstr1[4]=电源

匹配1[5]=4

matchstr1[5]=所需的KitPowerSupplyRequired\u C

tmpstr1[5]=电源

匹配1[6]=5

matchstr1[6]=CHSirenAndLightsKeypadPrinting\u D

tmpstr1[6]=CHSirendLightsKeyAdPrinting

匹配1[7]=6

matchstr1[7]=DREnableDR\u 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

谢谢

如果您提供您所编写的代码,即使它没有按预期工作,也可能会有所帮助。这会让人们更好地了解你想要实现的目标。当您包含代码时,请描述它是如何无法满足期望的。如果有任何运行时错误或语法错误,请说明它们是什么以及发生在哪里。好的,让我来编写代码。上面是Debug.Print语句,其格式类似于C程序数组。它不是vba代码。我现在将添加代码