Coloring the Tab using VBA Code

Sivas

New Member
Joined
Aug 29, 2018
Messages
20
Anyone help on below query,

I have set of 25 sheets in a workbook. Based on the column name in a sheet, if the value is greater than 0 in that column, that sheet tab needs to get colored.

I have the code as below,
Code:
Sub Click()
    Dim sh As Worksheet, ws As Worksheet, x As Long, i As Long, rngCIK As Range
        With ThisWorkbook
        .Sheets.Add(Before:=.Sheets(1)).Name = "Report"
        End With
    Set ws = ActiveSheet
    i = 2
    ws.Range("A1:B1").Value = Array("CIK", "Count")
        For Each sh In Sheets
        If Not sh Is ws Then
            Set rngCIK = sh.Rows(1).Find("CIK", , xlValues, xlWhole, 1, 1, 0)
            If Not rngCIK Is Nothing Then
            x = Application.CountA(rngCIK.EntireColumn) - 1
                ws.Rows(i).Range("A1:B1").Value = Array(sh.Name, x)
                i = i + 1
            End If
        End If
    Next sh
End Sub

But am not able to highlight and color the tab, which has the column value greater than 0.
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about
Code:
Sub Click()
   Dim sh As Worksheet, ws As Worksheet, x As Long, i As Long, rngCIK As Range
   With ThisWorkbook
      .Sheets.Add(Before:=.Sheets(1)).Name = "Report"
   End With
   Set ws = ActiveSheet
   i = 2
   ws.Range("A1:B1").Value = Array("CIK", "Count")
   For Each sh In Sheets
      If Not sh Is ws Then
         Set rngCIK = sh.Rows(1).find("CIK", , xlValues, xlWhole, 1, 1, 0)
         If Not rngCIK Is Nothing Then
            x = Application.CountA(rngCIK.EntireColumn) - 1
            ws.Rows(i).Range("A1:B1").Value = Array(sh.Name, x)
            i = i + 1
           [COLOR=#0000ff] If x > 0 Then sh.Tab.Color = 45678[/COLOR]
         End If
      End If
   Next sh
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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