Solved

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

Posted on 2016-11-21
8
30 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
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.

 
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

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

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…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

920 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

16 Experts available now in Live!

Get 1:1 Help Now