简体   繁体   English

根据图纸名称仅将Excel VBA中的某些图纸动态保存为PDF

[英]Dynamic saving to PDF only certain Sheets in Excel VBA based on sheetname

I have an Excel workbook with 5 sheets(I'm simplifying for clarity here): 我有5张纸的Excel工作簿(为简化起见,我在这里简化):

'DataMaster'

'CityData S'

'TownData S'

'TownlandData S'

'StreetData S'

Sometimes there are additional sheets/tabs but the sheets to be saved are all suffixed with the ' S' suffix. 有时还有其他工作表/选项卡,但要保存的工作表均带有“ S”后缀。 'Datamaster' here does not need to be saved. 这里的“ Datamaster”不需要保存。

I wish to export/save to PDF using a loop that 我希望使用一个循环将其导出/保存为PDF

1) Only will look at sheets with rightmost 2 characters in their names being ' S'. 1)仅查看名称中最右边2个字符为“ S”的工作表。

2) Will not care if there 3 or 10 of these sheets. 2)不在乎这些表是3还是10。

3) will export to PDF with filename being the leftphrase in the sheetname. 3)将导出为PDF,文件名是工作表名称中的左短语。

Thus the export would be: 因此,出口将是:

CityData.PDF , 
TownData.PDF , 
TownlandData.PDF , 
StreetData.PDF

Any tips/help would be appreciated, I have the basic export to PDF code only. 任何提示/帮助将不胜感激,我只基本导出为PDF代码。

You can use a For Each loop to loop through every worksheet in your workbook. 您可以使用For Each循环遍历工作簿中的每个工作表。 You can use an If to test if the Right() two characters match. 您可以使用If来测试Right()两个字符是否匹配。

Something like: 就像是:

Sub SaveToPDF
    Dim ws as worksheet

    'Loop through worksheets, each worksheet is assigned to variable "ws" declared above:
    For Each ws in ThisWorkbook.Worksheets
        If Right(ws.name, 2) = " S" Then
            'export as pdf
            ws.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                Filename:="C:\" & Left(ws.name, len(ws.name)-2) & ".pdf", _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=True
        End If
    Next ws
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM