We help IT Professionals succeed at work.
Get Started

Merge Sheet from one workbook to another - assistance with existing code

79 Views
Last Modified: 2017-04-01
I aquired this code to pull in (Sheet1) from another workbook and merge it into my master workbook. Its working but..

The problem is when the tab is pulled in from WB2 it needs to be placed infront of Sheet1, its currently coming in and going to the last tab. How do you bring it in so it is 1st tab? also once it has been pulled into the master file I need to protect this sheet before saving and exit.  

Dim objExcel
    Dim objWB1
    Dim objWB2
    Dim ws
    Set objExcel = CreateObject("Excel.Application")
    objExcel.DisplayAlerts = False
    Set objWB1 = objExcel.Workbooks.Open("c:\SOURCE_TAB_Benefits.xlsm")
    Set objWB2 = objExcel.Workbooks.Open("c:\BENEFITS_Run.xlsx")
    For Each ws In objWB1.Sheets
        ws.Copy objWB2.Sheets(objWB2.Sheets1.Count)
       

Next
    objWB1.Close False
    objWB2.Save
    objWB2.Close
    With objExcel
       
        .DisplayAlerts = True
        .Quit
    End With
    Set objExcel = Nothing
    MsgBox "Done!"
Comment
Watch Question
Excel VBA Developer
CERTIFIED EXPERT
Distinguished Expert 2018
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE