L
Legacy 233604
Guest
Hi Everyone,
I am new to excel programming. I found a great example of a similiar macro on this site that copies sheet1 from all files in this directory. I would like to add a couple of new features to this code:
1). I would like the user to browse to a directory for the source files. (I have example macro code for this as well).
2). The source worksbooks and the destination workbook will not be in the same directory. However, the destination workbook would be the active workbook.
3). I would like to put the copied data, from the folder of source workbooks, into the destination workbook (active) after a worksheet that is named hours.
I have tried several things and need help combing the 2 macros together.
Here are the two macros:
Sub CopySheeet1FromAllExcelFilesInThisFilesDirectory()
'Note last row of data in each Sheet1 is presumed to be the
'last row of Column A with data in it.
Dim strFileDirectory As String
Dim strFileName As String
Dim iAnswer As Integer
Dim intFileCount As Integer
Dim lNextWriteRow As Long
Dim sError As String
Dim sReport As String
Dim lX As Long
Dim bFound As Boolean
Dim lLastDataRow As Long
Dim iVisibleWindows As Integer
Dim sPreface As String
Dim lNextSummaryWriteRow As Long
Dim lLinesCopied As Long
'This workbook saved?
If ThisWorkbook.Path = "" Then
MsgBox "Save this file in the desired directory before continuing"
GoTo End_Sub
End If
For lX = 1 To Sheets.Count
If Worksheets(lX).Name = "Sheet1" Then
bFound = True
Exit For
End If
Next
If Not bFound Then Worksheets.Add(before:=Sheets(1)).Name = "Sheet1"
bFound = False
For lX = 1 To Sheets.Count
If Worksheets(lX).Name = "Summary" Then
bFound = True
Exit For
End If
Next
If Not bFound Then Worksheets.Add(after:=Sheets("Sheet1")).Name = "Summary"
'Close other workbooks - they may be ones we want to process
'and we don't want to overwrite them.
If Windows.Count > 1 Then
iAnswer = MsgBox("Close other workbooks and continue?" & vbCrLf & _
" OK to close all other workbooks and continue, or" & vbCrLf & _
" Cancel to stop this macro.", vbOKCancel + _
vbDefaultButton2 + vbExclamation, "Continue ?")
End If
If iAnswer = vbCancel Then
GoTo End_Sub
Else
iVisibleWindows = Windows.Count
For lX = Windows.Count To 1 Step -1
If Windows(lX).Caption <> ThisWorkbook.Name Then
If Windows(lX).Visible Then
'if workbook modified user will get
'chance to save or cancel for each
Windows(lX).Close
End If
iVisibleWindows = iVisibleWindows - 1
End If
Next
End If
strFileDirectory = ThisWorkbook.Path & "\"
'See if user chose Cancel for any close requests
If iVisibleWindows > 1 Then
MsgBox "Other Excel workbooks are still open. " & _
"Close other workbooks and try again", , "Process Cancelled."
GoTo End_Sub
End If
'More than this .xls file in the directory?
strFileName = Dir(strFileDirectory & "*.xls", 1)
Do While strFileName <> ""
intFileCount = intFileCount + 1
strFileName = Dir
Loop
If intFileCount = 1 Then
MsgBox "There are no other Excel files in the specified directory: " & vbCrLf & _
" " & strFileDirectory & vbCrLf & _
"There is nothing to process.", , "No Excel Files"
GoTo End_Sub
End If
iAnswer = MsgBox("All data on worksheets: 'Sheet1' and 'Summary' will be deleted. Continue?", vbOKCancel, "Clear Sheet1?")
If iAnswer = vbOK Then
MsgBox "DO NOT ENABLE MACROS IN ANY FILE THAT YOU ARE UNSURE OF" & vbLf & vbLf & _
"If you are prompted to enable macros for any file then you must do so for this process to complete." & vbLf & vbLf & _
"DO NOT ENABLE MACROS IN ANY FILE THAT YOU ARE UNSURE OF"
With ThisWorkbook.Worksheets("Sheet1")
.UsedRange.Clear
For lX = .Shapes.Count To 1 Step -1
.Shapes(lX).Delete
Next
End With
ThisWorkbook.Worksheets("Summary").UsedRange.Clear
lNextWriteRow = 1
lNextSummaryWriteRow = 2
Worksheets("Summary").Range("A1").Resize(1, 4).Value = Array("WorkBook Copied", "# Lines", "", "Total Lines Copied")
'Process other workbooks
strFileName = Dir(strFileDirectory & "*.xls?", 1)
Do While strFileName <> ""
If UCase(strFileName) <> UCase(ThisWorkbook.Name) And _
UCase(strFileName) <> "PERSONAL.XLS" And _
strFileName <> "PERSONAL.XLSM" Then
bFound = False
Workbooks.Open Filename:=strFileDirectory & strFileName
'Process file
For lX = 1 To Worksheets.Count
If Worksheets(lX).Name = "Sheet1" Then
bFound = True
Exit For
End If
Next
If bFound Then
Worksheets("sheet1").Activate
Range("A1").Select 'in case workbook was saved with an object selected
lLastDataRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet1").Rows("1:" & lLastDataRow).Copy Destination:=ThisWorkbook.Sheets("Sheet1").Cells(lNextWriteRow, 1)
sReport = sReport & vbLf & lLastDataRow & vbTab & ActiveWorkbook.Name
ThisWorkbook.Worksheets("Summary").Cells(lNextSummaryWriteRow, 1) = strFileName
ThisWorkbook.Worksheets("Summary").Cells(lNextSummaryWriteRow, 2) = lLastDataRow
lNextSummaryWriteRow = lNextSummaryWriteRow + 1
lLinesCopied = lLinesCopied + lLastDataRow
Else
sError = sError & vbLf & ActiveWorkbook.Name
End If
ActiveWorkbook.Saved = True
Windows(strFileName).Close
End If
strFileName = Dir
lNextWriteRow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
With ThisWorkbook.Worksheets("Summary")
.Columns("A:D").EntireColumn.AutoFit
.Range("A1").Activate
End With
Loop
ThisWorkbook.Worksheets("Summary").Cells(2, 4) = lLinesCopied
sPreface = "For directory: " & ThisWorkbook.Path & vbLf & vbLf
If sReport <> "" Then sReport = sReport & vbLf & "------" & vbLf & _
lLinesCopied & vbTab & "Total Lines Copied"
If sError <> "" Then sReport = sReport & vbLf & vbLf & _
"The following workbooks did not contain a worksheet named 'Sheet1':" & vbLf
MsgBox sPreface & "Rows" & vbTab & "Copied from 'Sheet1'" & vbLf & "1 thru" & vbTab & " in each of these workbooks:" & vbLf & _
sReport & sError
Sheets("Sheet1").Copy
With ThisWorkbook.Worksheets("Sheet1")
.UsedRange.Clear
For lX = .Shapes.Count To 1 Step -1
.Shapes(lX).Delete
Next
End With
Else
MsgBox "Process Cancelled"
End If
End_Sub:
End Sub
Option Explicit
Sub RDB_Merge_Data_Browse()
Dim myFiles As Variant
Dim myCountOfFiles As Long
Dim oApp As Object
Dim oFolder As Variant
Set oApp = CreateObject("Shell.Application")
'Browse to the folder
Set oFolder = oApp.BrowseForFolder(0, "Select folder", 512)
If Not oFolder Is Nothing Then
myCountOfFiles = Get_File_Names( _
MyPath:=oFolder.Self.Path, _
Subfolders:=False, _
ExtStr:="*.xl*", _
myReturnedFiles:=myFiles)
If myCountOfFiles = 0 Then
MsgBox "No files that match the ExtStr in this folder"
Exit Sub
End If
End If
End Sub
I am new to excel programming. I found a great example of a similiar macro on this site that copies sheet1 from all files in this directory. I would like to add a couple of new features to this code:
1). I would like the user to browse to a directory for the source files. (I have example macro code for this as well).
2). The source worksbooks and the destination workbook will not be in the same directory. However, the destination workbook would be the active workbook.
3). I would like to put the copied data, from the folder of source workbooks, into the destination workbook (active) after a worksheet that is named hours.
I have tried several things and need help combing the 2 macros together.
Here are the two macros:
Sub CopySheeet1FromAllExcelFilesInThisFilesDirectory()
'Note last row of data in each Sheet1 is presumed to be the
'last row of Column A with data in it.
Dim strFileDirectory As String
Dim strFileName As String
Dim iAnswer As Integer
Dim intFileCount As Integer
Dim lNextWriteRow As Long
Dim sError As String
Dim sReport As String
Dim lX As Long
Dim bFound As Boolean
Dim lLastDataRow As Long
Dim iVisibleWindows As Integer
Dim sPreface As String
Dim lNextSummaryWriteRow As Long
Dim lLinesCopied As Long
'This workbook saved?
If ThisWorkbook.Path = "" Then
MsgBox "Save this file in the desired directory before continuing"
GoTo End_Sub
End If
For lX = 1 To Sheets.Count
If Worksheets(lX).Name = "Sheet1" Then
bFound = True
Exit For
End If
Next
If Not bFound Then Worksheets.Add(before:=Sheets(1)).Name = "Sheet1"
bFound = False
For lX = 1 To Sheets.Count
If Worksheets(lX).Name = "Summary" Then
bFound = True
Exit For
End If
Next
If Not bFound Then Worksheets.Add(after:=Sheets("Sheet1")).Name = "Summary"
'Close other workbooks - they may be ones we want to process
'and we don't want to overwrite them.
If Windows.Count > 1 Then
iAnswer = MsgBox("Close other workbooks and continue?" & vbCrLf & _
" OK to close all other workbooks and continue, or" & vbCrLf & _
" Cancel to stop this macro.", vbOKCancel + _
vbDefaultButton2 + vbExclamation, "Continue ?")
End If
If iAnswer = vbCancel Then
GoTo End_Sub
Else
iVisibleWindows = Windows.Count
For lX = Windows.Count To 1 Step -1
If Windows(lX).Caption <> ThisWorkbook.Name Then
If Windows(lX).Visible Then
'if workbook modified user will get
'chance to save or cancel for each
Windows(lX).Close
End If
iVisibleWindows = iVisibleWindows - 1
End If
Next
End If
strFileDirectory = ThisWorkbook.Path & "\"
'See if user chose Cancel for any close requests
If iVisibleWindows > 1 Then
MsgBox "Other Excel workbooks are still open. " & _
"Close other workbooks and try again", , "Process Cancelled."
GoTo End_Sub
End If
'More than this .xls file in the directory?
strFileName = Dir(strFileDirectory & "*.xls", 1)
Do While strFileName <> ""
intFileCount = intFileCount + 1
strFileName = Dir
Loop
If intFileCount = 1 Then
MsgBox "There are no other Excel files in the specified directory: " & vbCrLf & _
" " & strFileDirectory & vbCrLf & _
"There is nothing to process.", , "No Excel Files"
GoTo End_Sub
End If
iAnswer = MsgBox("All data on worksheets: 'Sheet1' and 'Summary' will be deleted. Continue?", vbOKCancel, "Clear Sheet1?")
If iAnswer = vbOK Then
MsgBox "DO NOT ENABLE MACROS IN ANY FILE THAT YOU ARE UNSURE OF" & vbLf & vbLf & _
"If you are prompted to enable macros for any file then you must do so for this process to complete." & vbLf & vbLf & _
"DO NOT ENABLE MACROS IN ANY FILE THAT YOU ARE UNSURE OF"
With ThisWorkbook.Worksheets("Sheet1")
.UsedRange.Clear
For lX = .Shapes.Count To 1 Step -1
.Shapes(lX).Delete
Next
End With
ThisWorkbook.Worksheets("Summary").UsedRange.Clear
lNextWriteRow = 1
lNextSummaryWriteRow = 2
Worksheets("Summary").Range("A1").Resize(1, 4).Value = Array("WorkBook Copied", "# Lines", "", "Total Lines Copied")
'Process other workbooks
strFileName = Dir(strFileDirectory & "*.xls?", 1)
Do While strFileName <> ""
If UCase(strFileName) <> UCase(ThisWorkbook.Name) And _
UCase(strFileName) <> "PERSONAL.XLS" And _
strFileName <> "PERSONAL.XLSM" Then
bFound = False
Workbooks.Open Filename:=strFileDirectory & strFileName
'Process file
For lX = 1 To Worksheets.Count
If Worksheets(lX).Name = "Sheet1" Then
bFound = True
Exit For
End If
Next
If bFound Then
Worksheets("sheet1").Activate
Range("A1").Select 'in case workbook was saved with an object selected
lLastDataRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet1").Rows("1:" & lLastDataRow).Copy Destination:=ThisWorkbook.Sheets("Sheet1").Cells(lNextWriteRow, 1)
sReport = sReport & vbLf & lLastDataRow & vbTab & ActiveWorkbook.Name
ThisWorkbook.Worksheets("Summary").Cells(lNextSummaryWriteRow, 1) = strFileName
ThisWorkbook.Worksheets("Summary").Cells(lNextSummaryWriteRow, 2) = lLastDataRow
lNextSummaryWriteRow = lNextSummaryWriteRow + 1
lLinesCopied = lLinesCopied + lLastDataRow
Else
sError = sError & vbLf & ActiveWorkbook.Name
End If
ActiveWorkbook.Saved = True
Windows(strFileName).Close
End If
strFileName = Dir
lNextWriteRow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
With ThisWorkbook.Worksheets("Summary")
.Columns("A:D").EntireColumn.AutoFit
.Range("A1").Activate
End With
Loop
ThisWorkbook.Worksheets("Summary").Cells(2, 4) = lLinesCopied
sPreface = "For directory: " & ThisWorkbook.Path & vbLf & vbLf
If sReport <> "" Then sReport = sReport & vbLf & "------" & vbLf & _
lLinesCopied & vbTab & "Total Lines Copied"
If sError <> "" Then sReport = sReport & vbLf & vbLf & _
"The following workbooks did not contain a worksheet named 'Sheet1':" & vbLf
MsgBox sPreface & "Rows" & vbTab & "Copied from 'Sheet1'" & vbLf & "1 thru" & vbTab & " in each of these workbooks:" & vbLf & _
sReport & sError
Sheets("Sheet1").Copy
With ThisWorkbook.Worksheets("Sheet1")
.UsedRange.Clear
For lX = .Shapes.Count To 1 Step -1
.Shapes(lX).Delete
Next
End With
Else
MsgBox "Process Cancelled"
End If
End_Sub:
End Sub
Option Explicit
Sub RDB_Merge_Data_Browse()
Dim myFiles As Variant
Dim myCountOfFiles As Long
Dim oApp As Object
Dim oFolder As Variant
Set oApp = CreateObject("Shell.Application")
'Browse to the folder
Set oFolder = oApp.BrowseForFolder(0, "Select folder", 512)
If Not oFolder Is Nothing Then
myCountOfFiles = Get_File_Names( _
MyPath:=oFolder.Self.Path, _
Subfolders:=False, _
ExtStr:="*.xl*", _
myReturnedFiles:=myFiles)
If myCountOfFiles = 0 Then
MsgBox "No files that match the ExtStr in this folder"
Exit Sub
End If
End If
End Sub