Saving Spreadsheet as PDF and Xlsx

dlo1503

New Member
Joined
Feb 24, 2020
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hello

I have code I'm currently running and it performs the task exactly as I would like it to to save the file as a PDF. I also want it to create an Xlsx copy as well but I am struggling to get the coding correct.
Anything I've tried either overwrites the original file or it throws up run-time error"5" (invalid procedure call or argument) in the VBA.

I want to save the new files as the value of Q2 which is working well for the PDF

Thanks
Daniel

Sub Button6_Click()
Range("B8:J36").Sort Key1:=Range("C8"), Order1:=xlAscending, Header:= _
xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Dim rngRange As Range
Set rngRange = Worksheets("Sheet1").Range("Q2")
Set rngRange2 = Worksheets("Sheet1").Range("Q2")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
"FILE LOCATION" & rngRange & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False

ActiveSheet.ExportAsFixedFormat Type:=xlWorkbookNormal, filename:= _
"FILE LOCATION" & rngRange2 & ".xls" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Range("c4") = Range("c4") + 1
Range("B9:J36").ClearContents
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try using this

VBA Code:
ActiveSheet.Copy
    ActiveWorkbook.SaveAs Filename:="FILE LOCATION" & rngRange2 & ".xlsx" _
        , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    ActiveWindow.Close
Range("c4") = Range("c4") + 1
Range("B9:J36").ClearContents
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,289
Members
448,885
Latest member
LokiSonic

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