Option Explicit
' 12_500 07-02-2017#01.xls
' 12_500 07-02-2017.xls
Sub example01()
Const FOLDER_NAME = "\New folder\"
Dim FSO As Object ' Scripting.FileSystemObject
Dim fsoFile As Object ' Scripting.File
Dim fsoFolder As Object ' Scripting.Folder
Dim REX As Object ' VBScript_RegExp_55.RegExp
Dim wbSource As Workbook
Dim wbDestination As Workbook
Dim wks As Worksheet
Dim arrNames() As String
Dim n As Long
Dim i As Long
' Create references to new instances of the FileSystemObject Object and Rex Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set REX = CreateObject("VBScript.RegExp")
' Ensure the named folder is a sub-folder in the folder ThisWorkbook resides in...
On Error Resume Next
Set fsoFolder = FSO.GetFolder(ThisWorkbook.Path & FOLDER_NAME)
On Error GoTo 0
' ...or bail out!
If fsoFolder Is Nothing Then
MsgBox "Bad folder"
Exit Sub
End If
With REX
' The pattern will be aimed at confirming each individual filename like: '12_500 07-02-2017.xls' so Global is False as we want to test
' for a match against the entire filename
.Global = False
' In case someone saves a file as '12_500 07-02-2017.XLS' for example
.IgnoreCase = True
' This pattern matches 2-digits, followed by (FB) 1 to 2 spaces, FB 2-digits, FB a hyphen, FB 2-digits, FB a hyphen, FB 4-digits, FB '.xls'.
' As I did not insist on the match requiring start and end string markers (which I probably should have), this means that the actual match
' for '12_500 07-02-2017.xls' would be '00 07-02-2017.xls' (OOPS on my part)
'.Pattern = "[0-9]{2}\ {1,2}[0-9]{2}\-[0-9]{2}\-[0-9]{4}\.xls"
' Sooo... the updated pattern includes looking for the underscore and the '500' and would match '12_500 07-02-2017.xls'
'BUT... this would also match 'ACK 12_500 07-02-2017.xls' , so here's better yet I believe, as we'll include the start/end string markers
.Pattern = "^[0-9]{2}\_[0-9]{3}\ {1,2}[0-9]{2}\-[0-9]{2}\-[0-9]{4}\.xls$"
End With
' Oversize an array simply based on the max number of possibly matching filenames, that is, the number of files in the subfolder
ReDim arrNames(1 To fsoFolder.Files.Count)
' Then populate the array and track how many matches we found
For Each fsoFile In fsoFolder.Files
If REX.Test(fsoFile.Name) Then
n = n + 1
arrNames(n) = fsoFile.Name
End If
Next
' Then trim the array
ReDim Preserve arrNames(1 To n)
'The rest is self explanatory I believe, but please do not hesitate to ask if unsure.
Application.EnableEvents = False
For n = 1 To UBound(arrNames)
If FSO.FileExists(ThisWorkbook.Path & FOLDER_NAME & Left$(arrNames(n), Len(arrNames(n)) - 4) & "#01.xls") Then
Set wbSource = Workbooks.Open(ThisWorkbook.Path & FOLDER_NAME & Left$(arrNames(n), Len(arrNames(n)) - 4) & "#01.xls", , True)
Set wbDestination = Workbooks.Open(ThisWorkbook.Path & FOLDER_NAME & arrNames(n))
'Tack in a sheet so we can move all pre-existing sheets
wbSource.Worksheets.Add wbSource.Worksheets(1)
Do While wbSource.Worksheets.Count >= 2
wbSource.Worksheets(2).Move After:=wbDestination.Worksheets(wbDestination.Worksheets.Count)
Loop
wbSource.Close False
Kill ThisWorkbook.Path & FOLDER_NAME & Left$(arrNames(n), Len(arrNames(n)) - 4) & "#01.xls"
DoEvents
wbDestination.Close True
End If
Next
Application.EnableEvents = True
End Sub