Blue_Bunny
05-11-2011, 02:02 PM
Hi All -
I am somewhat embarrassed that I am "crying uncle" on this one ...
I have conducted quite a bit of research and wasted the better part of the day trying to tinker with solutions, but now I am throwing in the towel.
Quick summary:
I want to merge data from multiple files into a single workbook.
Here are the details:
1.) I would like to use one of the MSOFileDialog Objects to prompt the user to select files from a folder. My goal is to place this code into an add-in that the user will use to kick off this Sub. We can refer to this as Workbook 1.
2.) I only want to process the first worksheet from each file. I was thinking of using worksheets(1) versus worksheets("filename_with_a_Variable").
3.) I would like to add a new worksheet to the original workbook (Workbook 1) that is housing the macro and combine all the data found in the selected files. Again, I only need to process the first worksheet of each selected file. All the sheets will be the same, so the column heading should come along during the first copy of the first sheet in the first file selected, and then the rest of the data can go right below the "last used range" of the master worksheet.
What I have been trying to do is combine these two macros as follows:
Sub FilePicker()
Dim FD As Office.FileDialog
Dim file As Variant
Dim message As String
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
If .Show Then
For Each file In .SelectedItems
'****PLACE THE OTHER MACRO HERE***********
Next
End If
End With
End Sub
And place this macro inside the FOR EACH / NEXT:
Sub CopyFromWorksheets()
Dim wrk As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Master Worksheet
Dim rng As Range 'Range object
Dim colCount As Integer 'Column count in tables in the worksheets
Set wrk = ActiveWorkbook 'Working in active workbook
For Each sht In wrk.Worksheets
If sht.Name = "Master" Then
MsgBox "There is a worksheet called as 'Master'." & vbCrLf & _
"Please remove or rename this worksheet since 'Master' would be" & _
"the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
Exit Sub
End If
Next sht
'We don't want screen updating
Application.ScreenUpdating = False
'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
'Rename the new worksheet
trg.Name = "Master"
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Worksheets(1)
colCount = sht.Cells(1, 255).End(xlToLeft).Column
'Now retrieve headers, no copy&paste needed
With trg.Cells(1, 1).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value
'Set font as bold
.Font.Bold = True
End With
'We can start loop
For Each sht In wrk.Worksheets
'If worksheet in loop is the last one, stop execution (it is Master worksheet)
If sht.Index = wrk.Worksheets.Count Then
Exit For
End If
'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
'Put data into the Master worksheet
trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
Next sht
'Fit the columns in Master worksheet
trg.Columns.AutoFit
'Screen updating should be activated
Application.ScreenUpdating = True
End Sub
The second macro assumes that each of the worksheets to be copied are all located in the same workbook. I would try to create a solution that holds the information in VBA, pastes it in the aggregate / master worksheet, drops it and moves on to the next selected file instead of actually writing the data to a separate worksheets in Workbook 1.
Both of these macros work great independently ... I just can't combine them - HA!
Thanks in advance!
The BlueOne
I am somewhat embarrassed that I am "crying uncle" on this one ...
I have conducted quite a bit of research and wasted the better part of the day trying to tinker with solutions, but now I am throwing in the towel.
Quick summary:
I want to merge data from multiple files into a single workbook.
Here are the details:
1.) I would like to use one of the MSOFileDialog Objects to prompt the user to select files from a folder. My goal is to place this code into an add-in that the user will use to kick off this Sub. We can refer to this as Workbook 1.
2.) I only want to process the first worksheet from each file. I was thinking of using worksheets(1) versus worksheets("filename_with_a_Variable").
3.) I would like to add a new worksheet to the original workbook (Workbook 1) that is housing the macro and combine all the data found in the selected files. Again, I only need to process the first worksheet of each selected file. All the sheets will be the same, so the column heading should come along during the first copy of the first sheet in the first file selected, and then the rest of the data can go right below the "last used range" of the master worksheet.
What I have been trying to do is combine these two macros as follows:
Sub FilePicker()
Dim FD As Office.FileDialog
Dim file As Variant
Dim message As String
Set FD = Application.FileDialog(msoFileDialogFilePicker)
With FD
If .Show Then
For Each file In .SelectedItems
'****PLACE THE OTHER MACRO HERE***********
Next
End If
End With
End Sub
And place this macro inside the FOR EACH / NEXT:
Sub CopyFromWorksheets()
Dim wrk As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Master Worksheet
Dim rng As Range 'Range object
Dim colCount As Integer 'Column count in tables in the worksheets
Set wrk = ActiveWorkbook 'Working in active workbook
For Each sht In wrk.Worksheets
If sht.Name = "Master" Then
MsgBox "There is a worksheet called as 'Master'." & vbCrLf & _
"Please remove or rename this worksheet since 'Master' would be" & _
"the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
Exit Sub
End If
Next sht
'We don't want screen updating
Application.ScreenUpdating = False
'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
'Rename the new worksheet
trg.Name = "Master"
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Worksheets(1)
colCount = sht.Cells(1, 255).End(xlToLeft).Column
'Now retrieve headers, no copy&paste needed
With trg.Cells(1, 1).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value
'Set font as bold
.Font.Bold = True
End With
'We can start loop
For Each sht In wrk.Worksheets
'If worksheet in loop is the last one, stop execution (it is Master worksheet)
If sht.Index = wrk.Worksheets.Count Then
Exit For
End If
'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
'Put data into the Master worksheet
trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
Next sht
'Fit the columns in Master worksheet
trg.Columns.AutoFit
'Screen updating should be activated
Application.ScreenUpdating = True
End Sub
The second macro assumes that each of the worksheets to be copied are all located in the same workbook. I would try to create a solution that holds the information in VBA, pastes it in the aggregate / master worksheet, drops it and moves on to the next selected file instead of actually writing the data to a separate worksheets in Workbook 1.
Both of these macros work great independently ... I just can't combine them - HA!
Thanks in advance!
The BlueOne