Macro to change one cell on a sheet, save sheet as PDF, then loop back and change that one cell on that sheet and repeat until the end

SMRXIV

New Member
Joined
May 13, 2024
Messages
1
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hi All,

I need some help with building a Macro, I want to create a VBA code which changes one cell on a sheet, save the active print area in the sheet as PDF, then loop back and change that one cell on that sheet and repeat. I want the cell to change between integers of 1000 to 1100. I also want the naming of the PDFs linked to a certain cell so it can be flexible but also want to include some additional text to the file name that isnt flexible.

I am completely useless with VBA so asked chatgpt to help out and after a few iterations it spat this out which still doesn't work the way I wanted so wanted to come here to get some assistance if possible.

Thanks a lot in advance!

+++

Sub SavePrintAreaAsPDF()
2 Dim ws As Worksheet
3 Dim printArea As Range
4 Dim filePath As String
5 Dim fileNamePrefix As String
6 Dim counter As Integer
7
8 ' Set the worksheet to work with
9 Set ws = ThisWorkbook.Worksheets("Sheet1") ' Replace "Sheet1" with your sheet name
10
11 ' Set the file path to save the PDFs
12 filePath = "FilePath" ' Replace with your desired file path
13
14 ' Set the prefix for the file name (additional text)
15 fileNamePrefix = "Prefix_" ' Replace with your desired prefix
16
17 ' Loop through the integers from 1000 to 1100
18 For counter = 1000 To 1100
19 ' Change the value of the specified cell
20 ws.Range("c1").Value = counter ' Replace "C1" with the cell you want to change
21
22 ' Determine the print area using the UsedRange
23 Set printArea = ws.UsedRange
24
25 ' Check if a print area is set
26 If Not printArea Is Nothing Then
27 ' Get the file name from a certain cell
28 Dim fileName As String
29 fileName = fileNamePrefix & ws.Range("B1").Value ' Replace "B1" with the cell containing the flexible part of the file name
30
31 ' Save the print area as PDF
32 printArea.ExportAsFixedFormat Type:=xlTypePDF, fileName:=filePath & fileName & ".pdf", Quality:=xlQualityStandard
33 Else
34 MsgBox "No print area is set."
35 End If
36 Next counter
37 End Sub


Please also let me know if you need more information to assist
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,216,503
Messages
6,131,020
Members
449,615
Latest member
Nic0la

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