Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel Save As autofill macro

Posted on 2011-09-19
6
Medium Priority
?
345 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 1000 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
Industry Leaders: 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!

 

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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

688 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