View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.newusers
Otto Moehrbach Otto Moehrbach is offline
external usenet poster
 
Posts: 1,090
Default Help with Worksheet Merging Macro

Joe
What I usually do is use a "For" loop statement like:
For each ws in ThisWorkbook.Worksheets
That would include every sheet in the file. If you wanted to exclude one or
more sheets, say sheets "One" and "Two", you could use a statement like:
If ws.Name<"One" And ws.Name<"Two" Then
'Your code would go here.
End if
If every sheet that you wanted to operate on is named "RAY......." and you
want to exclude all others, you could use a statement like:
If Left(ws.Name,3) = "RAY" Then
'Your code would go here.
End If
HTH Otto
"Joe" wrote in message
...
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