VBA code for pulling data from a closed workbook w/o opening it

nadaraza

New Member
Joined
Jul 6, 2012
Messages
20
Hi,

Looking to pull the data in one cell from a closed workbook say A1, without actually opening the workbook or prompting excel to open and close it. Ideally if it can be live, change as it changes in the (closed) workbook without it needing to be opened ...otherwise just pull in the information from that cell and I'll just add an update button so it refreshes on command.

Thanks,
Nada
 
Thank you for your quick reply. I created a new Workbook with data in a couple of random cells, adjusted the code to the new workbook, sheet and cells and it worked! What I have discovered is the original workbook "Mar 2015" is password protected (forgot about that). Anyway, how do I address the password situation?
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
AlphaFrog, thank you for your quick reply! And ignore my previous post. A password had nothing to do with my problem.

I did get it to run! I think my problem was in the strPath which included "C:\Users\Ron's-PC\...... ". I placed my workbooks at "C:\MMS\" and it worked. I'm wondering about the apostrophe.
 
Last edited:
Upvote 0
AlphaFrog, thank you for your quick reply! And ignore my previous post. A password had nothing to do with my problem.

I did get it to run! I think my problem was in the strPath which included "C:\Users\Ron's-PC\...... ". I placed my workbooks at "C:\MMS\" and it worked. I'm wondering about the apostrophe.

The apostrophe (aka: single quote) is an issue. Replace one literal apostrophe with two apostrophes.

Code:
strPath = Replace(ActiveWorkbook.path & "\", "[COLOR=#ff0000]'[/COLOR]","[COLOR=#ff0000]'[/COLOR][COLOR=#800080]'[/COLOR]")
 
Last edited:
Upvote 0
Try something like this...

Code:
    strPath = "C:\Test\"
    strFile = "Book1.xls"
    strSheet = "Sheet1"
    strRng = Range("A1").Address(1, 1, xlR1C1)
    
    strRef = "'" & strPath & "[" & strFile & "]" & strSheet & "'!" & strRng
    
    Result = ExecuteExcel4Macro(strRef)


Alphafrog, Here is what I am doing currently:

Code:
Dim sourcedoc
Dim oExcel As Excel.Application
Dim oWB As Workbook
Set oExcel = New Excel.Application
Application.ScreenUpdating = False
Set sourcedoc = oExcel.Workbooks.Open(FileName:="blahblahblha"\CSlistmrl.xlsx")
arLOB = Range("L2:L9")
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
Set oExcel = Nothing
Set oWB = Nothing

So am I to understand that I could do the same thing without ever actually opening the file and still obtain the data to use in vba? I believe I already understand the benefits of this, but could you cover any pros and cons of doing it your way vs. mine?

Thanks a bunch!
 
Upvote 0
Hi everyone,

The code below worked from me, but i need to add formulas so that only specified data gets pulled.
Where can I include formulas to replace Sheet1'!$B$2:$F$12

Sub GetDataFromClosedBook()
Dim mydata As String
'data location & range to copy
mydata = "='C:\[Newbook.xls]Sheet1'!$B$2:$F$12" '<< change as required

'link to worksheet
With ThisWorkbook.Worksheets(1).Range("B2:F12") '<< change as required
.Formula = mydata
'convert formula to text
.Value = .Value

End With
End Sub
 
Upvote 0
Hello,
I wanted to create a VBA that will pull the values from a closed workbook into the open workbook and from certain ranges that are non-contiguous. The worksheets are identical in layout. For example, I wanted to copy whatever values were in cells D10, D16:D26, D31:D41 (and the same row/cells in columns J, P, and V) from SHEETx in FILEx, and have the values pulled into the exact same cells/rows into SHEETx in FILEy. I would need the user to be prompted to find FILEx when the macro button is pushed so it knows which file the data is coming from, and it could be located in a different directory. Any experts out there?
 
Upvote 0
another version:

Hi, how do I use the below code with the addition of lrow (as the last row may change) but I know the columns with data that need to be pulled are from column A to column S - please see my failed attempt in red:


Sub GetDataFromClosedBook()
Dim mydata As String
'data location & range to copy
mydata = "='C:\[Newbook.xls]Sheet1'!$A:$S"

lrow = "='C:\[Newbook.xls]Sheet1'!$A$1:$S$1".End(xldown).Row

For I = 2 to lrow

'link to worksheet
With ThisWorkbook.Worksheets(1).Range(("A" & I):("S" & I))
.Formula = mydata
'convert formula to text
.Value = .Value

End With

Next I

End Sub

Also will this macro automatically update when the data in mydata is changed and saved?

Many Thanks,

Jadde
 
Upvote 0
You wouldn't need VBA to put the value in a cell. You can put the link directly in the cell as a formula. I only provided the VBA solution because that's what you had asked for.

In the destination cell put something like this.
='C:\Test\[Book1.xls]Sheet1'!A1

This works perfectly.

But what if I want to import all the data, for instance, 10000 cells? Or better yet, what if I want to import all the data in the cells but I don't know exactly how many rows and columns there are?

How could this be done?

Thank you
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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