+ Reply to Thread
Results 1 to 3 of 3

Altering Code to account for WB opened multiple times

Hybrid View

  1. #1
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    Altering Code to account for WB opened multiple times

    Hey,

    I have another issue that I'm hoping someone here can solve without too much effort. I had an earlier problem where I needed to move sheets from a workbook with the naming convention of "DDMMYY.xls" I solved that problem thanks to shg with the following code:
    Sub Date2()
    
    Dim wkb As Workbook
        
        Set wkb = MostRecentWkb(10)
        If wkb Is Nothing Then
            MsgBox "The macro is not finding a Failed Trade Report open."
        Else
            wkb.Activate
        End If
    End Sub
    
    Function MostRecentWkb(Optional nDays As Long = 1) As Workbook
        Dim iDays As Long
        Dim sWkb As String
        
        For iDays = 0 To Abs(nDays)
            sWkb = WorksheetFunction.Text(Date - iDays, "mmddyy") & ".xls"
            If WorkbookIsOpen(sWkb) Then
                Set MostRecentWkb = Workbooks(sWkb)
                Exit For
            End If
        Next iDays
    End Function
    
    Function WorkbookIsOpen(sWkb As String) As Boolean
        On Error Resume Next
        WorkbookIsOpen = Not Workbooks(sWkb) Is Nothing
        Err.Clear
    End Function
    The issue I need help with is if the sheet has been opened multiple times, the naming convention of the file is changed to "DDMMYY(n).xls" Note the integer 'n' - I would like to somehow account for that.

    So, a file typically named "050509.xls" opens fine and works in my macro perfectly, but if a file is named "050509(2).xls" or "050509(6).xls", the macro is not locating the file. Does anyone know any quick fixes to my issue? It is not a huge deal, if no answer is found, I can provide a disclaimer to users explaining the problem.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Altering Code to account for WB opened multiple times

    Where is the (n) coming from?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    Re: Altering Code to account for WB opened multiple times

    The (n) comes from closing and re-opening excel workbooks. So for example, the spreadsheet I use named "DDMMYY.xls" comes up normally the first time it is opened. But if the same spreadsheet is closed and re-opened multiple times without re-booting the computer, it begins to get a variable (n) depending on how many times it has been opened.

    I think it has something to do with the spreadsheet being opened from an email. I get a daily email with this spreadsheet and open from outlook. Perhaps if it was saved first to the desktop it wouldn't get the (n), but I was looking for a way to continue opening from outlook.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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