Solved

Excel merge sheets

Posted on 2014-04-22
3
309 Views
Last Modified: 2014-04-22
Hello,
Can you please help,
I used below code to merge excel sheets.

- I need to have a pop box to ask me which sheets to merge together.
- The new created sheet to be named as the 2 sheets merged (Example Sheet1 & Sheet2)
- Delete the merged sheets.

Option Explicit
Sub Merge_Sheets()
Sheets.Add After:=Sheets(Sheets.Count)
   Dim wks As Worksheet
   Set wks = Sheets(Sheets.Count)

   wks.Name = "Sheet1 & Sheet2"
   
   With Sheets("Sheet1")
    Dim lastrow As Long
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("A1:O" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp)
   End With

   With Sheets("Sheet2")
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("A2:O" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp).Offset(1)
   End With
   
Application.DisplayAlerts = False
Worksheets("Sheet1").delete
Worksheets("Sheet2").delete

End Sub

any help is appreciated.
Thank you
0
Comment
Question by:W.E.B
  • 2
3 Comments
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40015579
Hi,

pls try

Option Explicit
Sub Merge_Sheets()
Dim ResultSheet1 As String
Dim ResultSheet2 As String
Dim OrigWks1 As Worksheet
Dim OrigWks2 As Worksheet
Sheets.Add after:=Sheets(Sheets.Count)
   Dim wks As Worksheet
   
   ResultSheet1 = InputBox("What is the first sheet to merge?")
   
   ResultSheet2 = InputBox("What is the second sheet to merge?")
   
   On Error Resume Next
   Set OrigWks1 = ActiveWorkbook.Sheets(ResultSheet1)
   Set OrigWks2 = ActiveWorkbook.Sheets(ResultSheet2)
   On Error Goto 0

   If OrigWks1 Is Nothing Or OrigWks2 Is Nothing Then
        MsgBox "An error occured", vbOKOnly
        Exit Sub
   End If
   
   Set wks = Sheets.Add(after:=Sheets(Sheets.Count))

   wks.Name = ResultSheet1 & " & " & ResultSheet2
   
   With OrigWks1
    Dim lastrow As Long
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("A1:O" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp)
   End With

   With OrigWks2
    lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("A2:O" & lastrow).Copy wks.Range("A" & wks.Rows.Count).End(xlUp).Offset(1)
   End With
   
Application.DisplayAlerts = False
OrigWks1.Delete
OrigWks2.Delete
Application.DisplayAlerts = True
End Sub

Open in new window

0
 

Author Comment

by:W.E.B
ID: 40015691
Hello,
thank you for your help.

it is working, however, it is creating 2 sheets.
one with the merged sheets,
and another empty sheet.

I guess you can't get the pop up message's  for me to enter first sheet and second sheet to merge at same time.

thanks
0
 

Author Closing Comment

by:W.E.B
ID: 40016104
Thank you very much
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question