Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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 将两个数组与行匹配';价值观_Excel_Vba - Fatal编程技术网

Excel 将两个数组与行匹配';价值观

Excel 将两个数组与行匹配';价值观,excel,vba,Excel,Vba,我想写一个使用两个1D数组的代码,基于与行上的值的匹配,它应该返回第三个数组中的值 这就是我想做的: 在Sheet1中,我有3列数据,包括ID、名称和金额,其中有许多行大小不确定: 在Sheet2中,我已经有了关于ID和名称的列,但没有关于金额的数据: 因此,我希望运行代码,将Sheet1中ID和Name数据与Sheet2中ID和Name数据的数组相匹配,然后将相应的金额数据按Sheet1中的方式返回给Sheet2 这是运行代码后Sheet2中所需的结果,即根据与Sheet1中ID和Name

我想写一个使用两个1D数组的代码,基于与行上的值的匹配,它应该返回第三个数组中的值

这就是我想做的:

在Sheet1中,我有3列数据,包括ID、名称和金额,其中有许多行大小不确定:

在Sheet2中,我已经有了关于ID和名称的列,但没有关于金额的数据:

因此,我希望运行代码,将Sheet1中ID和Name数据与Sheet2中ID和Name数据的数组相匹配,然后将相应的金额数据按Sheet1中的方式返回给Sheet2

这是运行代码后Sheet2中所需的结果,即根据与Sheet1中ID和Name上的数组的匹配情况返回Amount列中的数据:

这是我的代码,它不能正常运行:

Sub ArrayMatch()

Dim r As Long
Dim d As Long
Dim w_output As Worksheet
Dim w1 As Worksheet
Dim intLastRow As Integer
Dim IntLastCol As Integer
Dim arrName() As Variant
Dim arrID() As Variant
Dim arrrAmoun() As Variant

d = 8

With ThisWorkbook
    Set w1 = .Sheets("Sheet1")
    Set w_output = .Sheets("Sheet2")
End With

'***********************************
'Assign arrays

With w1

    intLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    IntLastCol = .Cells(4, Columns.Count).End(xlToLeft).Column
    arrID = .Range(.Cells(4, 1), .Cells(intLastRow, 1))
    arrName = .Range(.Cells(4, 3), .Cells(intLastRow, 2))
    arrAmoun = .Range(.Cells(4, 4), .Cells(intLastRow, 3))

    For r = 1 To UBound(arrID, 1)
        If Len(arrID(r, 1)) > 0 Then
            d = d + 1
                If w_output.Cells(d, 1) = arrID(r, 1) Then
                    If w_output.Cells(d, 2) = arrName(r, 1) Then
                       w_output.Cells(d, 4) = arrAmoun(r, 1)
                    End If
                End If
        End If
    Next r

End With

End Sub
我的代码没有返回任何内容,我可以假设这是因为我正在比较sheet1中的数组与Sheet2中的行,这在大小上是不可比较的,但我不知道如何以另一种方式进行比较。
我将非常感谢您的帮助。

刚刚修改了您的代码,添加了一个内部循环,用于在
w\u输出
表中检查ID和名称(也可以通过
查找
完成)。用临时数据测试。但是,还有其他(更有效的)方法来实现相同的目标

Sub ArrayMatch()

Dim r As Long
Dim d As Long
Dim w_output As Worksheet
Dim w1 As Worksheet
Dim intLastRow As Long            ' Modified to long
Dim IntLastRow1 As Long           ' Modified to long
Dim arrName() As Variant
Dim arrID() As Variant
Dim arrrAmoun() As Variant

'd = 8

With ThisWorkbook
    Set w1 = .Sheets("Sheet1")
    Set w_output = .Sheets("Sheet2")
End With

'***********************************
'Assign arrays

