Results 1 to 8 of 8

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

Threaded 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

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