A Microsoft Excel forum. ExcelBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » ExcelBanter forum » Excel Newsgroups » Excel Programming
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Copying a Range from Multiple Worksheets



 
 
Thread Tools Display Modes
  #1  
Old May 31st 12, 08:21 PM
E-on E-on is offline
Junior Member
 
First recorded activity by ExcelBanter: May 2012
Posts: 9
Default Copying a Range from Multiple Worksheets

I have posted it in other forms, but have not got any answer. I hope Banters would do the magic for me.

I have got the following code from Ron de Bruin’s site. I would like to make an adjustment to this code, but got stuck. Change to be made is highlighted in yellow. After all copies from individual sheets are done in the destination sheet, I would like the name of each files to appear in Column “A” of destination sheet not in Column “H” as per Ron’s code. I have added the following line, in order to insert a new column in Column “A”. Please help.

DestSh.Columns("A:B").Insert Shift:=xlToRigh

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 summary sheet if it exists.
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True

' Add a new summary worksheet.
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"

' Loop through all worksheets and copy the data to the
' summary worksheet.
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then

' Find the last row with data on the summary worksheet.
Last = LastRow(DestSh)

' Specify the range to place the data.
Set CopyRng = sh.Range("A1:G1")

' Test to see whether there are enough rows in the summary
' worksheet to copy all the data.
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the " & _
"summary worksheet to place the data."
GoTo ExitTheSub
End If

' This statement copies values and formats from each
' worksheet.
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With

' Optional: This statement will copy the sheet

' name in the H column. I would like the name of the sheet to be in Column A of destination sheet, instead of Column H. I have inserted the following line and changed the Column “H” in to “A”, but the code stopped working.


My Addition DestSh.Columns("A:B").Insert Shift:=xlToRight


DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name

End If
Next

ExitTheSub:

Application.Goto DestSh.Cells(1)

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

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Ads
  #2  
Old June 3rd 12, 04:28 AM posted to microsoft.public.excel.programming
isabelle
external usenet poster
 
Posts: 450
Default Copying a Range from Multiple Worksheets

hi E-on,

Last = LastRow(DestSh)

can you show the function "LastRow"

--
isabelle



Le 2012-05-31 15:21, E-on a écrit :
> I have posted it in other forms, but have not got any answer. I hope
> Banters would do the magic for me.
>
> I have got the following code from Ron de Bruin’s site. I would like
> to make an adjustment to this code, but got stuck. Change to be made is
> highlighted in yellow. After all copies from individual sheets are done
> in the destination sheet, I would like the name of each files to appear
> in Column “A” of destination sheet not in Column “H” as per Ron’s code.
> I have added the following line, in order to insert a new column in
> Column “A”. Please help.
>
> DestSh.Columns("A:B").Insert Shift:=xlToRigh
>
> 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 summary sheet if it exists.
> Application.DisplayAlerts = False
> On Error Resume Next
> ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
> On Error GoTo 0
> Application.DisplayAlerts = True
>
> ' Add a new summary worksheet.
> Set DestSh = ActiveWorkbook.Worksheets.Add
> DestSh.Name = "RDBMergeSheet"
>
> ' Loop through all worksheets and copy the data to the
> ' summary worksheet.
> For Each sh In ActiveWorkbook.Worksheets
> If sh.Name<> DestSh.Name Then
>
> ' Find the last row with data on the summary worksheet.
> Last = LastRow(DestSh)
>
> ' Specify the range to place the data.
> Set CopyRng = sh.Range("A1:G1")
>
> ' Test to see whether there are enough rows in the summary
> ' worksheet to copy all the data.
> If Last + CopyRng.Rows.Count> DestSh.Rows.Count Then
> MsgBox "There are not enough rows in the "& _
> "summary worksheet to place the data."
> GoTo ExitTheSub
> End If
>
> ' This statement copies values and formats from each
> ' worksheet.
> CopyRng.Copy
> With DestSh.Cells(Last + 1, "A")
> .PasteSpecial xlPasteValues
> .PasteSpecial xlPasteFormats
> Application.CutCopyMode = False
> End With
>
> ' Optional: This statement will copy the sheet
>
> ' name in the H column. I would like the name of the sheet
> to be in Column A of destination sheet, instead of Column H. I have
> inserted the following line and changed the Column “H” in to “A”, but
> the code stopped working.
>
>
> My Addition DestSh.Columns("A:B").Insert Shift:=xlToRight
>
>
> DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value
> = sh.Name
>
> End If
> Next
>
> ExitTheSub:
>
> Application.Goto DestSh.Cells(1)
>
> ' AutoFit the column width in the summary sheet.
> DestSh.Columns.AutoFit
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
> End Sub
>
>
>
>

  #3  
Old June 3rd 12, 05:19 PM
E-on E-on is offline
Junior Member
 
First recorded activity by ExcelBanter: May 2012
Posts: 9
Default

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

Isabelle! Thank you for your help!

The lastrow was a function. Please see above




Quote:
Originally Posted by isabelle View Post
hi E-on,

Last = LastRow(DestSh)

can you show the function "LastRow"

--
isabelle



Le 2012-05-31 15:21, E-on a écrit :
> I have posted it in other forms, but have not got any answer. I hope
> Banters would do the magic for me.
>
> I have got the following code from Ron de Bruin’s site. I would like
> to make an adjustment to this code, but got stuck. Change to be made is
> highlighted in yellow. After all copies from individual sheets are done
> in the destination sheet, I would like the name of each files to appear
> in Column “A” of destination sheet not in Column “H” as per Ron’s code.
> I have added the following line, in order to insert a new column in
> Column “A”. Please help.
>
> DestSh.Columns("A:B").Insert Shift:=xlToRigh
>
> 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 summary sheet if it exists.
> Application.DisplayAlerts = False
> On Error Resume Next
> ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
> On Error GoTo 0
> Application.DisplayAlerts = True
>
> ' Add a new summary worksheet.
> Set DestSh = ActiveWorkbook.Worksheets.Add
> DestSh.Name = "RDBMergeSheet"
>
> ' Loop through all worksheets and copy the data to the
> ' summary worksheet.
> For Each sh In ActiveWorkbook.Worksheets
> If sh.Name<> DestSh.Name Then
>
> ' Find the last row with data on the summary worksheet.
> Last = LastRow(DestSh)
>
> ' Specify the range to place the data.
> Set CopyRng = sh.Range("A1:G1")
>
> ' Test to see whether there are enough rows in the summary
> ' worksheet to copy all the data.
> If Last + CopyRng.Rows.Count> DestSh.Rows.Count Then
> MsgBox "There are not enough rows in the "& _
> "summary worksheet to place the data."
> GoTo ExitTheSub
> End If
>
> ' This statement copies values and formats from each
> ' worksheet.
> CopyRng.Copy
> With DestSh.Cells(Last + 1, "A")
> .PasteSpecial xlPasteValues
> .PasteSpecial xlPasteFormats
> Application.CutCopyMode = False
> End With
>
> ' Optional: This statement will copy the sheet
>
> ' name in the H column. I would like the name of the sheet
> to be in Column A of destination sheet, instead of Column H. I have
> inserted the following line and changed the Column “H” in to “A”, but
> the code stopped working.
>
>
> My Addition DestSh.Columns("A:B").Insert Shift:=xlToRight
>
>
> DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value
> = sh.Name
>
> End If
> Next
>
> ExitTheSub:
>
> Application.Goto DestSh.Cells(1)
>
> ' AutoFit the column width in the summary sheet.
> DestSh.Columns.AutoFit
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
> End Sub
>
>
>
>
  #4  
Old June 4th 12, 02:08 AM posted to microsoft.public.excel.programming
isabelle
external usenet poster
 
Posts: 450
Default Copying a Range from Multiple Worksheets

hi E-on,

replace
With DestSh.Cells(Last + 1, "A")

by
With DestSh.Cells(Last + 1, "B")

and

replace
DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name

by
DestSh.Cells(Last + 1, "A").Resize(CopyRng.Rows.Count).Value = sh.Name

--
isabelle



