SOLVED
Home

Setting a cell's value and background colour through VBA

%3CLINGO-SUB%20id%3D%22lingo-sub-632808%22%20slang%3D%22en-US%22%3ESetting%20a%20cell's%20value%20and%20background%20colour%20through%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-632808%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20a%20workbook%20with%2093%20'key'%20worksheets.%20The%20background%20for%20each%20of%20the%20tabs%20is%20one%20of%20nine%20colours.%20I%20am%20now%20trying%20to%20make%20the%20contents%20of%20a%20sequence%20of%20cells%20on%20another%20worksheet%2C%20within%20the%20same%20workbook%2C%20the%20name%20of%20one%20of%20the%20worksheets%2C%20in%20succession%2C%20and%20colour%20the%20cell%20with%20the%20same%20background%20colour%20as%20the%20tab%20from%20which%20the%20cell's%20value%20has%20been%20drawn.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20successfully%20used%20the%20following%20function%20to%20make%20the%20cell%20values%20the%20required%20value%20in%20each%20cell%2C%20obviously%20the%20function%20is%20called%20from%20each%20cell.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CU%3ESetting%20a%20Cell's%20Value%3C%2FU%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFunction%20Other_Worksheet_Name_GSU(WS_Number%20As%20Long)%3C%2FP%3E%3CP%3EDim%20fWS_Number%20As%20Long%3C%2FP%3E%3CP%3EfWS_Number%20%3D%20WS_Number%3C%2FP%3E%3CP%3EOther_Worksheet_Name_GSU%20%3D%20Sheets(fWS_Number).Name%3C%2FP%3E%3CP%3EEnd%20Function%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20then%20successfully%20used%20the%20following%20function%20to%20retrieve%20the%20tab%20background%20colour.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CU%3EGetting%20the%20background%20colour%20of%20a%20tab%3C%2FU%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFunction%20Get_Tab_Colour(WS_Number%20As%20Long)%3C%2FP%3E%3CP%3EDim%20fWS_Number%20As%20Long%3C%2FP%3E%3CP%3EfWS_Number%20%3D%20WS_Number%3C%2FP%3E%3CP%3EGet_Tab_Colour%20%3D%20Sheets(fWS_Number).Tab.Color%3C%2FP%3E%3CP%3EEnd%20Function%3C%2FP%3E%3CP%3EThat%20function%20returns%20single%20numeric%20values%20such%20as%20-%2013395711%2C%20255%2C%2042495%2C%2065535%2C%2065280%2C%2016776960%2C%2015119490.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20then%20tried%20to%20combine%20the%20two%20into%20one%20function%2C%20and%20that%20is%20where%20I%20have%20hit%20problems.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%20I%20have%20written%20is%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFunction%20Set_GSU_Value_and_Format(targetCell%20As%20String%2C%20WS_Number%20As%20Long)%3C%2FP%3E%3CP%3EDim%20ftargetCell%20As%20String%3CBR%20%2F%3EDim%20fWS_Number%20As%20Long%3CBR%20%2F%3EDim%20fTab_Colour%20As%20Long%3C%2FP%3E%3CP%3EftargetCell%20%3D%20targetCell%3C%2FP%3E%3CP%3EfWS_Number%20%3D%20WS_Number%3C%2FP%3E%3CP%3EfTab_Colour%20%3D%20Sheets(fWS_Number).Tab.Color%3C%2FP%3E%3CP%3ERange(ftargetCell).Interior.Color%20%3D%20fTab_Colour%3C%2FP%3E%3CP%3ESet_GSU_Value_and_Format%20%3D%20Sheets(fWS_Number).Name%3C%2FP%3E%3CP%3EEnd%20Function%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20call%20the%20function%20using%20the%20following%20call%20-%3C%2FP%3E%3CP%3E'%3DSet_GSU_Value_and_Format((ADDRESS((ROW())%2C(COLUMN())%2C4))%2C(ROW()-1))%3C%2FP%3E%3CP%3Ewhich%20gets%20sent%20to%20the%20VBA%20as%20Set_GSU_Value_and_Format(A2%2C1).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20tried%20to%20just%20set%20a%20cell's%20colour%20using%20similar%20code%20as%20follows%20-%3C%2FP%3E%3CP%3EFunction%20Set_Cell_Colour(targetCell%20As%20String%2C%20WS_Number%20As%20Long)%3C%2FP%3E%3CP%3EDim%20fWS_Number%20As%20Long%3CBR%20%2F%3EDim%20ftargetCell%20As%20String%3CBR%20%2F%3EDim%20fTab_Colour%20As%20Long%3C%2FP%3E%3CP%3EftargetCell%20%3D%20targetCell%3C%2FP%3E%3CP%3EfWS_Number%20%3D%20WS_Number%3C%2FP%3E%3CP%3EfTab_Colour%20%3D%20Sheets(fWS_Number).Tab.Color%3C%2FP%3E%3CP%3ERange(ftargetCell).Interior.Color%20%3D%20fTab_Colour%3C%2FP%3E%3CP%3EEnd%20Function%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20tried%20both%20the%20following%20calls%20-%3C%2FP%3E%3CP%3E%3CEM%3EFormula%20based%20approach%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3DSet_Cell_Colour((ADDRESS((ROW())%2C(COLUMN())%2C4))%2C(ROW()-1))%3C%2FP%3E%3CP%3Ewhich%20ends%20up%20sending%20Set_Cell_Colour(A2%2C1)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3ELiteral%20approach%3C%2FEM%3E%3C%2FP%3E%3CP%3E%3D%20Set_Cell_Colour(%22A2%22%2C1)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20both%20the%20formula%20based%2C%20and%20literal%2C%20approach%20to%20calling%20Set_Cell_Colour%20I%20get%20a%20%23VALUE%20error.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20also%20get%20a%20%23VALUE%20error%20with%20the%20call%20to%20Set_GSU_Value_and_Format.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20literal%20approach%20is%20only%20done%20for%20testing%20I%20want%20to%20be%20able%20to%20use%20a%20formula%20approach%2C%20in%20the%20long%20run.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20hopes%20for%20some%20learned%20help%2C%20please%20and%20many%20thanks%20in%20anticipation.%3CBR%20%2F%3E%3CBR%20%2F%3EPhilip%3C%2FP%3E%3CP%3EBendigo%2C%20Victoria%3C%2FP%3E%3CP%3EAustralia%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-632808%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-638250%22%20slang%3D%22en-US%22%3ERe%3A%20Setting%20a%20cell's%20value%20and%20background%20colour%20through%20VBA%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-638250%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F313780%22%20target%3D%22_blank%22%3E%40PMHunt1955%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMatter%20is%20now%20resolved%20through%20use%20of%20a%20Worksheet_Activate%20sub-routine.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECode%20%3D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPrivate%20Sub%20Worksheet_Activate()%3CBR%20%2F%3E%3CBR%20%2F%3EDim%20wb%20As%20Workbook%3CBR%20%2F%3EDim%20ws()%20As%20Worksheet%3CBR%20%2F%3EDim%20rng%20As%20Range%3CBR%20%2F%3EDim%20rngNow%20As%20Range%3CBR%20%2F%3EDim%20wsCount%20As%20Integer%3CBR%20%2F%3EDim%20wsColor()%20As%20String%3CBR%20%2F%3EDim%20wsPrime%20As%20Worksheet%3C%2FP%3E%3CP%3EDim%20I%20As%20Long%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20wb%20%3D%20ThisWorkbook%3CBR%20%2F%3ESet%20wsPrime%20%3D%20wb.Worksheets(%22Coverage_Statistics%22)%20'Targets%20Coverage_Statistics%20for%20placement%20of%20Table%20of%20Contents%3CBR%20%2F%3E%3CBR%20%2F%3EwsCount%20%3D%20wb.Names(%22Main_Families_Count%22).RefersToRange.Value%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20rng%20%3D%20wsPrime.Range(%22A1%3AB%22%20%26amp%3B%20wsCount)%3CBR%20%2F%3EReDim%20ws(1%20To%20wsCount)%3CBR%20%2F%3EReDim%20wsColor(1%20To%20wsCount)%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20I%20%3D%201%20To%20wsCount%3CBR%20%2F%3E%3CBR%20%2F%3ESet%20ws(I)%20%3D%20Worksheets(I)%3CBR%20%2F%3EwsColor(I)%20%3D%20ws(I).Tab.Color%3CBR%20%2F%3ESet%20rngNow%20%3D%20rng(I%20%2B%201%2C%201)%3CBR%20%2F%3ErngNow.Interior.Color%20%3D%20wsColor(I)%3CBR%20%2F%3ErngNow%20%3D%20ws(I).Name%3CBR%20%2F%3E%3CBR%20%2F%3ENext%20I%3CBR%20%2F%3E%3CBR%20%2F%3EEnd%20Sub%3C%2FP%3E%3C%2FLINGO-BODY%3E
PMHunt1955
Contributor

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 same workbook, the name of one of the worksheets, in succession, and colour the cell with the same background colour as the tab from which the cell's value has been drawn.

 

