Loop with a list and save worksheet separately in a specific path

VBANoob00

New Member
Joined
Apr 5, 2025
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,

Need help. See code below.

VBA Code:
Sub Create_PDF()

Application.ScreenUpdating = False

Dim rs1 As Worksheet, rs2 As Worksheet

Set rs1 = Worksheets("Sheet1")
Set rs2 = Worksheets("Sheet2")

myPath = "C:\Users\ELPADILL\OneDrive - Schenker AG\Desktop\2025 Target Agreement\2024 STIP1 Template & Letters\2024 Target Bonus"

For r = 2 To 6

empName = rs2.Cells(r, "B")
rs1.[B2] = empName

Set Rng = rs1.Range("A1:W41")
Rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myPath & " - " & empName & ".pdf"

Next r
 
Application.ScreenUpdating = False

End Sub

What it does are...

1. Worksheet1 (main worksheet) - calls each name on worksheet2, updates other cells based on data on worksheet3, and the code saves each separately as pdf
2. Worksheet2 (list of names) - there are list of names that needs to be looped
3. Worksheet3 (data) - data looked up by worksheet1

Output is separate pdf files for each name on worksheet.

What I want is for the code to save is as excel file (.xls)

Thanks!
 
Sorry this is my first post and don't know how to edit the original post. Anyway, so what current what the code does is it saves worksheet1 as pdf files for each name on worksheet2.
So if worksheet2 has 5 names once code is executed it will save 5 pdfs on a specific folder.

What I want is for the code to save worksheet1 as excel file (.xls).
 
Upvote 0
Hi, maybe like
VBA Code:
Option Explicit

Sub Create_PDF()
    Application.ScreenUpdating = False
    Dim r As Long, Rng As Range
    Dim wbNew       As Workbook

    Dim rs1 As Worksheet, rs2 As Worksheet
    Set rs1 = Worksheets("Sheet1")
    Set rs2 = Worksheets("Sheet2")

    Dim myPath As String, empName As String
    myPath = "C:\Users\ELPADILL\OneDrive - Schenker AG\Desktop\2025 Target Agreement\2024 STIP1 Template & Letters\2024 Target Bonus"

    For r = 2 To 6
        empName = rs2.Cells(r, "B")
        rs1.[B2] = empName

        Set Rng = rs1.Range("A1:W41")
        Set wbNew = Workbooks.Add

        Rng.Copy Destination:=wbNew.Sheets(1).Range("A1")
        wbNew.SaveAs Filename:=myPath & " - " & empName & ".xls", FileFormat:=xlExcel8
        wbNew.Close SaveChanges:=False
        '        Rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myPath & " - " & empName & ".pdf"
    Next r

    Application.ScreenUpdating = False
End Sub
 
Upvote 0
Thanks!

There was an error though

Run-time error '1004':

Method 'SaveAs' of object'_Workbook' failed

Also forgot to mention, you can remove this part...

VBA Code:
Set Rng = rs1.Range("A1:W41")

As I want to copy the whole worksheet1

Still thanks for replying immediately
 
Upvote 0
Okay, I'll take the name (empName) of the new workbook from the same nested loop.
VBA Code:
Option Explicit

Sub CreateNewBook()
    Dim r           As Long
    Dim wbNew       As Workbook

    Dim rs1 As Worksheet, rs2 As Worksheet
    Set rs1 = ThisWorkbook.Worksheets("Sheet1")
    Set rs2 = ThisWorkbook.Worksheets("Sheet2")

    Dim myPath As String, empName As String
    myPath = "C:\Users\ELPADILL\OneDrive - Schenker AG\Desktop\2025 Target Agreement\2024 STIP1 Template & Letters\2024 Target Bonus"
    Application.ScreenUpdating = False

    For r = 2 To 6

        With rs1
            empName = rs2.Cells(r, "B")
            rs1.[B2] = empName
            .Copy

            Set wbNew = ActiveWorkbook
            wbNew.SaveAs Filename:=myPath & " - " & empName & ".xls", FileFormat:=xlExcel8
            wbNew.Close SaveChanges:=False
        End With

    Next r

    Application.ScreenUpdating = False
End Sub
 
Upvote 0
Solution
Worked! Though it created extra copies of 1st name on the list but it worked. Thank you very much!
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top