Consulting

Results 1 to 10 of 10

Thread: Finding file name by numerical order

  1. #1
    VBAX Regular
    Joined
    Nov 2011
    Posts
    7
    Location

    Finding file name by numerical order

    Hi all

    I have an excel sheet that will create a standard filing system.

    in a folder there are folders numbered like this:

    "ENQ 0123, ENQ 0124" and go up 1 digit at a time.

    there are hundereds in this folder.
    I would just like to know if there is a quick way of finding the latest one so i can then tell my code to generate the next one.

  2. #2
    VBAX Regular
    Joined
    Nov 2011
    Posts
    7
    Location
    I have cross posted but cant post links yet

  3. #3
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  4. #4
    VBAX Regular
    Joined
    Nov 2011
    Posts
    7
    Location
    cheers.

    its looking like im going to have to use a loop if i want to do this.

    i wasnt keen on this because of the potential runtime.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Adapt the foldername 'G:\OF\'

    [VBA]Sub laatst_aangemaakte_map_snb()
    MsgBox Split(CreateObject("wscript.shell").exec("cmd /c Dir G:\OF\*. /b /o-d").stdout.readall, vbCrLf)(0)
    End Sub[/VBA]

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    If the filename pattern is consistent, maybe:
    [VBA]Option Explicit

    Sub example()
    MsgBox NextNumber(ThisWorkbook.Path)
    End Sub

    Function NextNumber(Path As String) As Long
    Dim File As Object
    Dim lTmp As Long

    With CreateObject("Scripting.FileSystemObject").GetFolder(ThisWorkbook.Path)
    For Each File In .Files
    If Mid(File.Name, InStrRev(File.Name, ".")) Like ".xls*" _
    And Left(File.Name, InStrRev(File.Name, ".") - 1) Like "ENQ ####" Then
    lTmp = CLng(Split(Left(File.Name, InStrRev(File.Name, ".") - 1), Chr(32))(1))
    If lTmp > NextNumber Then NextNumber = lTmp
    End If
    Next
    End With
    End Function[/VBA]
    Hope that helps,

    Mark

  7. #7
    VBAX Regular
    Joined
    Nov 2011
    Posts
    7
    Location
    this look promicing mark

    however this seems to be looking at files im talking folders.
    is there a way i could adapt it?

  8. #8
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  9. #9
    VBAX Regular
    Joined
    Nov 2011
    Posts
    7
    Location
    thanks ill have a look into that.

    looks like its somthing totaly new to me.

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by FRIEL
    ...in a folder there are folders numbered like this:

    "ENQ 0123, ENQ 0124" and go up 1 digit at a time...
    ACK! Sorry about that, and thank you for being so gracious in your correction. Your question was clear, my eyes were not

    Maybe:
    [VBA]Sub example()
    MsgBox NextFolderNumber(ThisWorkbook.Path)
    End Sub

    Function NextFolderNumber(Path As String) As Long
    Dim Folder As Object
    Dim lTmp As Long

    With CreateObject("Scripting.FileSystemObject")
    If .FolderExists(Path) Then
    For Each Folder In .GetFolder(Path).SubFolders
    If Folder.Name Like "ENQ ####" Then
    lTmp = CLng(Right(Folder.Name, 4))
    If lTmp > NextFolderNumber Then NextFolderNumber = lTmp
    End If
    Next
    End If
    End With
    End Function[/VBA]

Posting Permissions

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