Determine which number in a table a column is

mst3k4L

Board Regular
Joined
Nov 3, 2011
Messages
55
I'm not used to programming with tables, so my apologies if this is really simple.

So I originally needed to highlight certain cells in a row, but not the whole row. The code looked something like this.

Code:
Sub QC()

    Dim row As Range

    For Each row In [CAPEX].Rows          
                row.Columns(row.ListObject.ListColumns("Program name").Index).Interior.Color = 49407
'               * a whole lot more lines here which i deleted for this post*
                row.Columns(row.ListObject.ListColumns("Total Forecast").Index).Interior.Color = 49407
    Next
End Sub

But I soon realized that I could just do this instead

Code:
Sub QC()

    Dim row As Range

    For Each row In [CAPEX].Rows          
                        For i = 2 To 12 Step 1
                row.Columns(row.ListObject.ListColumns(i).Index).Interior.Color = 49407
            Next i
    Next
End Sub

A thought occured to be though. There's a chance that someone might add an extra column between column "Program name" and column "Total Forecast", and I would still like all the cells to be highlighted. I was thinking of something like this, but I have no idea how to do that.
Code:
Sub QC()

    Dim Program_name_column As Int
    Dim Total_Forecast_column As Int
    Dim row As Range

    For Each row In [CAPEX].Rows   

            Program_name_column = ?
            Total_Forecast_column = ?   
    
            For i = Program_name_column To Total_Forecast_column Step 1
                row.Columns(row.ListObject.ListColumns(i).Index).Interior.Color = 49407
            Next i
    Next
End Sub

So how would I go about setting those two variables dynamically in the program? Thank in advance.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If you just need the column, am assuming for example that its on row 1, I would do something like

Code:
Range("A1:XFD1").Find("program name").Column
 
Upvote 0
If you just need the column, am assuming for example that its on row 1, I would do something like

Code:
Range("A1:XFD1").Find("program name").Column

That worked. Thanks! Incase the table no longer starts on row 1, how would you just say to start with the first row in the Table?
 
Upvote 0
Why do you loop through the rows? You are not testing if a row meets some criteria so why not change all the rows at once?

Code:
dim tbl as listobject
dim cols as long

set tbl =[capex].cells(1).listobject

with tbl
   cols = .listcolumns("Total Forecast").index - .listcolumns("Program name").index + 1
   .listcolumns("Program name").databodyrange(1,1).resize(.listrows.count,cols).interior.color = 49407
end with
 
Upvote 0
Hi

Assuming the table in the active sheet, try also:

Code:
Sub Test()
Dim lstobj As ListObject
Dim r As Range

Set lstobj = ActiveSheet.ListObjects("Capex")

Set r = Range(lstobj.ListColumns("Program Name").Range, lstobj.ListColumns("Total Forecast").Range)

r.Interior.Color = 49407

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

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