Save as Macro named content of cell

josullivan601632

New Member
Joined
Aug 23, 2020
Messages
39
Office Version
  1. 2007
Platform
  1. Windows
Hi can anyone help me write a macro. I'm all good with excel and basic recorded macros, but VBA and the more advanced stuff I always fail at!!! I am working on a spreadsheet with one main tab called "InternalQuote" which feeds information into various other tabs and I want to be able to click on an image (which I will assign to the macro) whilst in the "InternalQuote" tab to;

Save the whole workbook as a new xlsm. document named the contents of "K9" and "K7" of another tab called "Quotation"

I also want to click on another image (which I will assign to the macro) to save the tab titled "Quotation" as a PDF, named also as the contents of "K9" and "K7" which also will open once created for the user to check.

Is this possible? And would anyone be able to spare some precious time and send me something to perhaps (cheekily) copy and paste?
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try these, with the pdf you may have adjust your sheet to fit.

VBA Code:
Sub SaveAsPDF()
Dim strTempFile As String
Dim strData As String
Dim spath As String

    spath = Environ("USERPROFILE") & "\Desktop\"
    strData = Sheets("Quotation").Range("K9").Value & Sheets("Quotation").Range("K7").Value & ".pdf"
    strTempFile = spath & strData

    Sheets("Quotation").ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTempFile, OpenAfterPublish:=True

End Sub

Sub SaveWorkbook()
Dim strTempFile As String
Dim strData As String
Dim spath As String

    spath = Environ("USERPROFILE") & "\Desktop\"
    strData = Sheets("Quotation").Range("K9").Value & Sheets("Quotation").Range("K7").Value & ".xlsm"
    strTempFile = spath & strData
    
    ActiveWorkbook.SaveCopyAs Filename:=strTempFile
    
End Sub
 
Upvote 0
Try these, with the pdf you may have adjust your sheet to fit.

VBA Code:
Sub SaveAsPDF()
Dim strTempFile As String
Dim strData As String
Dim spath As String

    spath = Environ("USERPROFILE") & "\Desktop\"
    strData = Sheets("Quotation").Range("K9").Value & Sheets("Quotation").Range("K7").Value & ".pdf"
    strTempFile = spath & strData

    Sheets("Quotation").ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTempFile, OpenAfterPublish:=True

End Sub

Sub SaveWorkbook()
Dim strTempFile As String
Dim strData As String
Dim spath As String

    spath = Environ("USERPROFILE") & "\Desktop\"
    strData = Sheets("Quotation").Range("K9").Value & Sheets("Quotation").Range("K7").Value & ".xlsm"
    strTempFile = spath & strData
   
    ActiveWorkbook.SaveCopyAs Filename:=strTempFile
   
End Sub
Hi Thank you so much for your time. both return a invalid 400 code error, I run a tab array on the doc which works find so I think the doc is ok with running macros, is there any thing else I can check to correct the 400 error?
 
Upvote 0
Sorry I know nothing about 400 errors, check my spelling matches your workbook, if that's ok then I would try copying your "Quotation" sheet to a new workbook & run the code in there, other than that check out 400 Error Solutions, both codes run fine on my pc.
 
Upvote 0
I copied both sheets to a new book and the codes worked a treat. I even changed the filepath and put a couple of spaces in the file names!!! Thank you for helping me do this (well actually you did it), but I have def picked up a teeny bit more knowledge, the code looks so simple when it is written for you.

One thing, when the excel doc is saved, it works so lovely that I am going in to check it is saved, I am sure there is a pop up that can be added via macro something like "file saved and the filename", can this be added to the macro you already wrote?

Have a great day :)
 
Upvote 0
Thanks, I'm glad it's working, I presume you added the spaces to the strData as part of the file name, if so you can just add "MsgBox strData, , "File Saved" underneath the current code & you will get a message box titled "File Saved" with your filename below, I hope that helps & all the best with your project.
 
Upvote 0
Thanks for your kind words, sometimes the finishing touch makes all difference, good luck with it all.
 
Upvote 0
Hi sorry to bother you again, all still working great (not broken it yet) but I have tried to add an additional macro to the workbook, practically the same as before but this 2nd PDF button prints the brochure instead of the quote, the brochure is another tab. It is returning a 400 error which I know you said you didn't know much about, but I wonder if this is do with my writing of the code. Please would you mind checking?

THE ONE YOU WROTE (without the no.1):

Sub SaveAsPDF1()
Dim strTempFile As String
Dim strData As String
Dim spath As String

spath = Environ("USERPROFILE") & "\Newman Refrigeration Ltd\Newman Refrigeration Ltd - General\Brian\Cost Book\"
strData = Sheets("Quotation").Range("K9").Value & " - " & Value & Sheets("Quotation").Range("K1").Value & ".pdf"
strTempFile = spath & strData
MsgBox strData, , "PDF has been saved with the following filename:"

Sheets("Quotation").ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTempFile, OpenAfterPublish:=True

End Sub

THE ONE I HAVE ADDED, SO JUST COPIED AND PASTED THE ORIGINAL CODE AND ADDED THE NO. 2 (IN RED) SO THEY ARE DIFFERENT BECAUSE IT SEEMED TO WANT ME TO DO THAT!

Sub SaveAsPDF2()
Dim strTempFile As String
Dim strData As String
Dim spath As String

spath = Environ("USERPROFILE") & "\Newman Refrigeration Ltd\Newman Refrigeration Ltd - General\Brian\Cost Book\"
strData = Sheets("Quotation").Range("K9").Value & " - " & Value & Sheets("Quotation").Range("K1").Value & ".pdf"
strTempFile = spath & strData
MsgBox strData, , "PDF has been saved with the following filename:"

Sheets("Quotation").ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTempFile, OpenAfterPublish:=True

End Sub


It ends with the PDF saved pop up then returns the 400 error, so think it must be something I must be doing but do want to learn what I am doing wrong! I promise I wont keep bothering you!!!!

:)

Any thoughts?
 
Upvote 0
Hi, it looks like you have an error in both,

strData = Sheets("Quotation").Range("K9").Value & " - " & Value & Sheets("Quotation").Range("K1").Value & ".pdf"

remove that part & give it a try.
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,305
Members
449,150
Latest member
NyDarR

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