Consulting

Results 1 to 6 of 6

Thread: Solved: Error on "Save As"

  1. #1
    VBAX Regular
    Joined
    Mar 2007
    Posts
    9
    Location

    Solved: Error on "Save As"

    Hello,

    I have the following bit of code for saving a file with the name of a specific cell. This bit works fine, I have a problem when you select No or Cancel from the Saveas dialog box I get 1004 runtime error, telling me Method 'SaveAs' of object'_Workbook' failed

    [VBA]
    Sub GenSave()

    Application.ScreenUpdating = False
    ActiveSheet.Unprotect
    Application.Goto "PICName"
    ActiveWorkbook.SaveAs Filename:=ActiveCell.Value, FileFormat _
    :=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:= _
    False, CreateBackup:=False
    Application.Range("Mth").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

    Can someone suggest how I can prevent this but retaining the ability of saving with name of the file being a cell contents (in this case "PICName")

    Thankyou in advance

    Chris


    Edited 30-Mar-07 by geekgirlau. Reason: insert vba tags

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Easiest would be adding
    On Error Resume Next

    onto your procedure.
    Just put the line right after SUB

  3. #3
    VBAX Regular
    Joined
    Mar 2007
    Posts
    9
    Location
    Thankyou... that worked great..

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub GenSave()
    Dim sFile As String

    Application.ScreenUpdating = False
    ActiveSheet.Unprotect
    Application.Goto "PICName"
    sFile = Application.GetSaveAsFilename(initialfilename:=ActiveCell.Value)
    If sFile <> "False" Then
    ActiveWorkbook.SaveAs Filename:=sFile, _
    FileFormat:=xlNormal, _
    Password:="", _
    WriteResPassword:="", _
    ReadOnlyRecommended:=False, _
    CreateBackup:=False
    End If
    Application.Range("Mth").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    Application.ScreenUpdating = True

    End Sub
    [/vba]

  5. #5
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    initialfilename --- you don't see that everyday! damn!

  6. #6
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Welcome to the board ChrisJ (not to mention david000!)

    Chris, I've taken the liberty of renaming your thread. Even though you have a solution now, it may help other members in the future find a solution to a similar problem. Generally speaking it's a good idea to use a subject for your thread that gives some indication of the topic, rather than a generic "help" title.

    I've also enclosed your text in VBA tags. Whenever you post code, select the code text and click on the "VBA" button - this formats your code as you see above, making it easier to read.

    Hope to hear a lot more from you both in future!

Posting Permissions

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