这些是“数据”表中使用的值
1 Bill Jones 53
2 Steve Smith 23
3 Greg Peterson 18
“报告”表上的布局
Column A Column B
First =VLOOKUP(1, Data!A1:D3, 2)
Last =VLOOKUP(1, Data!A1:D3, 3)
Age =VLOOKUP(1, Data!A1:D3, 4)
更新 vlookup 方程然后打印 PDF 的 vba 代码
Sub Create_Report()
Sheets("Report").Select
For i = 1 To 3
' Update vlookup equation
Range("B1").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(" & i & ", Data!RC[-1]:R[2]C[2], 2)"
Range("B2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(" & i & ", Data!R[-1]C[-1]:R[1]C[2], 3)"
Range("B3").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(" & i & ", Data!R[-2]C[-1]:RC[2], 4)"
' Make PDF version of report sheet
Create_PDF Sheets("Report"), "Report" & i & ".pdf"
Next i
End Sub
' Code taken from
'http://msdn.microsoft.com/en-us/library/ee834871%28v=office.11%29.aspx
Sub Create_PDF(Myvar As Object, FixedFilePathName As String)
'Test to see if the Microsoft Create/Send add-in is installed.
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
'Now export the PDF file.
On Error Resume Next
Myvar.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=FixedFilePathName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=OpenPDFAfterPublish
On Error GoTo 0
End If
End Sub
您可以直接调用此代码,使用 Excel 中的按钮,或从其他编程语言调用宏以进一步自动化。
这是在 Excel 2013 上测试的。
FWIW - Create_Report 代码基于一个宏,该宏记录了更新 vlookup 值所涉及的手动步骤。记录宏功能对于弄清楚如何自动化 Excel 有很大帮助。