How to copy from a closed workbook to active workbook using vba?

Siimo

New Member
Joined
Jan 9, 2017
Messages
12
I am trying to copy data from a closed workbook from a specific sheet and from a specific range and paste it into my active workbook but into a specific sheet starting at cell B7. I have the below code however it is only returning zeros. can anyone point me in the right direction? I will be building on this code once I can get this working properly so i will essentially want it to copy a variety of ranges from different sheets form the closed workbook and paste into specific sheets and ranges etc.

The ranges from the closed workbook are also named so if i can include that it would also make it slightly quicker.

Code:
Sub RectangleRoundedCorners6_Click()Dim mydata As String
'data location & range to copy
mydata = "='C:\[1133.xlsm]Cars'!$B$5:$Y$141" '<< change as required


'link to worksheet
With ThisWorkbook.Worksheets("1133Cars").Range("B7") '<< change as required
.Formula = mydata
'convert formula to text
.Value = .Value


End With
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Note comments below in code and try:
Code:
Sub M1()
        
    Dim wkb     As Workbook
    Dim sFile   As String
    
    sFile = "C:\1133.xlsm" 'The full path should go here
    
    Application.ScreenUpdating = False
    
    Set wkb = Workbooks.Open(sFile, ReadOnly:=True)
    
    With wkb
        'Cell (7,2) = row 7, column 2, i.e. B7
        sheets("1133Cars").Cells(7, 2).Resize(137, 24).Value = .sheets("Cars").Cells(5, 2).Resize(137, 24).Value
        .Close False
    End With
    
    ThisWorkbook.Activate
    sheets("1133Cars").Select
    
    Application.ScreenUpdating = True
    
    Set wkb = Nothing
    
End Sub
 
Upvote 0
Thank you for the reply Jack, I am receiving an error on the below line.

Code:
Set wkb = Workbooks.Open(sFile, ReadOnly:=True)

I believe this is due to it not finding the destination of the file. my destination is;

sFile = "C:\Users\Siimo\Documents\Group Tracking EoTF\[1133.xls]"

Which is what I have put in but it is still not finding this apparently.
 
Last edited:
Upvote 0
Try:
Code:
sFile = "C:\Users\Siimo\Documents\Group Tracking EoTF\1133.xlsx"
It's exactly the same path as in the Windows Explorer, not sure why you're including [ and ] around the file name.
 
Upvote 0
I have fixed the issue of finding the document. It is now bringing the below up as a debug;

Code:
Sheets("1133Cars").Cells(7, 2).Resize(137, 24).Value = .Sheets("Cars").Cells(5, 2).Resize(137, 24).Value

This could be due to the sheet it is copying from is a hidden sheet? If so could this be worked around?
 
Last edited:
Upvote 0
Change that line to:
Code:
     With sheets("1133Cars")
        .Hidden = xlSheetVisible
        .Cells(7, 2).Resize(137, 24).Value = .sheets("Cars").Cells(5, 2).Resize(137, 24).Value
    End With
 
Upvote 0
I get Run Time Error 438 (Object doesn't support this property or method)

debug shows the below line;

Code:
.Hidden = xlSheetVisible
 
Upvote 0
for previous error I changed the code around

Code:
[COLOR=#333333]With sheets("Cars")[/COLOR]        .Hidden = xlSheetVisible
        .Cells(7, 2).Resize(137, 24).Value = .sheets("1133Cars").Cells(5, 2).Resize(137, 24).Value [COLOR=#333333]    End With[/COLOR]

The exact code you gave me gave me an error of out of range and debug was showing the below line;
Code:
With Sheets("1133Cars")
 
Upvote 0
Shouldn't be a problem copying from a hidden sheet, the sheet is hidden but the cells of that sheet still contain values.

Without your workbook in front of me, difficult to decipher code changes, what is going on and what is described.
 
Upvote 0
Thank you for your help I appreciate it, I will have a play around with the coding and see if I can work it out.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top