Passing a Color from a Function with a Variable

Hardware Man

New Member
Joined
Apr 10, 2013
Messages
40
I have a list of 20 different colors whose RGB values I'd like to hard code into a function. I'd be passing 2 values to the function ("highNum" and "lowNum"). My goal is to calculate the percentage increase between those 2 numbers and return a color based on the result (with the idea being that the larger result gets the brighter color filled into the target cell).

I'm pretty much at the end of my rope in figuring out how to pass the value of the color between the sub and the function, and could really use a hand!

I'm fine with using either the RGB value or the hex value to pass the color (whichever is easiest).
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
So pass 100 and 50 and calculate that 100 is 200% of 50? Or 50 is 50% of 100?
And then what? I don't understand the rest. Examples of inputs and expected results would probably help.
 
Upvote 0
"I'm pretty much at the end of my rope in figuring out how to pass the value of the color between the sub and the function, and could really use a hand!"
 
Upvote 0
If all you can do is repeat what you've already said then I can't help. Sorry.
 
Upvote 0
No need for sarcasm. I repeated the line that mattered because I thought it made things clear.

I do not need help with calculating the percentages. I only need help with passing the color variable.
 
Upvote 0
Passing parameters between modules is a pretty basic feature of VBA. Based on your description, I'd GUESS something like this:

VBA Code:
Sub CallingModule()

    highNum = 110
    lowNum = 100
    x = MyFunction(highNum, lowNum)
    Debug.Print x
    
End Sub

Function MyFunction(a, b)

    pct = (a - b) / b
    
    PctArray = Array(0, 0.1, 0.2, 0.3, 0.5)
    RGBArray = Array(RGB(100, 0, 0), RGB(200, 0, 0), RGB(300, 0, 0), RGB(400, 0, 0), RGB(500, 0, 0))
    
    With WorksheetFunction
        MyFunction = .Index(RGBArray, .Match(pct, PctArray))
    End With
    
End Function

If this doesn't help, please show us the sub you have so far, and what your function looks like.
 
Upvote 0
Solution

Forum statistics

Threads
1,203,027
Messages
6,053,120
Members
444,640
Latest member
Dramonzo

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