Forum Discussion

PMHunt1955's avatar
PMHunt1955
Brass Contributor
May 22, 2019
Solved

Setting a cell's value and background colour through VBA

I have a workbook with 93 'key' worksheets. The background for each of the tabs is one of nine colours. I am now trying to make the contents of a sequence of cells on another worksheet, within the sa...
  • PMHunt1955's avatar
    May 23, 2019

    PMHunt1955 

     

    Matter is now resolved through use of a Worksheet_Activate sub-routine.

     

    Code =

     

    Private Sub Worksheet_Activate()

    Dim wb As Workbook
    Dim ws() As Worksheet
    Dim rng As Range
    Dim rngNow As Range
    Dim wsCount As Integer
    Dim wsColor() As String
    Dim wsPrime As Worksheet

    Dim I As Long

    Set wb = ThisWorkbook
    Set wsPrime = wb.Worksheets("Coverage_Statistics") 'Targets Coverage_Statistics for placement of Table of Contents

    wsCount = wb.Names("Main_Families_Count").RefersToRange.Value

    Set rng = wsPrime.Range("A1:B" & wsCount)
    ReDim ws(1 To wsCount)
    ReDim wsColor(1 To wsCount)

    For I = 1 To wsCount

    Set ws(I) = Worksheets(I)
    wsColor(I) = ws(I).Tab.Color
    Set rngNow = rng(I + 1, 1)
    rngNow.Interior.Color = wsColor(I)
    rngNow = ws(I).Name

    Next I

    End Sub

Resources