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

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,535
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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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,215,492
Messages
6,125,116
Members
449,206
Latest member
burgsrus

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