Hello
I hope somebody could help me with my conundrum. I have bolted together some online code below to copy data from multiple spreadsheets (in this case,3) in multiple workbooks to a single master worksheet in a new workbook, with the data from each source workbook being on a seperate line. The ranges in each workbook are;
Sheet 1 - A1:B1,D1:E1,G1
Sheet 2 - B2:C2,E2:F2,
Sheet 3 - A3,C3:D3,F3:G3
With the code below (starting Get_Data.....), I have tried to define the Source Range but it only picks up the reference A1:B1 when it transfers the data to the new master sheet. Could anybody help;
1) Ensure it picks up the correct range
2) Ensure the data from each worksheet in each workbook is on one line (no spaces) rather than on 3 rows
Any help would be most appreciated
Thanks
Plant
I hope somebody could help me with my conundrum. I have bolted together some online code below to copy data from multiple spreadsheets (in this case,3) in multiple workbooks to a single master worksheet in a new workbook, with the data from each source workbook being on a seperate line. The ranges in each workbook are;
Sheet 1 - A1:B1,D1:E1,G1
Sheet 2 - B2:C2,E2:F2,
Sheet 3 - A3,C3:D3,F3:G3
With the code below (starting Get_Data.....), I have tried to define the Source Range but it only picks up the reference A1:B1 when it transfers the data to the new master sheet. Could anybody help;
1) Ensure it picks up the correct range
2) Ensure the data from each worksheet in each workbook is on one line (no spaces) rather than on 3 rows
Any help would be most appreciated
Thanks
Plant
Code:
Private myFiles() As String
Private Fnum As Long
Sub RDB_Merge_Data_Browse4()
Dim myFiles As Variant
Dim myCountOfFiles As Long
Dim oApp As Object
Dim oFolder As Variant
Set oApp = CreateObject("Shell.Application")
Set oFolder = oApp.BrowseForFolder(0, "Select folder", 512)
If Not oFolder Is Nothing Then
' myCountOfFiles = Get_File_Names( _
' MyPath:=oFolder.Self.Path, _
' Subfolders:=True, _
' ExtStr:="*.xl*", _
' myReturnedFiles:=myFiles)
myCountOfFiles = Get_File_Names(MyPath:=oFolder.Self.Path, Subfolders:=True, ExtStr:="*.xl*", myReturnedFiles:=myFiles)
If myCountOfFiles = 0 Then
MsgBox "No files that match the ExtStr in this folder"
Exit Sub
End If
Get_Data _
FileNameInA:=True, _
PasteAsValues:=True, _
SourceShName:="all", _
SourceShIndex:=1, _
SourceRng:="A1:B1,D1:E1,G1,B2:C2,E2:F2,A3,C3:D3,F3:G3", _
StartCell:="", _
myReturnedFiles:=myFiles
End If
End Sub
Function Get_File_Names(MyPath As String, Subfolders As Boolean, _
ExtStr As String, myReturnedFiles As Variant) As Long
Dim Fso_Obj As Object, RootFolder As Object
Dim SubFolderInRoot As Object, file As Object
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'Create FileSystemObject object
Set Fso_Obj = CreateObject("Scripting.FileSystemObject")
Erase myFiles()
Fnum = 0
'Test if the folder exist and set RootFolder
If Fso_Obj.FolderExists(MyPath) = False Then
Exit Function
End If
Set RootFolder = Fso_Obj.GetFolder(MyPath)
'Fill the array(myFiles)with the list of Excel files in the folder(s)
'Loop through the files in the RootFolder
For Each file In RootFolder.Files
If LCase(file.Name) Like LCase(ExtStr) Then
Fnum = Fnum + 1
ReDim Preserve myFiles(1 To Fnum)
myFiles(Fnum) = MyPath & file.Name
End If
Next file
'Loop through the files in the Sub Folders if SubFolders = True
If Subfolders Then
Call ListFilesInSubfolders(OfFolder:=RootFolder, FileExt:=ExtStr)
End If
myReturnedFiles = myFiles
Get_File_Names = Fnum
End Function
Sub Get_Data(FileNameInA As Boolean, PasteAsValues As Boolean, SourceShName As String, _
SourceShIndex As Integer, SourceRng As String, StartCell As String, myReturnedFiles As Variant)
Dim SourceRcount As Long
Dim SourceRange As Range, destrange As Range
Dim mybook As Workbook, BaseWks As Worksheet
Dim rnum As Long, CalcMode As Long
Dim SourceSh As Variant
Dim sh As Worksheet
Dim I As Long
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet named "Combine Sheet"
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
BaseWks.Name = "Combine Sheet"
'Set start row for the Data
rnum = 1
'Check if we use a named sheet or the index
If SourceShName = "" Then
SourceSh = SourceShIndex
Else
SourceSh = SourceShName
End If
'Loop through all files in the array(myFiles)
For I = LBound(myReturnedFiles) To UBound(myReturnedFiles)
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(myReturnedFiles(I))
On Error GoTo 0
If Not mybook Is Nothing Then
If LCase(SourceShName) <> "all" Then
'Set SourceRange and check if it is a valid range
On Error Resume Next
If StartCell <> "" Then
With mybook.Sheets(SourceSh)
Set SourceRange = .Range(StartCell & ":" & RDB_Last(3, .Cells))
'Test if the row of the last cell >= then the row of the StartCell
If RDB_Last(1, .Cells) < .Range(StartCell).Row Then
Set SourceRange = Nothing
End If
End With
Else
With mybook.Sheets(SourceSh)
Set SourceRange = Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If
If Err.Number > 0 Then
Err.Clear
Set SourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If SourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set SourceRange = Nothing
End If
End If
On Error GoTo 0
If Not SourceRange Is Nothing Then
'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If
'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("B" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = myReturnedFiles(I)
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If
'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If
rnum = rnum + SourceRcount
End If
'Close the workbook without saving
mybook.Close savechanges:=False
Else
'Loop through all sheets in mybook
For Each sh In mybook.Worksheets
'Set SourceRange and check if it is a valid range
On Error Resume Next
If StartCell <> "" Then
With sh
Set SourceRange = .Range(StartCell & ":" & RDB_Last(3, .Cells))
If RDB_Last(1, .Cells) < .Range(StartCell).Row Then
Set SourceRange = Nothing
End If
End With
Else
With sh
Set SourceRange = Application.Intersect(.UsedRange, .Range(SourceRng))
End With
End If
If Err.Number > 0 Then
Err.Clear
Set SourceRange = Nothing
Else
'if SourceRange use almost all columns then skip this file
If SourceRange.Columns.Count > BaseWks.Columns.Count - 2 Then
Set SourceRange = Nothing
End If
End If
On Error GoTo 0
If Not SourceRange Is Nothing Then
'Check if there enough rows to paste the data
SourceRcount = SourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet to paste"
mybook.Close savechanges:=False
BaseWks.Parent.Close savechanges:=False
GoTo ExitTheSub
End If
'Set the destination cell
If FileNameInA = True Then
Set destrange = BaseWks.Range("C" & rnum)
With SourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = myReturnedFiles(I)
BaseWks.Cells(rnum, "B"). _
Resize(.Rows.Count).Value = sh.Name
End With
Else
Set destrange = BaseWks.Range("A" & rnum)
End If
'Copy/paste the data
If PasteAsValues = True Then
With SourceRange
Set destrange = destrange. _
Resize(.Rows.Count, .Columns.Count)
End With
destrange.Value = SourceRange.Value
Else
SourceRange.Copy destrange
End If
rnum = rnum + SourceRcount
End If
Next sh
'Close the workbook without saving
mybook.Close savechanges:=False
End If
End If
'Open the next workbook
Next I
'Set the column width in the new workbook
BaseWks.Columns.AutoFit
ExitTheSub:
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
End Sub
Sub ListFilesInSubfolders(OfFolder As Object, FileExt As String)
'Origenal SubFolder code from Chip Pearson
'http://www.cpearson.com/Excel/RecursionAndFSO.htm
'Changed by Ron de Bruin, 27-March-2008
Dim SubFolder As Object
Dim fileInSubfolder As Object
For Each SubFolder In OfFolder.Subfolders
ListFilesInSubfolders OfFolder:=SubFolder, FileExt:=FileExt
For Each fileInSubfolder In SubFolder.Files
If LCase(fileInSubfolder.Name) Like LCase(FileExt) Then
Fnum = Fnum + 1
ReDim Preserve myFiles(1 To Fnum)
myFiles(Fnum) = SubFolder & "\" & fileInSubfolder.Name
End If
Next fileInSubfolder
Next SubFolder
End Sub
Function RDB_Last(choice As Integer, rng As Range)
'Ron de Bruin, 5 May 2008
' 1 = last row
' 2 = last column
' 3 = last cell
Dim lrw As Long
Dim lcol As Integer
Select Case choice
Case 1:
On Error Resume Next
RDB_Last = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
Case 2:
On Error Resume Next
RDB_Last = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
Case 3:
On Error Resume Next
lrw = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
On Error Resume Next
lcol = rng.Find(What:="*", _
after:=rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
On Error Resume Next
RDB_Last = rng.Parent.Cells(lrw, lcol).Address(False, False)
If Err.Number > 0 Then
RDB_Last = rng.Cells(1).Address(False, False)
Err.Clear
End If
On Error GoTo 0
End Select
End Function