+ Reply to Thread
Results 1 to 7 of 7

VBA code for opening a file with a variable version number in the file name

Hybrid View

  1. #1
    Registered User
    Join Date
    02-10-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    24

    VBA code for opening a file with a variable version number in the file name

    I have a file that I save with a new version number each time I make major changes. The file name currently is: "Telephony Equipment Inventory v26 (Summary).xlsm". The "26" is the variable number. Can someone give me the vba code to ensure I open the file with the highest version number?

    Trish

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA code for opening a file with a variable version number in the file name

    Hi Trish,

    Try this:

    Sub HighFile(): Dim P As String, U As String, Latest As String
    Dim i As Integer, j As Integer, N As Integer, O As Integer
    ' "Telephony Equipment Inventory v26 (Summary).xlsm"
    P = ActiveWorkbook.Path 'OR put actual pathname Here
                                     U = Dir(P)
    GetLatest: i = InStr(1, U, "Telephony Equipment Inventory v")
    If i Then
    i = InStr(1, U, "v"): j = InStr(i + 1, U, " "): N = Val(Mid(U, i+1, j - i))
    If N > O Then
    O = N: Latest = U
    End If: End If
    U = Dir()
    If U <> "" Then GoTo GetLatest
    Workbooks.Open FileName:=P & "\" & Latest
    End Sub
    Last edited by xladept; 01-30-2014 at 02:17 AM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    02-10-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: VBA code for opening a file with a variable version number in the file name

    xladept, this failed at the "U = Dir()" line of code. I liked this because it was short, but if this won't work, I will give the longer code a try.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA code for opening a file with a variable version number in the file name

    Hi Trish,

    Try the following written and tested using Excel 2003:
    Sub MainProgram()
    
      Dim sPath As String
      Dim sFileName As String
      
      'Generate the file path (it is assumed that all the files are in the same folder)
      'as the Excel file that is running this code
      'Change this as required for your specific needs
      sPath = ThisWorkbook.Path & "\"
    
      sFileName = FindHighestNumberedSoftwareVersion(sPath)
      If Len(sFileName) = 0 Then
        MsgBox "No Matching File was found"
        Exit Sub
      Else
        MsgBox "The highest version file is '" & sFileName & "'."
      End If
    
      'Your code here
    
    End Sub
    
    
    
    Public Function FindHighestNumberedSoftwareVersion(sPath As String) As String
      'This finds the highest numbered software version of a file.
      'It is assumed that all files reside in the same folder
      '
      'The file name with the highest version number is returned
      'If there is no match, then the NULL string "" is returned.
      '
      
      'It is assumed that the version numbers go from '01' to '99'
      '
      'I personally prefer 3 or 4 digit version numbers e.g. '0001' to '9999'.
      
      'Question marks mean match any character in that position
      Const sMasterFileName = "Telephony Equipment Inventory v* (Summary).xlsm"   'This works for 1 or 2 digit file versions
      Const sVersionMASK = "v* (Summary).xlsm"
      
      
      Dim iVersionStartPosition As Integer
      Dim iVersion As Integer
      Dim iVersionMax As Integer
      
      Dim s As String
      Dim sHighestVersionFileName As String
      Dim sOldVersion As String
      Dim sSearchSpec As String
      
      
      'Obtain the version start position in the file name
      'Both of the following lines do the same thing
      'The 2nd line is included so you will understand better what is going on
      iVersionStartPosition = InStr(sMasterFileName, sVersionMASK) + 1
      iVersionStartPosition = InStr(sMasterFileName, "v* (Summary).xlsm") + 1
    
      
      'Generate a search specification for the file
      'Both of the following lines do the same thing
      'The 2nd line is included so you will understand better what is going on
      sSearchSpec = sPath & sMasterFileName
      sSearchSpec = sPath & "Telephony Equipment Inventory v* (Summary).xlsm"
      
      'Loop through all the files that match the file specification
      's is the name of the next matching file (without the folder name)
      sHighestVersionFileName = ""
      s = Dir(sSearchSpec)
      Do While s <> vbNullString
        'Debug.Print s
        
        'Extract the old version of the file name as text (two characters in length)
        'The version number is two characters long (either '12' or ' 2')
        sOldVersion = Mid(s, iVersionStartPosition, 2)
          
        'Convert the text version number to numeric if possible
        If IsNumeric(sOldVersion) = False Then
          MsgBox "Bad file Version Number found - processing will continue." & vbCrLf & _
                 "Folder: " & sPath & vbCrLf & _
                 "File: " & s
        Else
          'We have a match - save the version number if it is greater than the previous max value
          iVersion = CInt(sOldVersion)
          If iVersion > iVersionMax Then
            iVersionMax = iVersion
            sHighestVersionFileName = s
          End If
        End If
          
        s = Dir 'Find the next matching file
      Loop
      
      'Return the highest version file name
      FindHighestNumberedSoftwareVersion = sHighestVersionFileName
      
    End Function
    Lewis

  5. #5
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA code for opening a file with a variable version number in the file name

    Hi Trish,

    I omitted the backslash

    Sub HighFile(): Dim P As String, U As String, Latest As String
    Dim i As Integer, j As Integer, N As Integer, O As Integer
    ' "Telephony Equipment Inventory v26 (Summary).xlsm"
    P = ActiveWorkbook.Path & "\" 'OR put actual pathname Here
                                     U = Dir(P)
    GetLatest: i = InStr(1, U, "Telephony Equipment Inventory v")
    If i Then
    i = InStr(1, U, "v"): j = InStr(i + 1, U, " "): N = Val(Mid(U, i+1, j - i))
    If N > O Then
    O = N: Latest = U
    End If: End If
    U = Dir()
    If U <> "" Then GoTo GetLatest
    Workbooks.Open FileName:=P & "\" & Latest
    End Sub

  6. #6
    Registered User
    Join Date
    02-10-2012
    Location
    Ottawa, Canada
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: VBA code for opening a file with a variable version number in the file name

    I was unable to get your code to work xladept, so I used Lewis' code. Worked perfectly. Thank you, this will come in very handy again.

    Trish

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA code for opening a file with a variable version number in the file name

    Sorry Trish

+ 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. [SOLVED] Error in code - play music file automatically after opening file
    By mukeshbaviskar in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-15-2013, 10:06 AM
  2. [SOLVED] Code for opening file if match with file name is found
    By cokillerliu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-25-2013, 08:41 PM
  3. Replies: 22
    Last Post: 12-11-2012, 12:43 PM
  4. Replies: 0
    Last Post: 08-03-2009, 01:11 PM
  5. [SOLVED] Opening a file with code without a set file name
    By jenkinspat in forum Excel General
    Replies: 1
    Last Post: 03-04-2005, 07:06 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