Vlookup执行标准

时间:2014-11-11 21:31:44

标签: excel vba

如何根据特定值修改vlookup的执行。我希望它仅在输出表(表2)单元格(Q2到AB2)包含“预测”时才执行Vlookup,否则如果在相对单元格中标记为“实际”则跳过列。

最后,我想将列Q中的任何单元格复制并粘贴到包含vlookup forumla的AB中。我相信这可以使用String函数来完成。

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

'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  formula
.Range("Q" & X & ":AB" & X).Formula = _
"=VLOOKUP($E" & X & ",'" & sourceSheet.Name & "'!$A$2:$L$" & SourceLastRow & ",Match(Q$1,'" &    sourceSheet.Name & "'!$A$1:$AD$1,0),0)"
End If
Next
 End With
 End Sub

表2截图

screenshot

1 个答案:

答案 0 :(得分:1)

我只会硬编码:

For Y = 17 To 28 'Q to AB
  For X = 2 To OutputLastRow
  If InStr(1, .Range("C" & X), "PO Materials") + InStr(1, .Range("C" & X), "PO Labor") > 0 And Cells(2, Y) = "Forecast" Then
  'Apply  formula
  .Cells(X, Y).Formula = _ 'cell at row X, column Y
  "=VLOOKUP($E" & X & ",'" & sourceSheet.Name & "'!$A$2:$L$" & SourceLastRow & ",Match(" & cells(1,Y).address & ",'" & sourceSheet.Name & "'!$A$1:$AD$1,0),0)"
  End If
  Next
Next  

在应用公式之前,它会分解以检查每列中的第二个单元格