VBA EXCEL比较列并带来价值

时间:2018-01-03 19:12:01

标签: excel vba excel-vba

Image1

嗨,参考图像,我试图比较列G和列K,如果值相同,则将列J中的值复制到列F.但是,我的代码不会复制列中的值J到F.

Sub createarray1()

    Dim i As Integer
    Dim j As Integer
    Dim masterarray As Range
    Set masterarray = Range("D3:G12")
    Dim sourcearray As Range

    Set sourcearray = Range("H3:K26")

    For i = 1 To 10
        For j = 1 To 25
            If masterarray(i, 4).Value = sourcearray(j, 4).Value Then
                masterarray(i, 3) = sourcearray(j, 3).Value
            Else
                masterarray(i, 3).Value = ""
            End If
        Next
    Next

End Sub

Function concatenate()

    Dim nlastrow As Long

    For i = 2 To Cells(Rows.Count, "D").End(xlUp).Row
        Cells(i, "G").Value = Cells(i, "D").Value & "_" & Cells(i, "E").Value
    Next i

    Dim nnlastrow As Long

    For i = 2 To Cells(Rows.Count, "H").End(xlUp).Row
        Cells(i, "K").Value = Cells(i, "H").Value & "_" & Cells(i, "I").Value
    Next i

End Function

1 个答案:

答案 0 :(得分:1)

使用变量数组,这样就可以将对工作表的调用次数限制为仅为3。

当你找到肯定时,你需要退出内循环。

ctx.body = await https_req(options)

但这可以通过以下公式完成:

Sub createarray1()

    Dim i As Long
    Dim j As Long
    Dim masterarray As Variant
    Dim sourcearray As Variant
    With ThisWorkbook.Worksheets("Sheet1") ' change to your sheet
        masterarray = .Range("D3:G12")
        sourcearray = .Range("H3:K26")

        For i = LBound(masterarray, 1) To UBound(masterarray, 1)
            masterarray(i, 3) = ""
            For j = LBound(sourcearray, 1) To UBound(sourcearray, 1)
                If masterarray(i, 4) = sourcearray(j, 4) Then
                    masterarray(i, 3) = sourcearray(j, 3)
                    Exit For
                End If
            Next j
        Next i
        .Range("D3:G12") = masterarray
    End With
End Sub

将其放入F3并复制/向下拖动。