With w1

    intLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    IntLastRow1 = w_output.Cells(Rows.Count, 1).End(xlUp).Row
    arrID = .Range(.Cells(4, 1), .Cells(intLastRow, 1))
    arrName = .Range(.Cells(4, 3), .Cells(intLastRow, 3))
    arrAmoun = .Range(.Cells(4, 4), .Cells(intLastRow, 4))

    For r = 1 To UBound(arrID, 1)
        If Len(arrID(r, 1)) > 0 Then
            For d = 9 To IntLastRow1     ' Modified to for loop for w_output sheet
                If w_output.Cells(d, 1) = arrID(r, 1) Then
                    If w_output.Cells(d, 2) = arrName(r, 1) Then
                    w_output.Cells(d, 4) = arrAmoun(r, 1)
                    Exit For            ' added once found and amount  put in place
                    End If
                End If
            Next
        End If
    Next r

End With

End Sub

只需修改代码,使其包含一个内部循环,以检查
w_输出
表中的ID和名称(也可以通过
Find
完成)。用临时数据测试。但是,还有其他(更有效的)方法来实现相同的目标

Sub ArrayMatch()

Dim r As Long
Dim d As Long
Dim w_output As Worksheet
Dim w1 As Worksheet
Dim intLastRow As Long            ' Modified to long
Dim IntLastRow1 As Long           ' Modified to long
Dim arrName() As Variant
Dim arrID() As Variant
Dim arrrAmoun() As Variant

'd = 8

With ThisWorkbook
    Set w1 = .Sheets("Sheet1")
    Set w_output = .Sheets("Sheet2")
End With

'***********************************
'Assign arrays

With w1

    intLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
    IntLastRow1 = w_output.Cells(Rows.Count, 1).End(xlUp).Row
    arrID = .Range(.Cells(4, 1), .Cells(intLastRow, 1))
    arrName = .Range(.Cells(4, 3), .Cells(intLastRow, 3))
    arrAmoun = .Range(.Cells(4, 4), .Cells(intLastRow, 4))

    For r = 1 To UBound(arrID, 1)
        If Len(arrID(r, 1)) > 0 Then
            For d = 9 To IntLastRow1     ' Modified to for loop for w_output sheet
                If w_output.Cells(d, 1) = arrID(r, 1) Then
                    If w_output.Cells(d, 2) = arrName(r, 1) Then
                    w_output.Cells(d, 4) = arrAmoun(r, 1)
                    Exit For            ' added once found and amount  put in place
                    End If
                End If
            Next
        End If
    Next r

End With

End Sub

屏蔽编号似乎总是比ID编号小11。这是真的吗???你需要VBA吗?如果我理解正确,你可以使用是的,我想要VBA,这就是为什么我写的代码不起作用。没有索引,我想使用数组。@Gary'sStudent:值是多少并不重要。如何在相应的列中匹配它们很重要。屏蔽编号似乎总是比ID编号小11。这是真的吗???你需要VBA吗?如果我理解正确,你可以使用是的,我想要VBA,这就是为什么我写的代码不起作用。没有索引,我想使用数组。@Gary'sStudent:值是多少并不重要。如何在相应的列中匹配它们很重要。您是否在excel中进行了测试?因为在运行它时,什么都没有发生,没有返回值,甚至没有任何msgBox。为什么?对不起,我在表格1的第2栏用名字测试了它。但由于名称在Sheet1的第3列中,数组
arrName
的赋值出错。用新的作业(表1第3栏中的名称)重新测试,再次发现工作正常。编辑了答案,我也没意识到。非常感谢工作顺利:)干杯!嗨,你用excel测试过吗?因为在运行它时,什么都没有发生,没有返回值,甚至没有任何msgBox。为什么?对不起,我在表格1的第2栏用名字测试了它。但由于名称在Sheet1的第3列中,数组
arrName
的赋值出错。用新的作业(表1第3栏中的名称)重新测试,再次发现工作正常。编辑了答案,我也没意识到。非常感谢工作顺利:)干杯!