Referencing cell in another workbook

DJhuffman

New Member
Joined
Apr 16, 2019
Messages
25
First, I want to thank all the awesome people who have helped me so far. I've managed to streamline my tasks significantly with the VBA help that the people on this forum have provided. Thank you so much!

Now, my current bit of tinkering. I use a single file that has my macros programmed into it ("Macro storage file"). I open it, then I open a separate workbook that I run the macros in and update my data (we'll call it "520 Data"). I'd like to use a cell in Macro Storage File (B4) to generate a value in a 520 Data Worksheet (Test).

So far, this is what I cobbled together:
Code:
Sub Variable()


Dim wkb As Excel.Workbook
Dim wks As Excel.Worksheet


Set wkb = Excel.Workbooks("Macro storage file.xlsx")
Set wks = wkb.Worksheets("MAIN")


CM = wks.Range("B4")


Worksheets("Test").Range("B3").Value = CM


End Sub

However, attempting to run this generates a "Subscript out of range" error for the Set wkb line. I'm not certain if I'm going about this the wrong way or not, so any feedback would be appreciated.
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
How about
Code:
Sub DJhuffman()
   Dim Wbk As Workbook
   Dim wks As Worksheet
   
   Set Wbk = Workbooks("520 Data.xlsx")
   Set wks = ThisWorkbook.Worksheets("MAIN")
   
   cM = wks.Range("B4")
   
   Wbk.Worksheets("Test").Range("B3").Value = cM
End Sub
 
Upvote 0
Ok. That seems to be getting somewhere, but then the code choked on the last line. Still, I was able to use what you gave me to build the following:
Code:
Sub DJhuffman()
   Dim wks As Worksheet

   Set wks = ThisWorkbook.Worksheets("MAIN")

   cM = wks.Range("B4")


ActiveWorkbook.Worksheets("Test").Range("B3").Value = cM


End Sub

I didn't know about the ThisWorkbook command, which lets me ignore the Wbk command altogether.
This seems to do the task I was hoping to accomplish. I'll need to test it a little more, but the initial results are very promising. Thank you very much!
 
Upvote 0
but then the code choked on the last line.

Are you sure that 520 Data.xlsx is the ActiveWorkbook when the code runs?
Code:
[COLOR="#FF0000"]ActiveWorkbook.[/COLOR]Worksheets("Test").Range("B3").Value = cM
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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