简体   繁体   English

VBA IF然后Vlookup多个工作表

[英]VBA IF Then Vlookup Multiple Sheet

I am trying to retrieve data from another file using the VLOOKUP function however this is only to happen depending on if any of the 2 items of data appear in column 3(C) 我正在尝试使用VLOOKUP函数从另一个文件中检索数据,但这仅取决于3栏(C)中是否出现了这2个数据项

"PO Materials" OR "PO Labor" “ PO材料”或“ PO人工”

Sub MakeFormulas()

Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Dim X As Long

'What are the names of our worksheets?
Set sourceSheet = Worksheets("Sheet1")
Set outputSheet = Worksheets("Sheet2")


'Determine last row of source
With sourceSheet
    SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

With outputSheet

    'Determine last row in col C
    OutputLastRow = .Cells(.Rows.Count, "E").End(xlUp).Row

    For X = 5 To OutputLastRow
        If InStr(1, .Range("C" & X), "PO Materials") > 0 Then
            'Apply our formula
        .Range("Q2:Q" & OutputLastRow).Formula = _
            "=VLOOKUP(E2,'" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)"
        End If
    Next
End With

End Sub

Code is working; 代码正在运行; However, its giving #N/A if the cell is blank or contains any value in Column C, which means its not recognizing the If statement. 但是,如果单元格为空或在C列中包含任何值,则给出#N / A,这意味着它无法识别If语句。 If the column does not containn "PO Materials" or "PO Labor" I would like it to skip to the next cell in the range 如果该列不包含“ PO物料”或“ PO人工”,则希望跳至该范围内的下一个单元格

Thanks in advance for any help given. 在此先感谢您提供的任何帮助。

The code that generates the VLOOKUP is applying that formula to each cell in the column every time. 生成VLOOKUP的代码每次都会将该公式应用于列中的每个单元格。 That is, every time your If condition finds "PO Materials", it will apply the VLOOUKP to every cell between Q2 and the last row from column E. 也就是说,每次你的时间If条件可找到“PO材料”,它将VLOOUKP适用于Q2和E列中的最后一行之间的每一个细胞

I think this is what you want: 我认为这是您想要的:

.Range("Q" & X).Formula = _
    "=VLOOKUP(E" & X & ",'" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)"

Alternatively, it could be done completely within the spreadsheet: 另外,也可以在电子表格中完全完成此操作:

=IF(ISERROR(FIND("PO Materials",C6)),"",VLOOKUP(E6,Sheet1!$A$2:$B$6,2,0))

Final Code for future reference 最终代码以供将来参考

Sub MakeFormulas()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Dim X As Long

'What are the names of our worksheets?
Set sourceSheet = Worksheets("Sheet1")
Set outputSheet = Worksheets("Sheet2")


'Determine last row of source
With sourceSheet
SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With outputSheet

'Determine last row in col C
OutputLastRow = .Cells(.Rows.Count, "C").End(xlUp).Row

For X = 2 To OutputLastRow
If InStr(1, .Range("C" & X), "PO Materials") + InStr(1, .Range("C" & X), "PO Labor") > 0 Then
    'Apply our formula
.Range("Q" & X).Formula = _
"=VLOOKUP(E" & X & ",'" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)"
End If
Next
End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM