If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Problem with macro code in excel

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-17-09, 02:26
verse.thekeg verse.thekeg is offline
Registered User
 
Join Date: Feb 2009
Posts: 1
Problem with macro code in excel

Hi, I have this code that will save the current workbook, I want to put in place an error check so if the user put's in nothing, or hits cancel (which outputs as 'false.xls) it forces msgbox stating to put in a real file name ... my current code looks like this


Code:
' Save Sheet
Loop_Start:
    Dim NewName As String
    NewName = Application.GetSaveAsFilename
    ThisWorkbook.SaveAs Filename:=NewName & ".xls"
        If NewName = "False" Then GoTo ErrorMessage

        
ErrorMessage: MsgBox ("You need to specify a Filename when saving this order!")
        GoTo Loop_Start
It's not working in the sense that it doesn't check to see if the field is true to the statement. Thanks in advance for your help
Reply With Quote
  #2 (permalink)  
Old 02-17-09, 08:42
MikeTheBike MikeTheBike is offline
Registered User
 
Join Date: Apr 2004
Location: Derbyshire, UK
Posts: 714
Quote:
Originally Posted by verse.thekeg
Hi, I have this code that will save the current workbook, I want to put in place an error check so if the user put's in nothing, or hits cancel (which outputs as 'false.xls) it forces msgbox stating to put in a real file name ... my current code looks like this


Code:
' Save Sheet
Loop_Start:
    Dim NewName As String
    NewName = Application.GetSaveAsFilename
    ThisWorkbook.SaveAs Filename:=NewName & ".xls"
        If NewName = "False" Then GoTo ErrorMessage

        
ErrorMessage: MsgBox ("You need to specify a Filename when saving this order!")
        GoTo Loop_Start
It's not working in the sense that it doesn't check to see if the field is true to the statement. Thanks in advance for your help
Hi

The following code, I believe, does what you have requested
Code:
Sub SaveFile()
    Dim NewName As String
    Dim FileSaved As Boolean
    
    FileSaved = False
    
    Do Until FileSaved
        NewName = Application.GetSaveAsFilename(fileFilter:="Excel Files (*.xls), *.xls")
        If NewName <> "False" Then
            If Dir(NewName) = "" Then
                ThisWorkbook.SaveAs Filename:=NewName
                FileSaved = True
            Else
                If MsgBox("File exists. Do you want to overwrite existing file?", vbQuestion + vbYesNo, "Save File") = vbYes Then
                    Application.DisplayAlerts = False
                    ThisWorkbook.SaveAs Filename:=NewName
                    Application.DisplayAlerts = True
                    FileSaved = True
                End If
            End If
        Else
            MsgBox "You need to specify a Filename when saving this order!"
        End If
    Loop
End Sub
However, once the sub is entered you cannot leave it without saving the file (except with Ctl+Break!), which in my experience is never a good idea, and can be very frustrating.


MTB
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On