Defining dynamic sheet names in a macro

LEG_END

Board Regular
Joined
Jan 8, 2017
Messages
65
Hi,

I am trying to copy several hundred sheets of data in separate workbooks into one single workbook and all on one sheet.

I have the code which will work if I define the specific name of the sheet to pull the information from in this case my sheets are names "Driving_001"... right through to 700 sometime more sometimes less. The dynamic part of my sheets is 001...002...003 and so on

Is there a way in this code to allow it to recognise the sheets as they are opened? This is the part I want to be dynamic:

Sheets("Driving_001").Range("A1:A5000").Copy

Code:
Sub Collaborate()

Set ws = Worksheets("Variables")

    FileType = "*.csv*" 'The file type to search for
    FilePath = ws.Range("A3") 'The folder to search
    OutputCol = 1 'The first row of the active sheet to start writing to

ThisWorkbook.ActiveSheet.Range(Cells(3, OutputCol), Cells(3, OutputCol)) = FilePath & FileType
OutputCol = OutputCol + 1
    
Curr_File = Dir(FilePath & FileType)

Do Until Curr_File = ""
 
Set FldrWkbk = Workbooks.Open(FilePath & Curr_File, False, True) 'Open new data file
 
    Sheets("Driving_").Range("A1:A5000").Copy 'Copy data from specific Range

 'Move back to Master file
    Workbooks("TMS_V0_2.xlsm").Activate
    Sheets(1).Cells(4, OutputCol).Select
    ActiveSheet.Paste
    OutputCol = OutputCol + 1
    FldrWkbk.Close SaveChanges:=False 'Close the data file
    Curr_File = Dir 'Select Next File
    
Loop

Set FldrWkbk = Nothing

End Sub

Thanks in advance

Izzy
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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