I successfully used the following function to make the cell values the required value in each cell, obviously the function is called from each cell.

 

Setting a Cell's Value

 

Function Other_Worksheet_Name_GSU(WS_Number As Long)

Dim fWS_Number As Long

fWS_Number = WS_Number

Other_Worksheet_Name_GSU = Sheets(fWS_Number).Name

End Function

 

I then successfully used the following function to retrieve the tab background colour.

 

Getting the background colour of a tab

 

Function Get_Tab_Colour(WS_Number As Long)

Dim fWS_Number As Long

fWS_Number = WS_Number

Get_Tab_Colour = Sheets(fWS_Number).Tab.Color

End Function

That function returns single numeric values such as - 13395711, 255, 42495, 65535, 65280, 16776960, 15119490.

 

I then tried to combine the two into one function, and that is where I have hit problems.

 

The code I have written is

 

Function Set_GSU_Value_and_Format(targetCell As String, WS_Number As Long)

Dim ftargetCell As String
Dim fWS_Number As Long
Dim fTab_Colour As Long

ftargetCell = targetCell

fWS_Number = WS_Number

fTab_Colour = Sheets(fWS_Number).Tab.Color

Range(ftargetCell).Interior.Color = fTab_Colour

Set_GSU_Value_and_Format = Sheets(fWS_Number).Name

