J
Joe
I found the following post for Merging Excel worksheets and after I added
the Array statement it works great for my application except for one minor
problem. I have to crate this report on a monthly basis and all three of the
worksheets do not exist every month. I no there has to be a way to attach a
statement that checks to see that each tab exists before proceeding or
existing the macro becasue of an error. Could someone please help?
Sub MergeSheets()
' Merges data from all the selected worksheets onto the end of the
' active worksheet.
Const NHR = 1 'Number of header rows to not copy from each MWS
Sheets(Array("RAY517", "RAY518, RAY519")).Select
Sheets("RAY517").Activate
Dim MWS As Worksheet 'Worksheet to be merged
Dim AWS As Worksheet 'Worksheet to which the data are transferred
Dim FAR As Long 'First available row on AWS
Dim LR As Long 'Last row on the MWS sheets
Set AWS = ActiveSheet
For Each MWS In ActiveWindow.SelectedSheets
If Not MWS Is AWS Then
FAR = AWS.UsedRange.Cells(AWS.UsedRange.Cells.Count).Row + 1
LR = MWS.UsedRange.Cells(MWS.UsedRange.Cells.Count).Row
MWS.Range(MWS.Rows(NHR + 1), MWS.Rows(LR)).Copy AWS.Rows(FAR)
End If
Next MWS
End If
Joe
the Array statement it works great for my application except for one minor
problem. I have to crate this report on a monthly basis and all three of the
worksheets do not exist every month. I no there has to be a way to attach a
statement that checks to see that each tab exists before proceeding or
existing the macro becasue of an error. Could someone please help?
Sub MergeSheets()
' Merges data from all the selected worksheets onto the end of the
' active worksheet.
Const NHR = 1 'Number of header rows to not copy from each MWS
Sheets(Array("RAY517", "RAY518, RAY519")).Select
Sheets("RAY517").Activate
Dim MWS As Worksheet 'Worksheet to be merged
Dim AWS As Worksheet 'Worksheet to which the data are transferred
Dim FAR As Long 'First available row on AWS
Dim LR As Long 'Last row on the MWS sheets
Set AWS = ActiveSheet
For Each MWS In ActiveWindow.SelectedSheets
If Not MWS Is AWS Then
FAR = AWS.UsedRange.Cells(AWS.UsedRange.Cells.Count).Row + 1
LR = MWS.UsedRange.Cells(MWS.UsedRange.Cells.Count).Row
MWS.Range(MWS.Rows(NHR + 1), MWS.Rows(LR)).Copy AWS.Rows(FAR)
End If
Next MWS
End If
Joe