简体   繁体   中英

creating multiple files from a same worksheet VBA

In a file named FILE I have the following piece of code, which, among other stuff, fills in a table for each part defined in the sheet, and then saves a pdf file containing the part in the name, to be named FILEPART.pdf .

'parts
lastRow = Sheets("overview").Range("G1000").End(xlUp).Row
lastRowHere = Sheets("Source").Range("A13:A1000").End(xlUp).Row
'count of parts
Count = lastRowHere + 2
For m = 1 To Count    
    For n = 14 To lastRow
        Partname = Sheets("overview").Range("C" & n) & 
        _" of " & Sheets("overview").Range("A" & n)
       [...]
    Next n
    'creates the PDF file for mapping on each part
    Set FSO = CreateObject("Scripting.FileSystemObject")
    s(0) = ThisWorkbook.FullName
    If FSO.FileExists(s(0)) Then
        '//Change Excel Extension to PDF extension in FilePath
        s(1) = FSO.GetExtensionName(s(0))
        If s(1) <> "" Then
            s(1) = "." & s(1)
            sNewFilePath = Replace(s(0), s(1), Partname & ".pdf")

            '//Export to PDF with new File Path
            lastPart = Sheets("table").Cells(1000, m * 5 + 1).End(xlUp).Row
            Sheets("table").Range(Cells(1, m * 5 + 1), Cells(lastPart, m * 5 + 5)).
            _ExportAsFixedFormat Type:=xlTypePDF, FileName:=sNewFilePath, 
            _Quality:=xlQualityStandard, IncludeDocProperties:=True, 
            _IgnorePrintAreas:=False, OpenAfterPublish:=False

        End If
    Else
        '//Error: file path not found
        MsgBox "Error: this workbook may be unsaved.  Please save and try again."
    End If
    Set FSO = Nothing
Next m

My problem is that I get a single file, and not multiple ones with different names. Going step by step throught the loop I see that first it creates FILEPART1.pdf , then replace it with FILEPART2.pdf , ending with only FILEPARTLAST.pdf

This version of the code (after SJR's comments) solved the issue

'parts
lastRow = Sheets("overview").Range("G1000").End(xlUp).Row
lastRowHere = Sheets("Source").Range("A13:A1000").End(xlUp).Row
'count of parts
Count = lastRowHere + 2
s(0) = ThisWorkbook.FullName
For m = 1 To Count    
    PartNameSource = Sheets("Source").Range("A" & m + 13)
    For n = 14 To lastRow
        PartnameLine = Sheets("overview").Range("C" & n) & 
        _" of " & Sheets("overview").Range("A" & n)
       [...]
    Next n
    'creates the PDF file for mapping on each part
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(s(0)) Then
        '//Change Excel Extension to PDF extension in FilePath
        s(1) = FSO.GetExtensionName(s(0))
        If s(1) <> "" Then
            s(1) = "." & s(1)
            sNewFilePath = Replace(s(0), s(1), PartnameSource & ".pdf")

            '//Export to PDF with new File Path
            lastPart = Sheets("table").Cells(1000, m * 5 + 1).End(xlUp).Row
            Sheets("table").Range(Cells(1, m * 5 + 1), Cells(lastPart, m * 5 + 5)).
            _ExportAsFixedFormat Type:=xlTypePDF, FileName:=sNewFilePath, 
            _Quality:=xlQualityStandard, IncludeDocProperties:=True, 
            _IgnorePrintAreas:=False, OpenAfterPublish:=False

        End If
    Else
        '//Error: file path not found
        MsgBox "Error: this workbook may be unsaved.  Please save and try again."
    End If
    Set FSO = Nothing
Next m

REMARK

If PartNameSource contains characters which make the file name invalid (like / ) there will be a run-time error 1004 Document not saved. The document may be open, or an error may have been encountered when saving run-time error 1004 Document not saved. The document may be open, or an error may have been encountered when saving

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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