Solved

Excel VBA - copying whole sheet from another workbook

Posted on 2011-02-27
13
725 Views
Last Modified: 2012-05-11
Hi

I am using the code shown in the code section below to copy all the data
from a sheet in another workbook. The data doesn't copy over for some reason
Sub Main()

    Dim MYDOC_DIR As String
  
    On Error GoTo EH
    
    MYDOC_DIR = Environ("userprofile") & "\Desktop"
    Dim oWb As Excel.Workbook
    Set oWb = Application.Workbooks.Open(MYDOC_DIR & "\" & "FILE FROM ITS.xlsm")
   
    oWb.Sheets(1).Cells.Copy
    
    Sheets("Sheet1").Select
    Cells.Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
    oWb.Close (False)
    Set oWb = Nothing
    
    Exit Sub
EH:
    MsgBox Err.Description

End Sub

Open in new window

0
Comment
Question by:murbro
  • 6
  • 5
  • 2
13 Comments
 
LVL 10

Expert Comment

by:cdebel
ID: 34992926
Does it hang and give you an error message?
0
 

Author Comment

by:murbro
ID: 34992949
no error message - just data not being copied across. Thanks
0
 
LVL 10

Expert Comment

by:cdebel
ID: 34992958
I think the error come from that once you open the workbook, your initial workbook is not the activeworkbook anymore... (so from line 13 to 16, you should refer to ThisWorkbook
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 10

Expert Comment

by:cdebel
ID: 34992963
There...    Before line 13, just do ThisWorkbook.Activate.

It will select your initial workbook, then your "Select" on line 13 will apply to this workbook, and not the one that you have just opened...

    ThisWorkbook.Activate
    Sheets("Sheet1").Select
    Cells.Select
    ActiveSheet.Paste
    Application.CutCopyMode = False

Open in new window

0
 
LVL 42

Expert Comment

by:dlmille
ID: 34993039
cdebel - I don't believe your wode will work either - after the Sheets("Sheet1").select, Sheets("Sheet1") becomes the ActiveSheet does it not?

Should it not be

ThisWorkbook.Activate
Sheets("sheet1").cells.copy
ThisWorkbook.Paste
Application.CutCopyMode = False


Dave
0
 
LVL 10

Expert Comment

by:cdebel
ID: 34993050
@dlmile:
Yes it become active sheet.
What he seems to want is to copy data from Sheet1 (in oWB) into ThisWorkbook.Sheets(1)...
So my code does work.

Yours doesn't work... (i'm refering to this line: ThisWorkbook.Paste)

0
 
LVL 10

Expert Comment

by:cdebel
ID: 34993060
@dlmile: also, in your solution, you seems to copy data FROM THISWORKBOOK, while it should be copied TO THISWORKBOOK.

At least, this is what his code says there: oWb.Sheets(1).Cells.Copy

So we should let him see if i got it wrong.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34993061
The below code is corrected.  I just tested with test data

dave
Sub Main()
Dim myCurrent As Workbook

    Dim MYDOC_DIR As String
  
    On Error GoTo EH
    
    Set myCurrent = ActiveWorkbook
    
    MYDOC_DIR = Environ("userprofile") & "\Desktop"
    Dim oWb As Excel.Workbook
    Set oWb = Application.Workbooks.Open(MYDOC_DIR & "\" & "FILE FROM ITS.xlsm")
   
    oWb.Sheets(1).Cells.Copy
    
    myCurrent.Activate
    Sheets("Sheet1").Select
    Cells.Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    
    oWb.Close (False)
    Set oWb = Nothing
    
    Exit Sub
EH:
    MsgBox Err.Description

End Sub

Open in new window

0
 
LVL 42

Expert Comment

by:dlmille
ID: 34993070
@Cdebel - agreed.

Please try cdebel's first.  

otherwise, try mine.

Dave
0
 
LVL 10

Accepted Solution

by:
cdebel earned 500 total points
ID: 34993072
@dlmile:
So basically, this is exactly what i did previously.

The only thing you add is an unnecessary line (line 8)... because ThisWorkbook always refer to ThisWorkbook (where the VBA code is).  So there's no need to remember which workbook it is to activate it back later...

Let's hope that the askers will notice that i gave him his answer.
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34993073
Yes - Cdebels does work.  Don't give me points.  Use his solution

dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 34993085
@Cdebel - sorry about that.  I almost never use ThisWorkbook because I didn't realize the truth to your comment (This Workbook will allways be where the source code is).  Particularly, because most of my stuff ends up in addins :)

Also, in my original comment, I didn't snap to the realization the COPY had already been done before your code (your snippet was just short enough for me to miss that, for some reason) - otherwise I wouldn't have posted.


Thanks for the lesson!

Dave
0
 

Author Closing Comment

by:murbro
ID: 34996030
Hi. Thanks very much
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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…
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…

821 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