View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default ron de bruins code won't concatenate

Is it only processing one worksheet or is it only copying once?

I'd change this:

For Each sh In ActiveWorkbook.Worksheets
If LCase(Left(sh.Name, 4)) = "1111" Then

to:

For Each sh In ActiveWorkbook.Worksheets
msgbox sh.range("A1").address(external:=true)
If LCase(Left(sh.Name, 4)) = "1111" Then
msgbox "processing " & sh.name

The first msgbox will show you the entire address for A1 of every sheet
(including drive, path, filename, sheet name and address). Maybe you'll see
something wrong.

The second msgbox will tell you if the loop is at least starting. And if it is,
then I bet Barb has the correct solution.


Theo wrote:

Nope -neither of those are the case.
I've tried other things, according to some of his tips, like naming the
worksheets in
an array, but I still only get one worksheet - the last one.

T

"Dave Peterson" wrote:

This line
If LCase(Left(sh.Name, 4)) = "1111" Then
looks at the first 4 characters of the sheet name.

So either the wrong workbook is active (and it only has a single worksheet that
begins with 1111) or your worksheet names have something else before those
1111's--maybe an extra space?

Theo wrote:

Hi all - got this code from Ron's site. but it only copies the last worksheet
that begins with 1111; there are 3 worksheets that begin with 1111. HELP!
If each 1111 worksheet has 100 rows, I am expecting the new worksheet to
have 300 rows.

Thanks
T
Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'Delete the sheet "RDBMergeSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True

'Add a worksheet with the name "RDBMergeSheet"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"

'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If LCase(Left(sh.Name, 4)) = "1111" Then


Set CopyRng = sh.Range("A1").CurrentRegion


'Fill in the range that you want to copy
Set CopyRng = sh.Range("A1").CurrentRegion

'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If

'This example copies values/formats, if you only want to copy the
'values or want to copy everything look at the example below
this macro
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With

End If
Next

ExitTheSub:

Application.Goto DestSh.Cells(1)

'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub

Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function

Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function


--

Dave Peterson


--

Dave Peterson