小编cy6*_*581的帖子

测试某个字符串范围的较短方法

尝试创建将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)

excel vba excel-vba

4
推荐指数
1
解决办法
104
查看次数

标签 统计

excel ×1

excel-vba ×1

vba ×1