+ Reply to Thread
Results 1 to 8 of 8

Check for existing File name, open if exist or open new workbook if not

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Check for existing File name, open if exist or open new workbook if not

    Hi all,

    I need help again with my code. I have code but I can't get it to work, it just stops and I am hoping that another set of eyes will see what I missed. SIPPath is my variable for building the file path and Swb is my variable for the full file name. Both are defined as strings and ext is variable as string for file extension ".xlsm".

    I use this code to build the daily file path (SIPPath), if it does not exist to the file name, then build the daily filename (Swb) + extension (ext) as we need for daily file system. I need it to check for an existing file name and if it exists then open the file, if the file name does not exist then open the file with that days file name.

    All help is welcomed and appreciated.

    Private Sub Workbook_Open()
    
                    
             If Target.Cells.Count > 1 Then Exit Sub
                If Target.Value = "" Then Exit Sub
        
                On Error GoTo EndItAll:
                Application.EnableEvents = False
                
                'Working Code For Sign-In-Workbook Master 10 06 2017
                
                'Check for Year & Month folders Exists, if not Then create
                Dim SIPPath As String
                Dim Swb As String   'Swb = Daily Sign In Workbook File Name
                Dim ext As String  'ext = File name Extension for Excel Macro Enabled File
                ext = ".xlsm"
                
                'CHANGE DRIVE LETTER "E" TO "S" TO RUN ON DPS IMPOUND COMPUTERS
                SIPPath = "C:\Phoenix XXShared\XX\Sign In Logs\" & Format(Date, "yyyy") & "\"
        
                    If Dir(SIPPath, vbDirectory) = "" Then MkDir SIPPath
        
                        SIPPath = SIPPath & Format(Date, "MM") & " " & Format(Date, "YYYY") & "\"
        
                            If Dir(SIPPath, vbDirectory) = "" Then MkDir SIPPath
    
                            Swb = SIPPath & Format(Now, "MM") & "-" & Format(Now, "DD") & "-" & Format(Now, "YYYY") & " Sign-In-Workbook" & ext
     
                            If Dir(Swb, vbDirectory) <> "" Then Workbooks.Open (Swb)
    
                           Else
                            
                            'Saves File with Daily Log Name if not found
                            ActiveWorkbook.SaveAs Filename:=SIPPath & Format(Now, "MM") & "-" & Format(Now, "DD") _
                            & "-" & Format(Now, "YYYY") & " Sign-In-Workbook", FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
                            CreateBackup:=False
            
                        ActiveWorkbook.Save
            
                    If Worksheets("SIGN-IN").Range("B1").Value <= 0 Then Worksheets("SIGN-IN").Range("B1").Value = Date
                    If Worksheets("CREDIT_CARDS").Range("B4").Value <= 0 Then Worksheets("CREDIT_CARDS").Range("B4").Value = Date
                    If Worksheets("HP_DEPOSIT").Range("B12").Value <= 0 Then Worksheets("HP_DEPOSIT").Range("B12").Value = Date
                    If Worksheets("CAP_XX_DEPOSIT").Range("B12").Value <= 0 Then Worksheets("CAP_XX_DEPOSIT").Range("B12").Value = Date
                  
                  End If
                  
                    
                'This will Auto Save Active Workbook every 5 minutes
                Application.OnTime Now + TimeValue("00:05:00"), "SaveWb"
            
    End Sub
               
    EndItAll:
        Application.EnableEvents = True
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Check for existing File name, open if exist or open new workbook if not

    Why are you checking if Swb is a directory here?
    If Dir(Swb, vbDirectory) <> "" Then Workbooks.Open (Swb)
    Doesn't Swb hold the name of the workbook you want to check for the existence of?

    If you wanted to check that the file existed in a particular folder you would need the full path and filename, so something like this.
    If Dir(SIPPath & Swb)<> "" Then
        Workbooks.Open SIPPath & Swb
    Else
        ' code if the workbook doesn't exist.
    End If
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: Check for existing File name, open if exist or open new workbook if not

    Well we use this at work and several of us will have to use the log at different times during the day. The file is opened on each users computer. We use a master form for each day and Two of our users are NOT computer users at all. Several time they have had two workbooks open at the same time. I have a routine that will save the work book every 5 minutes and whenever closed. It has happened that they close the workbooks without saving and some how have closed workbooks with corrupted data saving some of the prior days information in the workbook dated for today. I have been trying to bullet proof the files by adding the auto save and save on close routines. I was trying to have the Master workbook check for the days file existing and if it is in the directory to automatically open it with no user input, if it doesn't exist then create it, as I think they are messing it up by answering the "file exists do you want to overwrite it?" warning. As I said I was trying to bullet proof the file because I know I can't fix the users. Any suggestions are very welcome.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Check for existing File name, open if exist or open new workbook if not

    The code I posted should check for the existence of the file named in the variable Swb in the folder/directory named in SIPPath.

  5. #5
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: Check for existing File name, open if exist or open new workbook if not

    Thanks for the quick response.
    I did not realize that I was using a directory check instead of a filename test. I am very new to VBA. Thanks again for the help.

    To clarify the SIPPath is just the path builder, after I have the path I built the File name that includes the SIPPath in the Swb Filename (Swb = SIPPath & Swb & ext). So Swb is the complete path with the file name (Swb = C:\Path\MM-DD-YYYY Sign In Log.xlsm).

    Thanks again for your help


    I put your code in and now I get a "Else without an If" error. I don't know why. Do you have any suggestion?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Check for existing File name, open if exist or open new workbook if not

    If Swb contains the full path and filename try removing vbDirectory from your code.
     If Dir(Swb, vbDirectory) <> "" Then Workbooks.Open (Swb)

  7. #7
    Forum Contributor
    Join Date
    06-06-2017
    Location
    Phoenix, AZ
    MS-Off Ver
    2013
    Posts
    129

    Re: Check for existing File name, open if exist or open new workbook if not

    Thanks for the help but, now it runs to the Else statement and stops with error "Else without If". I don't understand why it stops when there is an If two lines back. Any more ideas?

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Check for existing File name, open if exist or open new workbook if not

    This is a single line If and doesn't require End If and shouldn't be followed by an Else on the next line, if you wanted to add an Else it would be on the same line immediately after what you already have.
    If Dir(Swb, vbDirectory) <> "" Then Workbooks.Open (Swb)
    The first thing you should do is get rid of the Else, it's on it's own, unattached to an If statement.

    You should then look at changing all your single line If statements to multiline blocks.
    If condition 
        code if condition true
    Else
        code if condition false
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 18
    Last Post: 09-15-2017, 09:30 AM
  2. [SOLVED] Macro to open workbook with a suffix (need help to check if file is already open)
    By rossi_69 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2017, 11:03 AM
  3. open existing file if exist, else create it.
    By simeonmein in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-03-2014, 11:17 AM
  4. Replies: 3
    Last Post: 03-11-2006, 02:50 PM
  5. Replies: 0
    Last Post: 03-10-2006, 05:55 AM
  6. Replies: 1
    Last Post: 01-02-2006, 11:30 PM
  7. Replies: 0
    Last Post: 12-30-2005, 04:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1