Cell that includes text and table identification/referencing and for/next and data-sort

MeisterConrad

New Member
Joined
Jan 17, 2017
Messages
11
I hope you can help me; I just can't see my way through it.
I have a series of 20 identical tables (Table1, Table2, etc.), arrange from left to right on one worksheet.
Each of these tables has a merged cell containing concatenated data that spans across the top of the table. This merged cell serves as a sort of pre-header/nameplate for the table.

I'm trying to make code on the worksheet that recognizes when this nameplate cell is clicked, then the associated table gets data-sorted. But the code is to recognize when any of the nameplate cells are selected.

I recorded a macro for the data-sort operation:

VBA Code:
Sub Macro2()
'
' Macro2 Macro
' Select a Table from the RANGE ID box, then data-sort the Table.
'

'
    Application.Goto Reference:="Table2"
    ActiveWorkbook.Worksheets("LedgerTablesSheet").ListObjects("Table2").Sort. _
        SortFields.Clear
    ActiveWorkbook.Worksheets("LedgerTablesSheet").ListObjects("Table2").Sort. _
        SortFields.Add Key:=Range("Table2[Date Cleared]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("LedgerTablesSheet").ListObjects("Table2").Sort. _
        SortFields.Add Key:=Range("Table2[Trans-action Date]"), SortOn:= _
        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("LedgerTablesSheet").ListObjects("Table2").Sort. _
        SortFields.Add Key:=Range("Table2[Budget Category]"), SortOn:= _
        xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("LedgerTablesSheet").ListObjects("Table2").Sort. _
        SortFields.Add Key:=Range("Table2[SubCategory]"), SortOn:=xlSortOnValues, _
        Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("LedgerTablesSheet").ListObjects("Table2").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

The code I came up with to identify the table based on the nameplate cell being selected is:

VBA Code:
Dim TableToSort As ListObject
Dim i As Integer
For i = 1 to 20, step 1
Set TableToSort = ActiveSheet.ListObjects("Table" & i)

If ActiveCell.Text = "Table" & i Then
TableToSort = ActiveSheet.ListObjects("Table" & i)
Exit For
End If
Next i

For one problem, I need the code to recognize if the cell contains the text "Table" and the number assigned to i, forgetting about the rest of the text in the cell.
But I also need to join these two bits of code into one fell swoop. And something doesn't feel quite right.
Am I not referencing the tables properly?
I'm really trying to clean up a lot of redundant code from a previous version of this workbook, so I kinda wanted to make this a sub-routine that can also be triggered in other operations. Thanks in advance for your help.
 
Last edited by a moderator:

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Watch MrExcel Video

Forum statistics

Threads
1,127,501
Messages
5,625,174
Members
416,075
Latest member
TechJosh

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
Top