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
 
I need to apologize, its my own stupidity. I got it working.

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?
Now only this one left, can this be done?
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Run-time error 1004

Cant do that to a merged cell

No you cannot, which is why you should avoid merged cells when possible. To clear a range containing merged cells, the range has to include ALL cells that make up the merged cells.
 
Upvote 0
Not tested.

VBA Code:
Sub saveSheetWithoutFormulas()
    Dim saveLocation As String, xlsxFile  As String
    Dim WB As Workbook, WS As Worksheet
    Dim SheetName As String
    
    SheetName = ActiveSheet.Name
    
    saveLocation = "C:\Users\****\Desktop\" & ActiveSheet.Range("f4").Value
    xlsxFile = Split(saveLocation, ".")(0) & ".xlsx"
    
    Application.DisplayAlerts = False
    ThisWorkbook.SaveCopyAs FileName:=saveLocation
    
    Set WB = Application.Workbooks.Open(FileName:=saveLocation)
    Set WS = WB.Worksheets(SheetName)
    WS.UsedRange.Value = WS.UsedRange.Value

    WB.SaveAs FileName:=xlsxFile, FileFormat:=xlOpenXMLWorkbook    'see XlFileFormat Enumeration for different formats
    WB.Close False
    Application.DisplayAlerts = False
        
    Kill saveLocation
End Sub
 
Upvote 0
Solution
Not tested.

VBA Code:
Sub saveSheetWithoutFormulas()
    Dim saveLocation As String, xlsxFile  As String
    Dim WB As Workbook, WS As Worksheet
    Dim SheetName As String
   
    SheetName = ActiveSheet.Name
   
    saveLocation = "C:\Users\****\Desktop\" & ActiveSheet.Range("f4").Value
    xlsxFile = Split(saveLocation, ".")(0) & ".xlsx"
   
    Application.DisplayAlerts = False
    ThisWorkbook.SaveCopyAs FileName:=saveLocation
   
    Set WB = Application.Workbooks.Open(FileName:=saveLocation)
    Set WS = WB.Worksheets(SheetName)
    WS.UsedRange.Value = WS.UsedRange.Value

    WB.SaveAs FileName:=xlsxFile, FileFormat:=xlOpenXMLWorkbook    'see XlFileFormat Enumeration for different formats
    WB.Close False
    Application.DisplayAlerts = False
       
    Kill saveLocation
End Sub
You are a machine! Its working perfectly! Thank you very much!
 
Upvote 0
Hey @rlv01 ,

I know I am supposed to start a new post, but I am not getting response on it there. May I ask to help me with invoice increment?

I have this code
VBA Code:
Range("f4").Value = Range("f4").Value + 1

Inserted it all over the code, keep giving me an error. I have no idea where to place it. It is supposed to increment the Template sheet number +1 each time the macro is run.

I would also like to transfer the main info of each estimate to a estimate database sheet i.e. estimate number "F4", date "F3", Client "A14" and estimate total "G44" when the macro is run. I haven't started to try and figure this part out
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,192
Members
449,072
Latest member
DW Draft

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