#1   Report Post  
Mark Jackson
 
Posts: n/a
Default Merge Worksheets

Hello,

I have a macro that will merge my worksheets into one worksheet called
"Master". It works fine. Is it possible when I merge my worksheets again to
create another worksheet called "Master1"..."Master2"..."Master3"...and so on
without overwriting or deleting the previous "Master" worksheet? The
workbook is used in Share Mode. Here is the macro that I use:

Sub CopyFromWorksheets()
Dim wrk As Workbook 'Workbook object - Always good to work with object
variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Master Worksheet
Dim Rng As Range 'Range object
Dim colCount As Integer 'Column count in tables in the worksheets

Set wrk = ActiveWorkbook 'Working in active workbook

For Each sht In wrk.Worksheets
If sht.Name = "Master" Then
sht.Delete
End If

Next sht

'We don't want screen updating
Application.ScreenUpdating = False

'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Works heets.Count))
'Rename the new worksheet
trg.Name = "Master"
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Worksheets(1)
colCount = sht.Cells(1, 255).End(xlToLeft).Column
'Now retrieve headers, no copy&paste needed
With trg.Cells(1, 1).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value
'Set font as bold
.Font.Bold = True
End With

'We can start loop
For Each sht In wrk.Worksheets
'If worksheet in loop is the last one, stop execution (it is Master
worksheet)
If sht.Index = wrk.Worksheets.Count Then
Exit For
End If
'Data range in worksheet - starts from second row as first rows are
the header rows in all worksheets
Set Rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536,
1).End(xlUp).Resize(, colCount))
'Put data into the Master worksheet
trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(Rng.Rows.Count,
Rng.Columns.Count).Value = Rng.Value
Next sht
'Fit the columns in Master worksheet
trg.Columns.AutoFit

'Screen updating should be activated
Application.ScreenUpdating = True
End Sub

I appreciate your help. T

Thanks
Mark
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge Worksheets Mark Jackson Excel Discussion (Misc queries) 1 June 9th 05 11:39 AM
Trying to merge two excel worksheets Matt Carter Excel Discussion (Misc queries) 0 April 8th 05 04:11 PM
how to merge worksheets in the same workbook how to merge worksheets in a workbook Excel Worksheet Functions 3 January 5th 05 09:49 AM
Merge Data From Multiple Worksheets Corby Excel Worksheet Functions 1 December 22nd 04 04:25 PM
Multiple Worksheets and Print Merge function pfe Excel Discussion (Misc queries) 2 December 3rd 04 12:23 AM


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

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

About Us

"It's about Microsoft Excel"