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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
It is Cells(1) because DataBodyRange refers to the Data excluding the header, amend it if necessary.

On another point, the code (because you are using Columns.Count) will trigger if a cell outside the table, in the same column is changed.

If you want it restricted to the table then 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 Then
        If Not Intersect(Target, tb.ListColumns("NT").DataBodyRange) Is Nothing 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 If
End Sub
 
Upvote 0
So many considerations! Thank you; and the sorting macro is noticably faster than what I had originally, for some reason.
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

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