VBA find matching Excel files with a subtext - and merge them into single new file

41 Views Asked by At

I have excel files with suffixes like this. Final_38200_1.xls, _2.xls, _3.xls etc. I also have files with a _0 suffix.

The _0 suffix files are for purposes of this question done. don't need touched.

For the suffixes greater than 0 - with matching prefix, these files need appended together into a new _0 file. they are parts of a whole. I need the data all on the same sheet.

There is a rate code column, which I update based on the suffix number currently. After all the updating - i then need to final_38200_1 final_38200_2 files into a new final_38200_0 file.

here's the code i use to modify rate code based on suffix.

[Sub Subshed_Rates()

Dim ss As String
Dim s As String

Dim name As String

Dim fso As New Scripting.FileSystemObject
name = fso.GetBaseName(ActiveWorkbook.name)

'Debug.Print (name)


s = Right(name, 1)

If s > 0 Then
ss = "S" & s
ElseIf s = 0 Then Exit Sub
End If

Range("E1:E40000").Select

Selection.Replace What:="*AG*", Replacement:=ss & " AG", 
LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, 
SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="*RES*", Replacement:=ss & " RES", 
LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, 
SearchFormat:=False, _
    ReplaceFormat:=False
Selection.Replace What:="*COM*", Replacement:=ss & " COMM", 
LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, 
SearchFormat:=False, _
    ReplaceFormat:=False

End Sub]

After that - I need to amend _1 and _2 to a new _0 file. how do i do that?

0

There are 0 best solutions below