VBA export/save PDF from sheet select only non blank cells

ExCellibur

New Member
Joined
Sep 27, 2021
Messages
1
Office Version
  1. 365
Platform
  1. 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!
 

Attachments

  • Capture.PNG
    Capture.PNG
    9.6 KB · Views: 20
  • Capture2.PNG
    Capture2.PNG
    24.9 KB · Views: 20

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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