Solved

Excel merge sheets

Posted on 2014-04-22
3
307 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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.

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now