Construct a line of code using values of an array

SeniorNewbie

Board Regular
Joined
Jul 9, 2023
Messages
77
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
  2. MacOS
Hi out there,

my target is to construct a code line by loop through an array with the values of iR,iG. iB. All my attempts end in Type mismatch, No such Property or similar errors.
The following code show my attempts:
VBA Code:
Sub Colors()
Dim aColors, i As Integer, sParam As String

'aColors = Array("255, 0, 0", "255, 100, 100")                  'for line 55
'aColors = Array("RGB(255, 0, 0)", "RGB(255, 100, 100)")        'for line 56
'aColors = Array(" = RGB(255, 0, 0)", " = RGB(255, 100, 100)")  'for line 57

For i = 0 To UBound(aColors)
    With ActiveSheet
        sParam = aColors(i)
        '.Cells(i + 1, 1).Interior.Color = RGB(aColors(i))
        '.Cells(i + 1, 1).Interior.Color = sParam
        '.Cells(i + 1, 1).Interior.Color sParam
    End With
Next i

End Sub
To test the code the corresponding two lines have to be activated.

Any idea what's my mistake?

THX in advance
Senior Newbie
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
How about
VBA Code:
Sub Colors()
Dim aColors, i As Integer, sParam As String

aColors = Array(Array(255, 0, 0), Array(255, 100, 100))                 'for line 55
'aColors = Array("RGB(255, 0, 0)", "RGB(255, 100, 100)")        'for line 56
'aColors = Array(" = RGB(255, 0, 0)", " = RGB(255, 100, 100)")  'for line 57

For i = 0 To UBound(aColors)
    With ActiveSheet
'        sParam = aColors(i)
        .Cells(i + 1, 1).Interior.Color = RGB(aColors(i)(0), aColors(i)(1), aColors(i)(2))
        '.Cells(i + 1, 1).Interior.Color = sParam
        '.Cells(i + 1, 1).Interior.Color sParam
    End With
Next i

End Sub
 
Upvote 0
Solution
Hi Fluff,

thank you so much! it works. I didn't think, that's necessary to seperate the values. I hoped it's easier ... but anyway THX a lot!!

Have a nice day
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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