Refer to column name instead of "column number"

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
Is it possible to refer to a column in a table with something like Columns("Name").Value instead of Cells(n,m)?
 
Yes, that should solve the problem.

Remark: you didn't declare the variable and you gave it a meaningless name. Both are bad practice.

For ex.

Code:
...
Dim lRow As Long
 ...
lRow = objNewRow.Index
...

Understood, and thanks for the tip; lazy sunday ;)
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
As sort of a side-bar to the original post, the solution:

Code:
Sub test()
Dim lstObj As ListObject
Dim rColumn As Range
 
' Get the table reference
Set lstObj = Worksheets("Sheet1").ListObjects("Table1")
 
' Get the column reference
Set rColumn = lstObj.ListColumns("Field1").DataBodyRange
 
' display first data value in the column
MsgBox rColumn(1).Value
 
End Sub

only works if there is data in column "Field1" of the listobject. How can the column be found if there is no data in it?
 
Last edited:
Upvote 0
Ok, just to close my own loop, use:
Code:
lstObj.ListColumns("Field1").Range.Column
to find the column number of "Field1" when the table is empty.

When the table is empty, there is no DataBodyRange in any column, so that property is null.
 
Upvote 0

Forum statistics

Threads
1,222,629
Messages
6,167,190
Members
452,104
Latest member
jadethejade

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