End Function

 

I call the function using the following call -

'=Set_GSU_Value_and_Format((ADDRESS((ROW()),(COLUMN()),4)),(ROW()-1))

which gets sent to the VBA as Set_GSU_Value_and_Format(A2,1).

 

I also tried to just set a cell's colour using similar code as follows -

Function Set_Cell_Colour(targetCell As String, WS_Number As Long)

Dim fWS_Number As Long
Dim ftargetCell As String
Dim fTab_Colour As Long

ftargetCell = targetCell

fWS_Number = WS_Number

fTab_Colour = Sheets(fWS_Number).Tab.Color

Range(ftargetCell).Interior.Color = fTab_Colour

End Function

 

I have tried both the following calls -

Formula based approach

=Set_Cell_Colour((ADDRESS((ROW()),(COLUMN()),4)),(ROW()-1))

which ends up sending Set_Cell_Colour(A2,1)

 

Literal approach

= Set_Cell_Colour("A2",1)

 

In both the formula based, and literal, approach to calling Set_Cell_Colour I get a #VALUE error.

 

I also get a #VALUE error with the call to Set_GSU_Value_and_Format.

 

The literal approach is only done for testing I want to be able to use a formula approach, in the long run.

 

With hopes for some learned help, please and many thanks in anticipation.

Philip

Bendigo, Victoria

Australia

1 Reply
Solution

@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

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
46 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
Security Community Webinars
Valon_Kolica in Security, Privacy & Compliance on
13 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
29 Replies