如果工作表1包含特定文本,则VBA代码:Vlookup Sheet1复制并粘贴到工作表2上

时间:2014-10-30 21:50:33

标签: excel vba if-statement vlookup

在这里挑选你的大脑。试着这几天。 VBA noob。

两张表:源表(“NB& COax PO详细测试”)&输出表(“新建和同轴测试”)

来源表是一个有组织的清单。 PO在Col I中,每个PO值按月分解(J,F直到dec)。每行都专用于一个唯一的采购订单,月度预测按月细分。

输出表格在行中列出了采购订单,在列中列出了每月预测。如果Col C(输出表)是PO Materials或PO Labor然后Vlookup,则想法是查看源表中每个PO的月度预测,否则跳到下一行。 Vlookup必须将月度预测应用于每个月。在每次Vlookup之后,我试图复制并粘贴该值,以便没有太多编码导致Excel崩溃。同样在源表中,每月上面列出了输出表vlookup的Col Index Num。

简而言之。

如果(或(Outputsheet.Col I =“PO Labor”,Outputsheet.Col I =“PO Materials”),Vlookup(Outputsheet.ColE,SourceSheet.Range(Col I to Col AB:5000),SourceSheetR3,False ),“”复制并粘贴同一行中的下一列重复,直到OutputSheet Col R.复制并粘贴。下一行。

SourceSheet R3是变量,它会发生变化,所以我希望Vlookup从每个月的Col.上面提到列号。

(输出表和源表单击下一个图像)http://imgur.com/SHANSLF&ydjQfb3#0 (代码)http://imgur.com/MieCu5G

1 个答案:

答案 0 :(得分:0)

最后在这个社区的几位慷慨成员和Chandoo社区的帮助下完成了这项工作。这是我放在一起的最终代码,实际上是有效的。

Sub MakeFormulas()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Dim X As Long
Dim Z 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 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).Value = _
  Evaluate("=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

End With
End Sub
相关问题