Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 23

Thread: Solved: Save As

  1. #1
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location

    Solved: Save As

    How can I set below code to let user enter the filename instead of giving direct "Training Audit.xls" and is it possible to let user save it in any location?

    [vba]Sub macro5()
    ChDir "C:\Documents and Settings\ofsjcr\Desktop\"
    ActiveWorkbook.SaveAs Filename:= _
    "C:\Documents and Settings\ofsjcr\Desktop\Training Audit.xls", FileFormat:= _
    xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    , CreateBackup:=False
    End Sub[/vba]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  2. #2
    VBAX Contributor moa's Avatar
    Joined
    Nov 2006
    Posts
    177
    Location
    Not sure about the best approach but look up the GetSaveAsFilename method in help.
    Glen

  3. #3
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Hey Jammer,

    Have u tried the Application.GetSaveAsFilename option. I've not tried it for now, but i've used it before for saving as different file name. The location of where to save it needs to be worked on.

    Lynnnow

  4. #4
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Could you show me a bit of the code? thanks

    Quote Originally Posted by lynnnow
    Hey Jammer,

    Have u tried the Application.GetSaveAsFilename option. I've not tried it for now, but i've used it before for saving as different file name. The location of where to save it needs to be worked on.

    Lynnnow
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  5. #5
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Hi Jammer

    This works, try this.

    [vba]
    FileSaver1 = Application.GetSaveAsFilename() ' You can set an initial file name in this parentheses
    ActiveWorkbook.SaveAs FileName:=FileSaver1
    [/vba]
    HTH

    I had variables passing down the name of the file so i've deleted that part.
    Lynnnow

  6. #6
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Perfect lynnnow thanks.

    Quote Originally Posted by lynnnow
    Hi Jammer

    This works, try this.

    [vba]
    FileSaver1 = Application.GetSaveAsFilename() ' You can set an initial file name in this parentheses
    ActiveWorkbook.SaveAs FileName:=FileSaver1
    [/vba]
    HTH

    I had variables passing down the name of the file so i've deleted that part.
    Lynnnow
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  7. #7
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Hey Jammer,

    Glad to be of help.

    Lynnnow

  8. #8
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    one more thing Save as works very fine now but when I click cancel Error 1004 appears "Method SaveAs of Object WorkBook Failed". I was trying to put if condition like

    [VBA]If filesaver1 <> False Then
    MsgBox "Save as " & filesaver1
    End If[/VBA]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  9. #9
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    I guess i found solution...

    [VBA]filesaver1 = Application.GetSaveAsFilename()
    If filesaver1 <> False Then
    MsgBox "Save as " & filesaver1
    ActiveWorkbook.SaveAs Filename:=filesaver1
    End If[/VBA]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  10. #10
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Hey Jammer,

    I tried out the scenario you've indicated, but I don't get any errors. I clicked on cancel and the If exited without problems. I tried putting a msgbox, that worked fine too.

    this is wot i've used.

    [VBA] FileSaver1 = Application.GetSaveAsFilename()
    If FileSaver1 <> False Then
    MsgBox "File will be saved: " & ActiveWorkbook.FullName
    ActiveWorkbook.SaveAs FileName:=FileSaver1
    End If[/VBA]

    HTH

    Lynnnow

  11. #11
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    Now I have error in Save as with this code
    [vba]
    filesaver1 = Application.GetSaveAsFilename()
    If filesaver1 <> False Then
    MsgBox "Save as " & filesaver1
    ActiveWorkbook.SaveAs Filename:=filesaver1
    End If
    [/vba]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  12. #12
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    wotz the error u're getting coz it worked fine with me.

  13. #13
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    When I click SAVE in "Save Dialog Box its okay but when i click CANCEL Run-time error '1004'
    Method 'SaveAs' of object'_WorkBook' Failed

    Quote Originally Posted by lynnnow
    wotz the error u're getting coz it worked fine with me.
    Last edited by jammer6_9; 05-08-2007 at 03:03 AM.
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  14. #14
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    You're right lynnnow... The code below works fine... Shukrya...

    [VBA]Sub macro5()
    Dim filesaver1 As Variant
    filesaver1 = Application.GetSaveAsFilename()
    If filesaver1 <> False Then
    MsgBox "File will be saved: " & ActiveWorkbook.FullName
    ActiveWorkbook.SaveAs Filename:=filesaver1
    End If
    End Sub
    [/VBA]
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  15. #15
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Better try this. Will be saved as xls. Yours just a file without extension if you don't type it in.[VBA]Sub file_to_save()
    Dim sFile As Variant
    sFile = Application.GetSaveAsFilename(InitialFileName:="", _
    fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls", Title:="Save workbook to..")
    If sFile <> False Then
    ThisWorkbook.SaveAs Filename:=sFile
    Else
    MsgBox ("Please give a name ..."), vbInformation
    End If
    End Sub[/VBA]Charlize

  16. #16
    VBAX Mentor jammer6_9's Avatar
    Joined
    Apr 2007
    Location
    Saudi Arabia
    Posts
    318
    Location
    This is a "Charlize User Friendly Version"... Tnx...

    Quote Originally Posted by Charlize
    Better try this. Will be saved as xls. Yours just a file without extension if you don't type it in.[vba]Sub file_to_save()
    Dim sFile As Variant
    sFile = Application.GetSaveAsFilename(InitialFileName:="", _
    fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls", Title:="Save workbook to..")
    If sFile <> False Then
    ThisWorkbook.SaveAs Filename:=sFile
    Else
    MsgBox ("Please give a name ..."), vbInformation
    End If
    End Sub[/vba]Charlize
    T-ogether
    E-veryone
    A-chieves
    M-ore


    One who asks a question is a fool for five minutes; one who does not ask a question remains a fool forever.

  17. #17
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    glad to have got it solved. can use it in my work also. thanks charlize. However, it can be looped till the filename is provided like so.

    Dim sFile As Variant
    TryAgain: sFile = Application.GetSaveAsFilename(InitialFileName:="", _
    fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls", Title:="Save workbook to..")
    If sFile <> False Then
    ThisWorkbook.SaveAs Filename:=sFile
    Else
    MsgBox ("Please give a name ..."), vbInformation
    Application.GoTo TryAgain
    End If

    didn't try it out coz i've got another macro running and don't want to interrupt it.

  18. #18
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Better do this instead of goto[vba]Sub file_to_save()
    Dim sFile As Variant
    Do While sFile = False
    sFile = Application.GetSaveAsFilename(InitialFileName:="", _
    fileFilter:="Microsoft Office Excel Workbook (*.xls), *.xls", Title:="Save workbook to..")
    If sFile <> False Then
    ActiveWorkbook.SaveAs Filename:=sFile
    Else
    MsgBox ("Please give a name ..."), vbInformation
    End If
    Loop
    End Sub[/vba]Charlize

    ps.: They told me that trying to avoid goto's helps to keep the code clean and readible. It helps to avoid 'spaghetti-coding' because you follow a logical order in the code and not a jumping scenario.

  19. #19
    VBAX Tutor lynnnow's Avatar
    Joined
    Jan 2005
    Location
    Mumbai, Maharashtra, India
    Posts
    299
    Location
    Won't a goto equate to a do while loop? just asking, pls assist

  20. #20
    VBAX Contributor moa's Avatar
    Joined
    Nov 2006
    Posts
    177
    Location
    Quote Originally Posted by Charlize
    ps.: They told me that trying to avoid goto's helps to keep the code clean and readible. It helps to avoid 'spaghetti-coding' because you follow a logical order in the code and not a jumping scenario.
    Glen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •