ExCellibur
New Member
- Joined
- Sep 27, 2021
- Messages
- 1
- Office Version
- 365
- Platform
- Windows
Hi all,
Hope you are well.
I am trying to create a VBA macro that saves or exports a pdf file based on a range that populates a sheet based on the cell value captured. Each cell in said range contains an IF formula. I have gathered a few different scripts to come up with a macro to identify non blank values in a range and then populate a sheet that links the cell values pulled from said range with other relevant data connected with index/match functions. The problem I am having is that the exportasFixedformat runs but then ends with an error prompt. In my understanding, it is because the range I have set does not ignore the blank values as I intended it so that it does not run in error. I have the following script:
Sub ExportToPDF()
Dim rng As Range, c As Range, lo As ListObject, iCol As Long, LastRow As Long
Application.ScreenUpdating = False
ActiveWorkbook.ActiveSheet.Visible = True
With ActiveWorkbook.Sheets("Calculator")
Set lo = ActiveSheet.ListObjects(2)
iCol = lo.ListColumns("ID").Index
lo.Range.AutoFilter Field:=iCol, Criteria1:="<>"
Set rng = .Range("S2", .Cells(.Rows.Count, "S").End(xlUp))
End With
For Each c In rng
With ActiveWorkbook.Sheets("Payslip")
.Range("C3").Value = c.Value
.Calculate
.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & Format(Now(), "mmddyyyy") & CStr(c.Value) & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
Next c
End Sub
How can I improve this script or is there another way to write to get the macro right? Any help would be appreciated!
Hope you are well.
I am trying to create a VBA macro that saves or exports a pdf file based on a range that populates a sheet based on the cell value captured. Each cell in said range contains an IF formula. I have gathered a few different scripts to come up with a macro to identify non blank values in a range and then populate a sheet that links the cell values pulled from said range with other relevant data connected with index/match functions. The problem I am having is that the exportasFixedformat runs but then ends with an error prompt. In my understanding, it is because the range I have set does not ignore the blank values as I intended it so that it does not run in error. I have the following script:
Sub ExportToPDF()
Dim rng As Range, c As Range, lo As ListObject, iCol As Long, LastRow As Long
Application.ScreenUpdating = False
ActiveWorkbook.ActiveSheet.Visible = True
With ActiveWorkbook.Sheets("Calculator")
Set lo = ActiveSheet.ListObjects(2)
iCol = lo.ListColumns("ID").Index
lo.Range.AutoFilter Field:=iCol, Criteria1:="<>"
Set rng = .Range("S2", .Cells(.Rows.Count, "S").End(xlUp))
End With
For Each c In rng
With ActiveWorkbook.Sheets("Payslip")
.Range("C3").Value = c.Value
.Calculate
.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & Format(Now(), "mmddyyyy") & CStr(c.Value) & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End With
Next c
End Sub
How can I improve this script or is there another way to write to get the macro right? Any help would be appreciated!