merge sheet vba help

Dan88

Active Member
Joined
Feb 14, 2008
Messages
275
Office Version
  1. 365
Platform
  1. Windows
Hello everyone. Is there a way to tweak the below vba so it only merges 2 sheets from my workbook vs ALL the sheets in the workbook?
For instance i need it to merge sheet name "hello" and sheet name "bye"

THanks!!

Sub merge()
Dim wrk As Workbook
Dim sht As Worksheet
Dim trg As Worksheet
Dim rng As Range
Dim colCount As Integer

Set wrk = ActiveWorkbook




Application.ScreenUpdating = False



Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))


trg.Name = "ARP "


Set sht = wrk.Worksheets(1)
colCount = sht.Cells(1, 255).End(xlToLeft).Column


With trg.Cells(1, 1).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value


.Font.Bold = True
End With


For Each sht In wrk.Worksheets


If sht.Index = wrk.Worksheets.Count Then
Exit For
End If

Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))


trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
Next sht


trg.Columns.AutoFit



Application.ScreenUpdating = True
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi Dan88,

Try this:

Code:
Option Explicit
Sub merge()

    Dim wrk As Workbook
    Dim sht As Worksheet
    Dim trg As Worksheet
    Dim rng As Range
    Dim colCount As Long
    Dim varMySheets As Variant
    
    Application.ScreenUpdating = False
    
    varMySheets = Array("hello", "bye") 'Sheet names to be consolidated
    
    Set wrk = ActiveWorkbook
    Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))

    trg.Name = "ARP " 'I would not have a tab with trailing space(s)
    
    Set sht = wrk.Worksheets(1)
    
    colCount = sht.Cells(1, Columns.Count).End(xlToLeft).Column

    With trg.Cells(1, 1).Resize(1, colCount)
        .Value = sht.Cells(1, 1).Resize(1, colCount).Value
        .Font.Bold = True
    End With

    For Each sht In wrk.Worksheets
        If IsNumeric(Application.Match(sht.Name, varMySheets, 0)) Then
            Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(Rows.Count, 1).End(xlUp).Resize(, colCount))
            trg.Cells(Rows.Count, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
        End If
    Next sht

    trg.Columns.AutoFit

    Application.ScreenUpdating = True
    
End Sub

What happens if you need to merge a second time as the code will currently fail because it cannot create the tab "ARP " again?

Robert
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,454
Members
452,514
Latest member
cjkelly15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top