Solved

Excel Save As autofill macro

Posted on 2011-09-19
6
341 Views
Last Modified: 2012-05-12
Hi,

I'm trying to write a macro for my excel file that saves the document with a specific name.

E.g. for a document like this, i want to be able to push the macro button

experts-saveas.xlsx

and have a save as screen pop up with my specific data auto-filling the document name section

screen to pop up
thanks!
0
Comment
Question by:WTC_Services
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:Norie
ID: 36565135
This will take the filename from the A3:C3, formatting the date to ddmmyyyy as the format in the cell isn't valid.
Option Explicit

Sub Button1_Click()
Dim wbThis As Workbook
Dim strInitialName As String
Dim strSaveName As Variant

    Set wbThis = ThisWorkbook
    strInitialName = Range("A3") & "_" & Format(Range("B3"), "ddmmyyyy") & "_" & Range("C3") & ".xlsm"
    
    
    strSaveName = Application.GetSaveAsFilename(strInitialName, "Excel Workbook (*.xlsm),*.xlsm)")
    
    If TypeName(strSaveName) = "String" Then
        wbThis.SaveAs strSaveName, xlWorkbookNormal
    Else
        MsgBox "File not saved"
    End If
End Sub

Open in new window

0
 

Author Comment

by:WTC_Services
ID: 36584723
Thanks imnorie,

This should help a lot with naming my file, my dilemma now is that i am trying to save to pdf, currently my macro is like this:

        ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\Users\nickf\Desktop\Wilson.pdf", Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        True

now i wish to incorporate your method with my current script, can anyone help?

WTC
0
 
LVL 2

Accepted Solution

by:
geoffkk earned 250 total points
ID: 36814954
Sounds simple:

Sub Button1_Click()
Dim wbThis As Workbook
Dim strInitialName As String
Dim strSaveName As Variant

    Set wbThis = ThisWorkbook
    strInitialName = Range("A3") & "_" & Format(Range("B3"), "ddmmyyyy") & "_" & Range("C3") & ".xlsm"
   
   
    strSaveName = Application.GetSaveAsFilename(strInitialName, "PSF file(*.pdf),*.pdf)")
   
    If TypeName(strSaveName) = "String" Then
       ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        strSaveName , Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        True
    Else
        MsgBox "File not saved"
    End If
End Sub

Geoff
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:WTC_Services
ID: 36906470
Geoff,

Thank you, this brings up the save as pdf box but for some reason "Wilson_20092011_Melbourne" doesn't appear in the file name section. Maybe there is something wrong with strSaveName??

WTC
0
 
LVL 34

Assisted Solution

by:Norie
Norie earned 250 total points
ID: 36906721
If you want the initiall filename to appear then it needs to have the same extension, ie pdf, as the filter you are using.
Option Explicit

Sub Button1_Click()
Dim wbThis As Workbook
Dim strInitialName As String
Dim strSaveName As Variant
Dim strPath As String

    Set wbThis = ThisWorkbook

    strInitialName = Range("A3") & "_" & Format(Range("B3"), "ddmmyyyy") & "_" & Range("C3") & ".pdf"

    strPath = "C:\Users\nickf\Desktop\"

    strSaveName = Application.GetSaveAsFilename(strInitialName, "PDF Document (*.pdf),*.pdf")

    If TypeName(strSaveName) = "String" Then

        wbThis.ExportAsFixedFormat Type:=xlTypePDF, _
                                                Filename:=strPath & strSaveName, _
                                                Quality:=xlQualityStandard, _
                                                IncludeDocProperties:=True, _
                                                IgnorePrintAreas:=False, _
                                                OpenAfterPublish:=True

    Else
        MsgBox "File not saved"
    End If

End Sub

Open in new window


Note, this saves the workbook the code is in (ThisWorkbook).

If you want to save another workbook then you'll need to give details.
0
 

Author Closing Comment

by:WTC_Services
ID: 36907258
Thanks for the help guys, both of your solutions helped with my problem!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question