Too many Different Cell Formats Message... Now what?

Mousehunter

New Member
Joined
May 6, 2008
Messages
25
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
It all started when I promised a colleague of mine to prepare a script in VBA that would highlight the active cell with the desired colour and then restore the old colour when he would activate another cell. While trying to do so, I found out about the ActiveCell.Interior.Color property which is far superior to the ActiveCell.Interior.ColorIndex I knew about. The former has 256^3 options (almost 16.77mil choices) while the latter only 56.

So VBA must be grouping all different colours of the RGB palette to fit the 56 choices of the ColorIndex. This ought to be around 300 thousand palette choices per ColorIndex. In my effort to store in an Array how many times each ColorIndex is used, I change the Fill Color of the cell A1 looping through all RGB possible permutations, extract the corresponding ColorIndex and keep track of it in an array.

Alas, I never seem to reach the combinations because around the 66th thousand I get the message of the title.

I tried the search but the thread below does not seem to give a clear answer (or it is I who does not understand) as to how am I going to work around this...


The code I am using can be found below
VBA Code:
Sub AllColoursArray()

'Dim AllColoursArray(1 To 256 ^ 3, 1 To 2)
Dim AllStats(1 To 56)
Dim intR As Integer, intG As Integer, intB As Integer, ColourIndex As Integer, ColourCounter As Double

ColourCounter = 0

For intR = 0 To 255
    For intG = 0 To 255
        For intB = 0 To 255
            ColourCounter = ColourCounter + 1
            Cells(1, 1).Interior.Color = RGB(intR, intG, intB)
            ColourIndex = Cells(1, 1).Interior.ColorIndex
'            AllColoursArray(ColourCounter, 1) = ColourIndex
'            AllColoursArray(ColourCounter, 2) = "RGB(" & intR & ", " & intG & ", " & intB & ")"
            AllStats(ColourIndex) = AllStats(ColourIndex) + 1
        Next intB
    Next intG
Next intR

Range("B1:B56").Value = Application.WorksheetFunction.Transpose(AllStats)

End Sub

While I would like to see behind the scenes and understand why I cannot get this to work, the real question is: In the macro I delivered (that highlights a cell and then restores the old colour) If my colleague has a custom colour as a fill color in a cell (one of the 16.77mil), Excel/VBA will classify it in in one of the 56 colour indices. If I only keep track of the colour indices in my code how will it know what colour to restore? Does it not loose the information?

Thanks in advance

PS: I can provide the code I delivered if you wish as well but it ought to be straightforward
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Why are you trying to do this?
It would be simpler just to change your existing code to store the color property, rather than the colorindex
 
Upvote 0
It is an excel limitation.


You could get around it if you *really want to* by using a succession of new workbooks.

VBA Code:
Sub AllColoursArray()
   
    'Dim AllColoursArray(1 To 256 ^ 3, 1 To 2)
    Dim AllStats(1 To 56)
    Dim intR As Integer, intG As Integer, intB As Integer, ColourIndex As Integer, ColourCounter As Long
    Dim WB As Workbook, WS As Worksheet

    Set WB = Application.Workbooks.Add
    Set WS = WB.Worksheets(1)
       
    ColourCounter = 0
    Application.ScreenUpdating = False
    For intR = 0 To 255
        For intG = 0 To 255
            For intB = 0 To 255
                If ColourCounter <= 60000 Then
                    ColourCounter = ColourCounter + 1
                    With WS
                        .Cells(1, 1).Interior.Color = RGB(intR, intG, intB)
                        ColourIndex = .Cells(1, 1).Interior.ColorIndex
                    End With
                   
                    '            AllColoursArray(ColourCounter, 1) = ColourIndex
                    '            AllColoursArray(ColourCounter, 2) = "RGB(" & intR & ", " & intG & ", " & intB & ")"
                    AllStats(ColourIndex) = AllStats(ColourIndex) + 1
                Else
                    DoEvents
                    WB.Close SaveChanges:=False
                    Set WB = Application.Workbooks.Add
                    Set WS = WB.Worksheets(1)
                    ThisWorkbook.Activate
                    ColourCounter = 0
                    DoEvents
                End If
            Next intB
        Next intG
    Next intR
    WB.Close SaveChanges:=False
    Application.ScreenUpdating = True
    Range("B1:B56").Value = Application.WorksheetFunction.Transpose(AllStats)
