cy6*_*581 4 excel vba excel-vba
尝试创建将Excel发票表导出为PDF的代码到指定的文件路径.该路径基于发票是否列出某个产品ProductX.
这就是我想出来的,但是在一个范围内遍历每个单元格以查看ProductX是否存在似乎很麻烦.
有更简单的方法吗?感谢任何帮助!
Sub ExportToPDF()
'
Dim file_path As String
Dim search_range As Range
Dim each_cell As Range
' Set search_range as desired search range
Set search_range = ActiveSheet.Range("A53:R56")
For Each each_cell In search_range.Cells
If InStr(1, each_cell.Value, "ProductX", vbTextCompare) Then
file_path = Some_path_A
Else: file_path = Some_path_B
End If
Next each_cell
'Export the sheet as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=file_path, Quality:=xlQualityStandard _
, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
Run Code Online (Sandbox Code Playgroud)
bre*_*tdj 10
您可以使用Find部分匹配.
此代码假定返回的路径包含您需要的文件路径变量 - 您可能需要调整它.
Dim rng1 As Range
Set rng1 = ActiveSheet.Range("A53:R56").Find("ProductX", , xlFormulas, xlPart)
If rng1 Is Nothing Then Exit Sub
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=rng1.Value, Quality:=xlQualityStandard _
, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
Run Code Online (Sandbox Code Playgroud)