Using Colour Codes in an array

Mike UK

New Member
Joined
Dec 14, 2011
Messages
41
Hi,
I have a table that stores the field name in column 1 then the colour code in column 2 (see below) in the VBA code I am trying to use the Colour code. If I use it as a number it works but I can't work out the correct number for the RGB codes I have. How do I pass the array variable?

Column 1 Column 2 RGB Code
Coxes Apples16225862 247,150,70
Bramley Apples16225862247,150,70
Commice Pears151507723,30,69
Conference Pears151507723,30,69

<colgroup><col><col><col></colgroup><tbody>
</tbody>

The Array of Column 1 and Column 2 is called arrAssetClass

Range("B" & Rowcount).Interior.Color = arrAssetClass(AssetClassRow, 2)

Any ideas how I can get the correct number code or pass the RGB or Hex code?
Thanks,
Mike
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Mike

If the values in column 2 are valid colour codes then they should work.

If you wanted to use the string in the RGB code column you could split it and try something like this.
Code:
arrRGB = Split(arrAssetClass(AssetClassRow,3), ",")

Range("B" & Rowcount).Interior.Color = RGB(arrGRB(0),arrGRB(1),arrGRB(2))
 
Upvote 0
Mike

If the values in column 2 are valid colour codes then they should work.

If you wanted to use the string in the RGB code column you could split it and try something like this.
Code:
arrRGB = Split(arrAssetClass(AssetClassRow,3), ",")

Range("B" & Rowcount).Interior.Color = RGB(arrGRB(0),arrGRB(1),arrGRB(2))

Thanks for your help and reply - Column 2 are valid colour codes but I can't find the correct number that corresponds to the RGB code in Column 3. Any ideas how I can find the correct numerical code?
 
Upvote 0
If i colour some cells using 16225862 and RGB(247,150,70) they arent the same colour. RGB(70,150,247) is though.
 
Upvote 0
Heres a method once you have sorted out the order issue:

Code:
With Sheets("Sheet1")
    lr = .Cells(.Rows.Count, "C").End(xlUp).Row
    arr = .Range("C2:D" & lr)
    For i = LBound(arr) To UBound(arr)
        R = Split(arr(i, 1), ",")(0)
        G = Split(arr(i, 1), ",")(1)
        B = Split(arr(i, 1), ",")(2)
        arr(i, 2) = Int("&H" & Hex(RGB(R, G, B)))
    Next
    .Range("D2:D" & lr) = Application.Index(arr, 0, 2)
End With

This places the number in column D based on the RGB in column C. Is that what you want?
 
Upvote 0
Thanks Norie - used this code and worked perfectly. Also very simple and only a couple of lines of code..... Many thanks for your help.
 
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

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