Replace cell address with Table Header reference

gberg

Board Regular
Joined
Jul 16, 2014
Messages
209
Office Version
  1. 365
Platform
  1. Windows
I have this code that I want to replace the "D2" cell reference with the header name of the table

With Cell Reference
VBA Code:
Range(("D2"), Range("D2").End(xlToRight)).Select

I want something like this, but this gives me an error code
VBA Code:
Range("TBL_Jan_JPR[[#Headers],[ACCEPTED CONTRACT]]", Range("TBL_Jan_JPR[[#Headers],[ACCEPTED CONTRACT]]").End(xlToRight)).Select
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I have this code that I want to replace the "D2" cell reference with the header name of the table

With Cell Reference
VBA Code:
Range(("D2"), Range("D2").End(xlToRight)).Select

I want something like this, but this gives me an error code
VBA Code:
Range("TBL_Jan_JPR[[#Headers],[ACCEPTED CONTRACT]]", Range("TBL_Jan_JPR[[#Headers],[ACCEPTED CONTRACT]]").End(xlToRight)).Select
Hi gberg,

It's best to record a macro to see what the code should look like. However, I use code in some of my macros that reference Table Headers.

Try these different selection options. The first three are a range of contiguous Table Columns, while the last one is using a single Table Column. Just change my table name (t_Books) to your table name (TBL_Jan_JPR) and then in the VB Editor, use the F8 key to step through each selection. The last one will likely be what you want.

VBA Code:
Sub Select_Table_Range()

  '#All Selects Table Headers, DatabodyRange and Totals row for specified range of Table columns
  Range("t_Books[[#All],[Title]:[Author]]").Select

  '#Headers selects Headers for specified range of Table columns
  Range("t_Books[[#Headers],[Title]:[Author]]").Select

  'Selects the DatabodyRange only for chosen Headers
  Range("t_Books[[Title]:[Author]]").Select
 
  'Selects a Single Header
  Range("t_Books[[#Headers],[Title]]").Select
 
End Sub
 
Upvote 0
Please try the following (only slight change from what you had)

VBA Code:
Range(Range("TBL_Jan_JPR[[#Headers],[ACCEPTED CONTRACT]]"), Range("TBL_Jan_JPR[[#Headers],[ACCEPTED CONTRACT]]").End(xlToRight)).Select
 
Upvote 0
Solution

Forum statistics

Threads
1,226,114
Messages
6,189,052
Members
453,522
Latest member
Seeker2025

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