ASKER
ASKER
Set rRng = .Range("A1").CurrentRegion @@///change range here
3.You would set the specific sheet if the workbooks contain more than 1 sheet, all the sheets would ideally be the same, i.e. all Sheet 1 or the same position in the workbook. Here's 3 optionsWith ThisWorkbook.Worksheets(1) ''/// the first Tab
With ThisWorkbook.Worksheets("Data") ''/// all sheets named Data
With ThisWorkbookSheet1 ''/// uses the sheet's Codename
ASKER
ASKER
Option Explicit
'---------------------------------------------------------------------------------------
' Module : Data
' Author : Roy Cox (royUK)
' Website : for more examples and Excel Consulting
' Date : 19/11/2011
' Purpose : Combine data from several workbooks
' Disclaimer: Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
Sub CombineData()
Dim oWbk As Workbook
Dim rRng As Range
Dim rToCopy As Range
Dim rNextCl As Range
Dim lCount As Long
Dim bHeaders As Boolean
Dim sFil As String
Dim sPath As String
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
' On Error GoTo exithandler
' assumes workbooks are in a sub folder named "Data"
sPath = "P:/test2/pydev/"
ChDir sPath
sFil = Dir("*.xl**") 'file type
Do While sFil <> "" 'will start LOOP until all files in folder sPath have been looped through
With ThisWorkbook.Worksheets("Sheet1")
Set rRng = .Range("A1").CurrentRegion ''///change range here
If rRng.Cells.Count = 0 Then
'no data in master sheet
bHeaders = False
Else: bHeaders = True
End If
Set oWbk = Workbooks.Open(sPath & Application.PathSeparator & sFil) 'opens the file
'A1 must be within the data, if not amend the Range below
Set rToCopy = oWbk.Sheet1.Range("B2,J2,S2")
If Not bHeaders Then
Set rNextCl = .Cells(1, 1)
bHeaders = True
Else: Set rNextCl = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
End If
''/// add file name
rNextCl.Value = oWbk.Name
''/// copy cells next to file name
rToCopy.Copy rNextCl.Offset(, 1)
End With
oWbk.Close False 'close source workbook
sFil = Dir
Loop ' End of LOOP
exithandler:
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
ASKER
ThisWorkbook.Path & Application.PathSeparator & "Data"
ASKER
ASKER
ASKER
Option Explicit
'---------------------------------------------------------------------------------------
' Module : Data
' Author : Roy Cox (royUK)
' Website : for more examples and Excel Consulting
' Date : 19/11/2011
' Purpose : Combine data from several workbooks
' Disclaimer: Disclaimer; This code is offered as is with no guarantees. You may use it in your
' projects but please leave this header intact.
'---------------------------------------------------------------------------------------
Sub CombineData()
Dim oWbk As Workbook
Dim rRng As Range, rToCopy As Range, rNextCl As Range
Dim bHeaders As Boolean
Dim sFil As String, sPath As String
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
' On Error GoTo exithandler
' assumes workbooks are in a sub folder named "Data"
sPath = "P:\\test2\pydev\data"
ChDir sPath
sFil = Dir("*.xl**") 'file type
Do While sFil <> "" 'will start LOOP until all files in folder sPath have been looped through
With ThisWorkbook.Worksheets("Sheet1")
Set rRng = .Range("b2,j2,s2") ''///change range here
If rRng.Cells.Count = 0 Then
'no data in master sheet
bHeaders = False
Else: bHeaders = True
End If
Set oWbk = Workbooks.Open(sPath & Application.PathSeparator & sFil) 'opens the file
'A1 must be within the data, if not amend the Range below
'Set rToCopy = oWbk.Sheet1.Range("B2,J2,S2")
If Not bHeaders Then
Set rNextCl = .Cells(1, 1)
bHeaders = True
Else: Set rNextCl = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
End If
''/// add file name
rNextCl.Value = oWbk.Name
''/// copy cells next to file name
rToCopy.Copy rNextCl.Offset(, 1)
End With
oWbk.Close False 'close source workbook
sFil = Dir
Loop ' End of LOOP
exithandler:
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
ASKER
ASKER
ASKER
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
Open in new window