Solved

Opening second Excel workbook and copying contents of worksheet back to first workbook

Posted on 2016-11-21
8
20 Views
Last Modified: 2016-11-21
I always forget how to do this.

I have a project workbook, "wb1.xlsm" and 3 source workbooks, s1.xls, s2.xls and s3.xls.

In wb1.xlm there are 3 destination worksheets names "d1", "d2", & "d3".  In workbooks s1, s2 and s3 there is only one sheet in each.

I want to open each of the source workbooks in order.
Read the contents of cell "G1" and determine based on the value in the cell if the whole sheet needs to be pasted back in "wb1.xlsm" to sheet "d1", "d2" or "d3"

Can someone help me with this?

Thanks

Glen
0
Comment
Question by:GPSPOW
  • 4
  • 4
8 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 41896389
Glen

Should be quite straightforward.

One bit missing though, how does the value in G1 determine which sheet the data should goto?

In the meantime here's some code to open the workbooks in question.
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim arrWBs As Variant 
Dim I As Long

    arrWBs = Array("s1.xls", "s2.xls", "s3.xls.")

    For I = LBound(arrWBs) to UBound(arrWBs)
        Set wbSrc = Workbooks.Open(arrWBs(I))    ' probably need to include a path here
        MsgBox wbSrc.Sheets(1).Range("G1").Value
        ' code to copy data to relevant sheet
        wbSrc.Close SaveChanges:=False 
    Next I

Open in new window

0
 

Author Comment

by:GPSPOW
ID: 41896399
The path will always be "C:\Path\"

Cell "G1" has the subject of the data.  If the value = "67-67104 PMC Nonexempt" then the data is pasted to "sheet1".

If the value ="67-67104 PMC OvtHrs" then the data is pasted to "sheet2".
If the value = '67-67104 PMC ExHrs" then the data is pasted to "sheet3"

How do we tell it paste to the right place?

Thanks

Glen
0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 41896450
Glen

Try this.
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim wsDst As Worksheet
Dim arrWBs As Variant 
Dim I As Long

    arrWBs = Array("s1.xls", "s2.xls", "s3.xls.")

    For I = LBound(arrWBs) to UBound(arrWBs)

        Set wbSrc = Workbooks.Open("C:\Path\" & arrWBs(I))  

        Select Case wbSrc.Sheets(1).Range("G1").Value
            Case "67-67104 PMC Nonexempt"
                Set wsDst = wbDst.Sheets("Sheet1") 
            Case "67-67104 PMC OvtHrs" 
                Set wsDst = wbDst.Sheets("Sheet2") 
            Case "67-67104 PMC ExHrs"
                Set wsDst = wbDst.Sheets("Sheet3") 
        End Select

        If Not ws Is Nothing Then
            wbSrc.Sheets(1).UsedRange.Copy wsDst.Range("A1")
        End If

        wbSrc.Close SaveChanges:=False 

    Next I

Open in new window

0
 

Author Comment

by:GPSPOW
ID: 41896516
I am getting a runtime error 91 :  Object Variable or With Block Variable not set for the following:

Sub Workbook_Open()
    Dim wbDst As Workbook
    Dim wbSrc As Workbook
    Dim wsDst As Worksheet
    Dim arrWBs As Variant
    Dim I As Long

    arrWBs = Array("Kronos_1.xls", "Kronos_2.xls", "Kronos_3.xls")

    For I = LBound(arrWBs) To UBound(arrWBs)

        Set wbSrc = Workbooks.Open("C:\Path\" & arrWBs(I))

        Select Case wbSrc.Sheets(1).Range("G1").Value
            Case "67-67104 PMC Nonexempt"
                Set wsDst = wbDst.Sheets("NonExHrs")
            Case "67-67104 PMC StL PMC"
                Set wsDst = wbDst.Sheets("OvtHrs")
            Case "67-67104 PMC Exempt"
                Set wsDst = wbDst.Sheets("SalHrs")
        End Select

        If Not wbSrc Is Nothing Then
            wbSrc.Sheets(1).UsedRange.Copy wsDst.Range("A1")
        End If

        wbSrc.Close SaveChanges:=False

    Next I

End Sub


The debug highlights :

        If Not wbSrc Is Nothing Then
            wbSrc.Sheets(1).UsedRange.Copy wsDst.Range("A1")
        End If


Any suggestions?
thanks

Glen
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 33

Expert Comment

by:Norie
ID: 41896565
Oops, checked the wrong thing for 'Nothing'.

It should be this.
If Not wsDst Is Nothing Then
    wbSrc.Sheets(1).UsedRange.Copy wsDst.Range("A1")
End If

Open in new window

Mind you that does bring up another possible problem.

The error you were getting was being caused by there not being a match In the Select Case statement.

Are you sure '67-67104 PMC Nonexempt' etc are the exact values to look for in G1?

No leading/trailing spaces/spelling mistakes?
0
 

Author Comment

by:GPSPOW
ID: 41896592
Now it is not recognizing the wbDst.sheets reference.

Set wsDst = wbDst.Sheets("OvtHrs")


 Where do you set the value for wbDst?  This is the workbook that has the VBA code in it.

thanks

Glen
0
 

Author Closing Comment

by:GPSPOW
ID: 41896597
I added Set wbDst = ActiveWorkbook.

It works perfectly.

Thank you

Glen
0
 
LVL 33

Expert Comment

by:Norie
ID: 41896631
Oops, another basic mistake.

I'm posting from my tablet so I'll blame it on that.:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 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

20 Experts available now in Live!

Get 1:1 Help Now