Le 2012-06-03 12:19, E-on a écrit :
> 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
>
> Isabelle! Thank you for your help!
>
> The lastrow was a function. Please see above
>
>
>
>
> isabelle;1602335 Wrote:
>> hi E-on,
>>
>> Last = LastRow(DestSh)
>>
>> can you show the function "LastRow"
>>
>> --
>> isabelle
>>
>>
>>
>> Le 2012-05-31 15:21, E-on a écrit :-
>>> I have posted it in other forms, but have not got any answer. I hope
>>> Banters would do the magic for me.
>>>
>>> I have got the following code from Ron de Bruin’s site. I would like
>>> to make an adjustment to this code, but got stuck. Change to be made

>> is
>>> highlighted in yellow. After all copies from individual sheets are

>> done
>>> in the destination sheet, I would like the name of each files to

>> appear
>>> in Column “A” of destination sheet not in Column “H” as per Ron’s

>> code.
>>> I have added the following line, in order to insert a new column in
>>> Column “A”. Please help.
>>>
>>> DestSh.Columns("A:B").Insert Shift:=xlToRigh
>>>
>>> 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 summary sheet if it exists.
>>> Application.DisplayAlerts = False
>>> On Error Resume Next
>>> ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
>>> On Error GoTo 0
>>> Application.DisplayAlerts = True
>>>
>>> ' Add a new summary worksheet.
>>> Set DestSh = ActiveWorkbook.Worksheets.Add
>>> DestSh.Name = "RDBMergeSheet"
>>>
>>> ' Loop through all worksheets and copy the data to the
>>> ' summary worksheet.
>>> For Each sh In ActiveWorkbook.Worksheets
>>> If sh.Name<> DestSh.Name Then
>>>
>>> ' Find the last row with data on the summary worksheet.
>>> Last = LastRow(DestSh)
>>>
>>> ' Specify the range to place the data.
>>> Set CopyRng = sh.Range("A1:G1")
>>>
>>> ' Test to see whether there are enough rows in the summary
>>> ' worksheet to copy all the data.
>>> If Last + CopyRng.Rows.Count> DestSh.Rows.Count Then
>>> MsgBox "There are not enough rows in the "& _
>>> "summary worksheet to place the data."
>>> GoTo ExitTheSub
>>> End If
>>>
>>> ' This statement copies values and formats from each
>>> ' worksheet.
>>> CopyRng.Copy
>>> With DestSh.Cells(Last + 1, "A")
>>> .PasteSpecial xlPasteValues
>>> .PasteSpecial xlPasteFormats
>>> Application.CutCopyMode = False
>>> End With
>>>
>>> ' Optional: This statement will copy the sheet
>>>
>>> ' name in the H column. I would like the name of the sheet
>>> to be in Column A of destination sheet, instead of Column H. I have
>>> inserted the following line and changed the Column “H” in to “A”, but
>>> the code stopped working.
>>>
>>>
>>> My Addition DestSh.Columns("A:B").Insert Shift:=xlToRight
>>>
>>>
>>> DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value
>>> = sh.Name
>>>
>>> End If
>>> Next
>>>
>>> ExitTheSub:
>>>
>>> Application.Goto DestSh.Cells(1)
>>>
>>> ' AutoFit the column width in the summary sheet.
>>> DestSh.Columns.AutoFit
>>>
>>> With Application
>>> .ScreenUpdating = True
>>> .EnableEvents = True
>>> End With
>>> End Sub
>>>
>>>
>>>
>>> -

>
>
>
>

  #5  
Old June 4th 12, 02:47 PM
E-on E-on is offline
Junior Member
 
First recorded activity by ExcelBanter: May 2012
Posts: 9
Smile

isabelle! Thank you very much. I have changed the lines and is now working the way I wanted it.
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying multiple cells out of multiple worksheets at same time. tom Excel Discussion (Misc queries) 1 April 2nd 10 09:03 PM
Copying a range of data across multiple worksheets JLGWhiz Excel Programming 0 April 29th 09 06:12 PM
Copying a range of data across multiple worksheets Isaiah Melton Excel Programming 0 April 29th 09 05:26 PM
Copying multiple Worksheets Dor474c Excel Discussion (Misc queries) 0 June 29th 05 10:10 PM
Copying from multiple worksheets GMP Excel Discussion (Misc queries) 4 May 6th 05 12:59 AM


All times are GMT +1. The time now is 07:50 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2004-2013 ExcelBanter.
The comments are property of their posters.