vlookup检索整个行而不是一个单元格

发布于 2025-01-21 11:44:54 字数 1233 浏览 0 评论 0原文

我对下面的代码有一个问题,该代码使用Vlookup在2张纸中匹配第一列,然后将数据从第一个到第二个复制数据,但是我没有复制行中的所有数据,而是仅复制匹配列旁边的第一个单元格。我不知道问题在哪里,所以真正欢迎所有帮助。

Sub solution()

Dim oldRow As Integer
Dim newRow As Integer
Dim lrow_output As Integer  'variable indicating last fulfilled row
Dim WB_Input As Workbook
Dim WB_Output As Workbook
Dim WS_Input As Worksheet
Dim WS_Output As Worksheet
Dim funcStr As String

Set WB_Input = Workbooks("input")
Set WB_Output = Workbooks("output1")

Set WS_Input = WB_Input.Worksheets("input")
Set WS_Output = WB_Output.Worksheets("Sheet1")

With WS_Output
    lrow_output = .Cells(.Rows.Count, 1).End(xlUp).Row
End With

With WS_Input
    funcStr = "=IFERROR(VLOOKUP(" & Cells(1, 1).Address(False, False) & "," & "'[" & WB_Input.Name & "]" & .Name & "'!" & Range(.Columns(1), .Columns(2)).Address & ",2,0),"""")"
End With


With WS_Output
    .Cells(1, 2).Formula = funcStr
    .Cells(1, 2).Copy
    Range(.Cells(1, 2), .Cells(lrow_output, 2)).PasteSpecial xlPasteFormulas
    WS_Output.Calculate
    Range(.Cells(1, 2), .Cells(lrow_output, 2)).Copy
    Range(.Cells(1, 2), .Cells(lrow_output, 2)).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
End With

End Sub

I have a problem with the code below that matches first column in 2 sheets using vlookup then copies data from first to second but I instead of copying all data in the row it only copies the first cells next to the matched column. I have no idea where the problem is so every bit of help is really welcomed.

Sub solution()

Dim oldRow As Integer
Dim newRow As Integer
Dim lrow_output As Integer  'variable indicating last fulfilled row
Dim WB_Input As Workbook
Dim WB_Output As Workbook
Dim WS_Input As Worksheet
Dim WS_Output As Worksheet
Dim funcStr As String

Set WB_Input = Workbooks("input")
Set WB_Output = Workbooks("output1")

Set WS_Input = WB_Input.Worksheets("input")
Set WS_Output = WB_Output.Worksheets("Sheet1")

With WS_Output
    lrow_output = .Cells(.Rows.Count, 1).End(xlUp).Row
End With

With WS_Input
    funcStr = "=IFERROR(VLOOKUP(" & Cells(1, 1).Address(False, False) & "," & "'[" & WB_Input.Name & "]" & .Name & "'!" & Range(.Columns(1), .Columns(2)).Address & ",2,0),"""")"
End With


With WS_Output
    .Cells(1, 2).Formula = funcStr
    .Cells(1, 2).Copy
    Range(.Cells(1, 2), .Cells(lrow_output, 2)).PasteSpecial xlPasteFormulas
    WS_Output.Calculate
    Range(.Cells(1, 2), .Cells(lrow_output, 2)).Copy
    Range(.Cells(1, 2), .Cells(lrow_output, 2)).PasteSpecial xlPasteValues
    Application.CutCopyMode = False
End With

End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文