View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
BEEJAY BEEJAY is offline
external usenet poster
 
Posts: 247
Default How to determine How many files in specified directory

The MASTER (Destination) WB will have only ONE WS.
Each Source WB has only ONE WS
Each Source WS in each WB has the same number of Columns
Each Source WS in each WB has Different number of rows.
Each Source WB will be named by number, in order, from
1.xls thru 12.xls - EXPECT that usually will have 4 or 5 Source WB's
All (ONLY) WB's for this process will be in C:\DATA

How can the number of WB's be determined automatically, and then
how can the required processes be activated for only that number of WB'sfiles.

Sub Combine_M2M_Extracts()

' M2M-Master.xlt file has been opened
' Shows as M2M-Master1.xls - Save M2M-Master1.xls as MASTER.xls

ActiveWorkbook.SaveAs Filename:="C:\DATA\MASTER.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False _
, CreateBackup:=False

' Determine how many WBs are available in C:\Data\
' Wb's must be appended into MASTER in Order - Low File Number to High

' If only ONE file in Directory, Then exit Sub
' If only TWO or More Files in Directory,
' Run AddHeading, then Run Combine_ALL
' If THREE or MORE, Run AddHeading, then Run Combine_ALL
' AND Loop Combine_ALL, as required

End Sub

Sub ADDHEADING()
' Open/Activate # 1 File and Select Row 1 ONLY (Header Row)
Workbooks.Open Filename:="C:\DATA\1.xls"
Windows("1.xls").Activate
Rows("1:1").Select
Selection.Copy
' Activate Destination File, and Insert Rows
Windows("Master.xls").Activate
Rows("1:1").Select
End Sub

Sub Combine_ALL()
' Open/Activate # 1 File and Select Used Range
' Start at Row 2 - The header row is not needed again
Workbooks.Open Filename:="C:\DATA\1.xls"
Windows("1.xls").Activate
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

' Activate Destination File, and Insert Rows
Windows("Master.xls").Activate
Range("A1").Select
Selection.End(xlDown).Offset(1, 0).Select
Selection.Insert Shift:=xlDown

' Activate #1 File and Close
Windows("1.xls").Activate
Application.DisplayAlerts = False
ActiveWindow.Close
Application.DisplayAlerts = True
End Sub

' At this point it should loop thru all available Source Files