Reference column names instead of numbers

Mindlesh

Board Regular
Joined
Apr 2, 2014
Messages
172
How can I adapt these macros to reference the corresponding column names in Table4?
Code:
Private Sub ToggleButton1_Click()
    [COLOR=#ff0000]' Reference a column named "Time"[/COLOR]
    ActiveSheet.Columns(3).Hidden = Not (ToggleButton1.Value)
End Sub
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
[COLOR=#ff0000]' Reference a column named "ET"[/COLOR]
If Target.Cells.Count = 1 And Target.Column = 4 Then
    ActiveSheet.Sort.SortFields.Clear
    [COLOR=#ff0000]' Reference the second cell in a column named "NT"[/COLOR]
    ActiveSheet.Sort.SortFields.Add Key:=Range("B2") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveSheet.Sort.SortFields.Add Key:=Range("A2") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    [COLOR=#ff0000]' Reference Table4[/COLOR]
    With ActiveSheet.Sort
        .SetRange Range("A:M")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End If
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You can use column letters instead of numbers (e.g. "C" instead of 3), but it's now clear what you mean by 'Reference a column named "Time"'.
If "Time" is a named range you might use:
ActiveSheet.Range("Time").Columns.Hidden or ActiveSheet.Range("Time").EntireColumn.Hidden
If "Time" is a cell value in a column, your code will need to work out what column that is (e.g. via Find).
 
Upvote 0
"Time" is the name of a column in a range that is formatted as a table named "Table4"; the sheet has multiple tables. I want to adapt the macros to refer to specific table and column names.
 
Upvote 0
Try
Code:
Range("Table4[[#All],[Time]]").Select

or
Code:
Range("Table4[[#All],[Time ]]").Select
 
Upvote 0
This works:
Code:
ActiveSheet.Range("Table4[[#All],[Time]]").Columns.Hidden = Not (ToggleButton1.Value)
What other changes should I make to use this concept across both macros?
 
Upvote 0
One way of referencing the 2nd cell in the column is

Code:
Range("Table4[[#All],[NT]]").Cells(2).Select

or another...

Code:
Range("Table4[NT]").Cells(1).Select

One way of referencing a whole table is

Code:
ActiveSheet.ListObjects("Table4").Range.Select
 
Upvote 0
Am I on the right track?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim tb As ListObject
    Set tb = ActiveSheet.ListObjects("Table4").Range.Select
    If Target.Cells.Count = 1 And Target.Column = Range("NT") Then
        tb.Sort.SortFields.Clear
        tb.Sort.SortFields.Add Key:=Range("NT").Cells(2).Select _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        tb.Sort.SortFields.Add Key:=Range("Se").Cells(2).Select _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With tb.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End If
End Sub
 
Upvote 0
Try...

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim tb As ListObject
    Set tb = ActiveSheet.ListObjects("Table4")
    If Target.Cells.Count = 1 And Target.Column = tb.ListColumns("NT").DataBodyRange.Column Then
        tb.Sort.SortFields.Clear
        tb.Sort.SortFields.Add Key:=tb.ListColumns("NT").DataBodyRange.Cells(1) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        tb.Sort.SortFields.Add Key:=tb.ListColumns("Se").DataBodyRange.Cells(1) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With tb.Sort
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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