End Sub

But Fluff asks the important question, which is "why?" If all you are trying to do for your colleague is highlight the active cell with the desired color and then restore the old color when another cell is activated, then just store the color property and forget about colorindex.
 
Upvote 0
Should be able to do it with conditional formatting as well, but will take a fair amount of time run.
 
Upvote 0
For what it's worth, this is a list of the colour index numbers & a count of how many times they occur when compared to the colour property
+Fluff 1.xlsm
ABC
1Colour indexcountColour
2148,724
3227,033
43209,183
54319,215
65178,152
7320
86129,129
9270
107414,661
11260
12888,530
13280
149149,378
15300
1610248,508
1711110,716
18250
1912535,154
2013492,784
21290
2214309,300
23310
2415606,944
2516579,510
2617423,806
27181,145,568
28540
291956,857
302044,902
31340
3221184,313
3322649,971
3423309,374
3524112,703
3633108,579
3735269,897
3836126,802
3937250,065
4038339,077
4139285,324
4240292,667
4341436,877
44421,020,541
4543885,226
4644200,047
4745227,586
4846290,107
4947743,915
5048743,364
5149197,753
52501,177,974
535165,664
5452172,796
5553360,798
5655630,420
5756577,322
List
 
Upvote 0
Why are you trying to do this?
It would be simpler just to change your existing code to store the color property, rather than the colorindex
Thank you for your time and I am sorry for the late response.

This very same problem had been tackled by another experienced member in the past (without the use of Conditional Formatting. Surprisingly while his code was doing the job, "it did not exactly clean up the mess" as it was doing something with the grids of the deactivated cell. I am scanning through the site in order to find the snippet. He was using the ActiveCell.Interior.Color.

I was such in a hurry to deliver that I did not drill down to the very bottom of why this was happening. I just modified his code to use the ActiveCell.Interior.ColorIndex but I was really concerned that 56 choices could not be holding 16.77mil choices...

I 'll be back in a while
 
Upvote 0
Why are you trying to do this?
It would be simpler just to change your existing code to store the color property, rather than the colorindex
This is the post I referred to. The solution is not restoring the cell as it exactly was

VBA Code:
Option Explicit
Dim LastCellColor As Long, LastCellAddress As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Len(LastCellAddress) Then Range(LastCellAddress).Interior.Color = LastCellColor
  LastCellAddress = ActiveCell.Address
  LastCellColor = ActiveCell.Interior.Color
  ActiveCell.Interior.Color = vbRed
End Sub

While if you modify it dropping the Color and using the Colorindex...

VBA Code:
Option Explicit

Dim LastCellColor As Long, LastCellAddress As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Len(LastCellAddress) Then Range(LastCellAddress).Interior.ColorIndex = LastCellColor
LastCellAddress = ActiveCell.Address
LastCellColor = ActiveCell.Interior.ColorIndex
ActiveCell.Interior.ColorIndex = 6
End Sub
 
Upvote 0
Why did you change color to colorindex? As I said in post#2 just use Color & forget about colorindex.
 
Upvote 0
Why did you change color to colorindex? As I said in post#2 just use Color & forget about colorindex.
Did you test Mr Rothstein's code? Does it work properly? :unsure: Because this is what I get using the ActiveCell.Interior.Color

1628705480511.png


Can you please test the code? Because if only I get this, perhaps there is something wrong with my settings. Or something else...
 
Upvote 0
You have no coloured cells on that sheet, so I don't understand what you are trying to do.
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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