Automate combining three workbooks (separate worksheets) using part of file name as guide

Clovisjr

New Member
Joined
Aug 22, 2018
Messages
1
I have folder of Excel workbooks that have names like this:
ArizonaResidential.xlsx
ArizonaNonResendential.xlsx
ArizonaAll.xlsx
MaineResidential.xlsx
MaineNonResidential.xlsx
MaineAll.xlsx
etc.

They each have one worksheet. The worksheets are different formats. I want to end up with one workbook for each state with each of the three types of worksheets in it (residential, nonresidential, and all).

is it possible to use VBA to look for the common string/token in the file name and combine each trio into one workbook? I’ve googled lots but I am just finding examples where the same sheets, same format are either merged into one sheet or other variations with all worksheets of one type. I’m open to utilities or other approaches as well.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
create three folders named ALL, RES and NR and use the following codes
Sub list_files()
Dim a As Long
Dim f As String, b As String
Cells(1, 1) = "=cell(""filename"")"
Cells(1, 2) = "=left(A1,find(""["",A1)-1)"
Cells(1, 3) = Cells(1, 2) & "ALL"
Cells(1, 4) = Cells(1, 2) & "RES"
Cells(1, 5) = Cells(1, 2) & "NR"


For a = 1 To 1
b = Choose(a, "*.xls*")
Cells(2, a) = b
Cells(3, a).Select
f = Dir(Cells(1, 2) & b)
Do While Len(f) > 0
For x = 1 To 3
y = Choose(x, "ALL", "residential", "non")
If InStr(f, y) > 0 Then
FileCopy Cells(1, 2) & f, Cells(1, x + 2) & f
End If
ActiveCell.Formula = f
ActiveCell.Offset(1, 0).Select
f = Dir()
Next x
Loop
Next a
MsgBox "Listing is complete"
End Sub
when you run the macro, it sorts files into respective folders and copies it. you may join them manually or programmatically
Ravi shankar
 
Upvote 0

Forum statistics

Threads
1,213,553
Messages
6,114,279
Members
448,562
Latest member
Flashbond

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top