Solved

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

Posted on 2016-11-21
8
38 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 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
Independent Software Vendors: 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

749 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