Hi all, me again!
Todays task for me is to try and open a folder which contains loads of various excel files, then open each file and copy the contents of sheet 1 exculding the headers (and offset function??).
Once It has copied the data then to paste it into a "master file" but to find the next free row and paste the data. I have had a look on the internet and found this code which does a great job of opening every single workbook in the file path. But Im stuck on how to select the data contained with in sheet 1 and copy the cells, stupid i know...
(Armed with my new VBA book I would like to crack this by the end of the day) :D
Public objFSO As Object
Sub GetContentsOfA1()
Dim aFileArray() As String
Dim x As Long, y As Long
Dim wrkBk As Workbook
Set objFSO = CreateObject("Scripting.FileSystemObject")
ReadDriveContents aFileArray, "H:\Macro\2013-07"
Set objFSO = Nothing
y = 1
For x = LBound(aFileArray) To UBound(aFileArray)
Set wrkBk = Workbooks.Open(aFileArray(x))
ThisWorkbook.Worksheets("Sheet1").Cells(y, 1).Value = wrkBk.Worksheets(1).Cells(1, 1)
wrkBk.Close SaveChanges:=False
y = y + 1
Next x
End Sub
Sub ReadDriveContents(ByRef aFileArray, ByVal DrivePath)
'// Returns an Array with All files and Folders
Dim objFolder As Object
Dim objFile As Variant
Dim colFiles As Variant
Dim i: i = 0
'// Read Parent Folder
Set objFolder = objFSO.Getfolder(DrivePath)
Set colFiles = objFolder.Files
For Each objFile In colFiles
If objFile Like "*.xls" Or _
objFile Like "*.xlsx" Or _
objFile Like "*.xlsm" Then
ReDim Preserve aFileArray(i)
aFileArray(UBound(aFileArray)) = objFile
i = i + 1
End If
Next
End Sub
Todays task for me is to try and open a folder which contains loads of various excel files, then open each file and copy the contents of sheet 1 exculding the headers (and offset function??).
Once It has copied the data then to paste it into a "master file" but to find the next free row and paste the data. I have had a look on the internet and found this code which does a great job of opening every single workbook in the file path. But Im stuck on how to select the data contained with in sheet 1 and copy the cells, stupid i know...
(Armed with my new VBA book I would like to crack this by the end of the day) :D
Public objFSO As Object
Sub GetContentsOfA1()
Dim aFileArray() As String
Dim x As Long, y As Long
Dim wrkBk As Workbook
Set objFSO = CreateObject("Scripting.FileSystemObject")
ReadDriveContents aFileArray, "H:\Macro\2013-07"
Set objFSO = Nothing
y = 1
For x = LBound(aFileArray) To UBound(aFileArray)
Set wrkBk = Workbooks.Open(aFileArray(x))
ThisWorkbook.Worksheets("Sheet1").Cells(y, 1).Value = wrkBk.Worksheets(1).Cells(1, 1)
wrkBk.Close SaveChanges:=False
y = y + 1
Next x
End Sub
Sub ReadDriveContents(ByRef aFileArray, ByVal DrivePath)
'// Returns an Array with All files and Folders
Dim objFolder As Object
Dim objFile As Variant
Dim colFiles As Variant
Dim i: i = 0
'// Read Parent Folder
Set objFolder = objFSO.Getfolder(DrivePath)
Set colFiles = objFolder.Files
For Each objFile In colFiles
If objFile Like "*.xls" Or _
objFile Like "*.xlsx" Or _
objFile Like "*.xlsm" Then
ReDim Preserve aFileArray(i)
aFileArray(UBound(aFileArray)) = objFile
i = i + 1
End If
Next
End Sub