PC Review


Reply
Thread Tools Rate Thread

Copy Data from several sheets into one issue

 
 
mirox
Guest
Posts: n/a
 
      27th Feb 2009
Hereby the macro to copy data from several sheets starting from row 2
till the last row with data - except header(use the function LastRow)

But I want to copy the all data from these sheets plus header data
from one of these sheets named i.e. MON

Please let me know how to modify code to ensure that additionally
header data will be copied but only from on sheet.

Many thanks.
Rgds.

********************************************************************************************************************
Sub CopyData()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("WEEK").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "WEEK"
StartRow = 2
For Each sh In ActiveWorkbook.Worksheets
If IsError(Application.Match(sh.Name, _
Array(DestSh.Name, "RPT - MON",
"RPT - TUE", "RPT - WED", "RPT - THU", "RPT - FRI", "RPT - SAT", "RPT
- SUN", "WEEK TOTAL"), 0)) Then
Last = LastRow(DestSh)
shLast = LastRow(sh)
If shLast > 0 And shLast >= StartRow Then
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows
(shLast))
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
End If
Next
ExitTheSub:
Application.GoTo DestSh.Cells(1)
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

********************************************************************************************************************

 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      27th Feb 2009
Try this change

From:
DestSh.Name = "WEEK"
StartRow = 2
For Each sh In ActiveWorkbook.Worksheets


To:

DestSh.Name = "WEEK"
First = True
StartRow = 2
For Each sh In ActiveWorkbook.Worksheets
If First = True then
sh.rows(1).copy destination:=DestSh.Rows(1)
First = False
End If

"mirox" wrote:

> Hereby the macro to copy data from several sheets starting from row 2
> till the last row with data - except header(use the function LastRow)
>
> But I want to copy the all data from these sheets plus header data
> from one of these sheets named i.e. MON
>
> Please let me know how to modify code to ensure that additionally
> header data will be copied but only from on sheet.
>
> Many thanks.
> Rgds.
>
> ********************************************************************************************************************
> Sub CopyData()
> Dim sh As Worksheet
> Dim DestSh As Worksheet
> Dim Last As Long
> Dim shLast As Long
> Dim CopyRng As Range
> Dim StartRow As Long
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
> Application.DisplayAlerts = False
> On Error Resume Next
> ActiveWorkbook.Worksheets("WEEK").Delete
> On Error GoTo 0
> Application.DisplayAlerts = True
> Set DestSh = ActiveWorkbook.Worksheets.Add
> DestSh.Name = "WEEK"
> StartRow = 2
> For Each sh In ActiveWorkbook.Worksheets
> If IsError(Application.Match(sh.Name, _
> Array(DestSh.Name, "RPT - MON",
> "RPT - TUE", "RPT - WED", "RPT - THU", "RPT - FRI", "RPT - SAT", "RPT
> - SUN", "WEEK TOTAL"), 0)) Then
> Last = LastRow(DestSh)
> shLast = LastRow(sh)
> If shLast > 0 And shLast >= StartRow Then
> Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows
> (shLast))
> If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
> MsgBox "There are not enough rows in the Destsh"
> GoTo ExitTheSub
> End If
> CopyRng.Copy
> With DestSh.Cells(Last + 1, "A")
> .PasteSpecial xlPasteValues
> .PasteSpecial xlPasteFormats
> Application.CutCopyMode = False
> End With
> End If
> End If
> Next
> ExitTheSub:
> Application.GoTo DestSh.Cells(1)
> 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
>
> ********************************************************************************************************************
>
>

 
Reply With Quote
 
Bernie Deitrick
Guest
Posts: n/a
 
      27th Feb 2009
See two minor changes below, denoted by '********

HTH,
Bernie
MS Excel MVP


Sub CopyData()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With


Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("WEEK").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "WEEK"

'*******change StartRow to 1 instead of 2
StartRow = 1

For Each sh In ActiveWorkbook.Worksheets
If IsError(Application.Match(sh.Name, _
Array(DestSh.Name, "RPT - MON",
"RPT - TUE", "RPT - WED", "RPT - THU", "RPT - FRI", "RPT - SAT", "RPT
- SUN", "WEEK TOTAL"), 0)) Then
Last = LastRow(DestSh)
shLast = LastRow(sh)
If shLast > 0 And shLast >= StartRow Then
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows
(shLast))
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False

'************* Add this line after a successful copy, to no longer copy headers
If StartRow = 1 Then StartRow = 2

End With
End If
End If
Next
ExitTheSub:
Application.GoTo DestSh.Cells(1)
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


"mirox" <(E-Mail Removed)> wrote in message
news:1ebf7242-4f2c-426f-a46a-(E-Mail Removed)...
> Hereby the macro to copy data from several sheets starting from row 2
> till the last row with data - except header(use the function LastRow)
>
> But I want to copy the all data from these sheets plus header data
> from one of these sheets named i.e. MON
>
> Please let me know how to modify code to ensure that additionally
> header data will be copied but only from on sheet.
>
> Many thanks.
> Rgds.
>
> ********************************************************************************************************************
> Sub CopyData()
> Dim sh As Worksheet
> Dim DestSh As Worksheet
> Dim Last As Long
> Dim shLast As Long
> Dim CopyRng As Range
> Dim StartRow As Long
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
> Application.DisplayAlerts = False
> On Error Resume Next
> ActiveWorkbook.Worksheets("WEEK").Delete
> On Error GoTo 0
> Application.DisplayAlerts = True
> Set DestSh = ActiveWorkbook.Worksheets.Add
> DestSh.Name = "WEEK"
> StartRow = 2
> For Each sh In ActiveWorkbook.Worksheets
> If IsError(Application.Match(sh.Name, _
> Array(DestSh.Name, "RPT - MON",
> "RPT - TUE", "RPT - WED", "RPT - THU", "RPT - FRI", "RPT - SAT", "RPT
> - SUN", "WEEK TOTAL"), 0)) Then
> Last = LastRow(DestSh)
> shLast = LastRow(sh)
> If shLast > 0 And shLast >= StartRow Then
> Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows
> (shLast))
> If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
> MsgBox "There are not enough rows in the Destsh"
> GoTo ExitTheSub
> End If
> CopyRng.Copy
> With DestSh.Cells(Last + 1, "A")
> .PasteSpecial xlPasteValues
> .PasteSpecial xlPasteFormats
> Application.CutCopyMode = False
> End With
> End If
> End If
> Next
> ExitTheSub:
> Application.GoTo DestSh.Cells(1)
> 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
>
> ********************************************************************************************************************
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy data to different sheets James Merrill Microsoft Excel Worksheet Functions 6 12th Nov 2009 06:19 AM
Copy data into sheets K Microsoft Excel Programming 13 7th Sep 2009 09:11 AM
Copy data to new sheets Sverre Microsoft Excel Programming 19 28th Aug 2009 04:01 PM
Copy data to sheets by name oakman Microsoft Excel Programming 3 23rd Mar 2006 12:30 AM
copy data from other sheets Vadiraj Microsoft Excel Worksheet Functions 1 29th Jan 2004 08:40 AM


Features
 

Advertising
 

Newsgroups
 


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