Solved

Excel VBA - copying whole sheet from another workbook

Posted on 2011-02-27
13
688 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
 
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 41

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 41

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 41

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 41

Expert Comment

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

dave
0
 
LVL 41

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

747 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

12 Experts available now in Live!

Get 1:1 Help Now