Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-11-21
8
Medium Priority
?
48 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 34

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 34

Accepted Solution

by:
Norie earned 2000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
LVL 34

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 34

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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…

688 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