SMRXIV
New Member
- Joined
- May 13, 2024
- Messages
- 1
- Office Version
- Prefer Not To Say
- Platform
- 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
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: