VBA Newbie - linking to other sheets...

Patience

Active Member
Joined
Mar 30, 2006
Messages
275
Hey y'all. I am working my way through Excel VBA in Easy Steps by Ed Robinson, and the amazing thing is, some of it is sticking in my head. The question I have is, in the following simple test code, what do I need to add to make the 't' (the string) something from a different worksheet, for the sake of argument, Book1Sheet1CellA1? What are the letters needed in place of "t = Range("A1")"

Thanks in advance. I can't find it in the book.

Code:
Private Sub CommandButton1_Click()
Dim t As String
t = Range("a1")
MsgBox t
End Sub
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hi Patience

You need to prefix the Range with the source workbook/worksheet:

Code:
Private Sub CommandButton1_Click() 
Dim t As String 
t = Workbooks("Book1.xls").Sheets("Sheet1").Range("a1") 
MsgBox t 
End Sub

Check the naming on the source, as it will only end in .xls if it has been already saved.
 

Watch MrExcel Video

Forum statistics

Threads
1,112,799
Messages
5,542,579
Members
410,561
Latest member
Sasha Lawrence
Top