How to access cells outside a table pased to a macro?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,543
Office Version
  1. 365
Platform
  1. Windows
I am working on a macro that will process data in a table. It also needs to access data in cells in rows above the table. Here's the code so far.

VBA Code:
Sub WtdRtg()
Const rnTable As String = "Tbl"   'The name of the main table
Dim TblData
TblData = Range(rnTable).Value
End Sub

How can I access the contents of the cell that is 3 rows above the cell in TblData(2,3)?

If TblData(2,3) is cell F7, then I want the contents of F3.

I know I could assign a named range to the cells in row 3, but a named range (eg C3:H3) does not automtically expand if I add a column, whereas the table range does.

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
How can I access the contents of the cell that is 3 rows above the cell in TblData(2,3)?

If TblData(2,3) is cell F7, then I want the contents of F3.
Not exactly sure what you want as F3 is 4 rows above F7 but it could be along these lines.

VBA Code:
MsgBox Range(rnTable).Cells(2, 3).Offset(-4).Value
 
Upvote 0
Solution
Not exactly sure what you want as F3 is 4 rows above F7 but it could be along these lines.

VBA Code:
MsgBox Range(rnTable).Cells(2, 3).Offset(-4).Value
Perfect. Thank you. 👍👍

I should have thought of Offset. I always think of it as a worksheet function. I forget about it in VBA.

Sorry about the confusion about the number of rows. It was just an example.
 
Upvote 0
You're welcome. Thanks for the follow-up.
 
Upvote 0

Forum statistics

Threads
1,216,100
Messages
6,128,834
Members
449,471
Latest member
lachbee

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