Save active sheet as PDF and new excel sheet, clear content

Jnb99

Board Regular
Joined
Mar 29, 2016
Messages
84
Hi everyone,

I need help with my code saving active sheet as PDF with file name as specific cell value.

File name must be combination of "F4" + "A14"
Range that must be saved: Sheet = "Estimate" and Range = (A1:G50)

Lastly cells (A23:A41) must "ClearContents" when vba is run

Currently I have this code, which save PDF fine, but thats where it stops.

VBA Code:
Private Sub CommandButton1_Click()
Dim saveLocation As String
Dim rng As Range

saveLocation = "C:\Users\****\Desktop\" & Range("f4").Value & Range("a14").Value & ".pdf"
Set rng = Sheets("Estimate").Range("A1:g50")


rng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation

Worksheets("Estimate").Range("A23:a41").ClearContents
End Sub

Sub saveSheetWithoutFormulas()
 Worksheets("Estimate").Copy
    
   ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    
    ActiveWorkbook.saveas Range("f4")

End Sub

Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The code you posted appears to do everything you say you want it to do. What is the problem you are having?
 
Upvote 0
The code you posted appears to do everything you say you want it to do. What is the problem you are having?
Hi,

Thank you for your quick reply.

It’s not saving a copy of the “estimate” worksheet, or if it is, I can not find. Pdf is saving perfect
 
Upvote 0
Maybe something like this then.

VBA Code:
Private Sub CommandButton1_Click()
    Dim saveLocation As String
    Dim rng As Range

    saveLocation = "C:\Users\****\Desktop\" & Range("f4").Value & Range("a14").Value & ".pdf"
    
    Set rng = Worksheets("Estimate").Range("A1:g50")
    rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation

    Worksheets("Estimate").Range("A23:a41").ClearContents
    Call saveSheetWithoutFormulas
End Sub

Sub saveSheetWithoutFormulas()
    Dim saveLocation As String
    Dim rng As Range

    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    
    saveLocation = "C:\Users\****\Desktop\" & ActiveSheet.Range("f4").Value
    ActiveWorkbook.SaveAs Filename:=saveLocation
End Sub
 
Upvote 0
Hi,

Sorry for the long delay.

I copied the code, but now nothing is working. It first failed on the clear contents bit, so I took it out to test the rest of the code, but its doing nothing
 
Upvote 0
Wait, I think I made a mistake somewhere, its working!

Only clear contents still a headache
 
Upvote 0
Ok, another problem. I want it to save the excel file as a normal excel file, not a macro enabled one. Currently it is also saving the active workbook as the file name as per the description in the code. I would like it to save a copy of the sheet, and keep the active workbook as a template. Is that possible?
 
Upvote 0
Hi,

Sorry for the long delay.

I copied the code, but now nothing is working. It first failed on the clear contents bit, so I took it out to test the rest of the code, but its doing nothing

There is nothing wrong with this statement. I tested it on my PC

VBA Code:
    Worksheets("Estimate").Range("A23:a41").ClearContents

When you say clear contents failed, what do you mean?
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,900
Members
449,194
Latest member
JayEggleton

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