VBA Highlight sub total row within table

CD2017

New Member
Joined
Feb 21, 2017
Messages
33
Hello, I want to highlight the subtotal and total rows within a number of tables using VBA.

Each table has different numbers of rows and columns.

For example:
In table 1, the subtotal rows are: B23:J23, B35:J35, B39:J39, B44:J44, B51:J51.
The total row is B52:J52.
In table 2, the subtotal rows are B9:K9, B11:K11, B13:K13, B15:J16.
The total row is B16:K16.

The rows and columns will change for each table.

I have the below code which will highlight the entire row, but I only want to highlight the information within the table.

So for table 1, the whole table runs from cells B7:J52.

I want each of these ranges to be highlighted B23:J23, B35:J35, B39:J39, B44:J44, B51:J51. But I don't want to have to specify the range because it will change for each table.

Also the whole row may have blanks in it so I can't use xlright.

I'm looking for an efficient way to do it. I'm not sure if current region can be incorporate so that excel knows when the word subtotal is present, then highlight to the end of the row within the current region.

Any help would be appreciated!

Thanks.

Sub FormatTotalRows()
Dim rCell As Range
For Each rCell In Sheets("TrialBalancePS").Range("B7:J100")

If Right(rCell.Value, 5) = "Total" Then
Rows(rCell.Row).Interior.ColorIndex = 36
End If
If Right(rCell.Value, 11) = "Grand Total" Then
Rows(rCell.Row).Interior.ColorIndex = 44
End If

Next

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Welcome to the board. Try:
Code:
Sub FormatTotalRows1()

    Dim x   As Long
    
    Application.ScreenUpdating = False
    
    With Sheets("TrialBalancePS")
        On Error Resume Next: .AutoFilterMode = False: On Error GoTo 0
        
        x = .Cells(.Rows.Count, 10).End(xlUp).row
        
        With .Cells(7, 2).Resize(x - 6, 10)
            .AutoFilter field:=3, Criteria1:="Total"
            .SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 36
            
            .AutoFilter
            .AutoFilter field:=10, Criteria1:="Grand Total"
            .SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 44
        End With
    End With
    
    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
Thanks JackDanIce - I used your idea of using autofilter and applied the following code:

Sub Format_Totals()
ActiveSheet.AutoFilterMode = False

sheetlist = Array("CashSummaryPS", "ChangesInvCapCommitmentPS", _
"InvestmentRevalPS", "InvScheduleSummaryPS", "InvestorCapAcctITDPS", _
"InvestorCapAcctQTDPS", "TrialBalancePS")
For i = LBound(sheetlist) To UBound(sheetlist)
Worksheets(sheetlist(i)).Activate

With Range("B7").CurrentRegion

Range("B7").Autofilter _
field:=1, _
Criteria1:="*Total*", _
VisibleDropDown:=False

.SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 36
Range("B7").Autofilter _
field:=1, _
Criteria1:="Grand Total", _
VisibleDropDown:=False

.SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 44
Range("B7").Autofilter
End With
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,750
Messages
6,126,665
Members
449,326
Latest member
asp123

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