RGB function fails with Value error

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
Apparently VB has an RGB function, but VBA does not. At least, when I try this in the Immediate wiondow,
Code:
=rgb(255,0,0)
I get "Compile error. Expected: line number or label or statement or end of statement". I don't understand that error, so I thought I'd write my own:
Code:
Function RGB(R As Long, G As Long, B As Long) As Long
  RGB = R + 256 * (G + (B * 256))
End Function
I put it in my person add-in module, but when I run it from a worksheet, it gets a Value error.

How can I either get a built-in RGB function or the one above to work?

Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I think the problem may be that VBA has a function named RGB so it is objecting to you calling your function by that name (how is it to know which RGB function to use when called?). But even renaming it, you still are making your function harder than it needs to be. Here I'll call the function xlRGB to distinguish it from VBA's RGB function. Give this a try...
Code:
Function xlRGB(R As Long, G As Long, B As Long) As Long
  xlRGB = RGB(R, G, B)
End Function
 
Upvote 0
In the immediate window, you would use:

Code:
?rgb(255,0,0)

and not:

Code:
=rgb(255,0,0)
 
Upvote 0
I think the problem may be that VBA has a function named RGB so it is objecting to you calling your function by that name (how is it to know which RGB function to use when called?).
I called it RGB because I thought I had determined that the RGB function was only in VB, not VBA. Apparently, I confused myself by using "=" instead of "?" in the Immediate window, as pointed out by RoryA.

But even renaming it, you still are making your function harder than it needs to be.
I used the explicit formula, rather than calling the function, because I had convinced myself that there was no such function. (sigh)

Here I'll call the function xlRGB to distinguish it from VBA's RGB function. Give this a try...
Code:
Function xlRGB(R As Long, G As Long, B As Long) As Long
  xlRGB = RGB(R, G, B)
End Function

I thought I tried that calling it MyRGB. I just copied your xlRGB function and it also fails with a #VALUE ! error. Then as I was preparing this reply, I discovered that all of the functions in my personal add-in are working. They all get #VALUE ! errors. I put breakpoints on the Function statements and they never get called.

What could I have done to cause my personal add-in to stop working? AAARRRGGG!!!
 
Upvote 0
I thought I tried that calling it MyRGB. I just copied your xlRGB function and it also fails with a #VALUE ! error. Then as I was preparing this reply, I discovered that all of the functions in my personal add-in are working. They all get #VALUE ! errors. I put breakpoints on the Function statements and they never get called.

What could I have done to cause my personal add-in to stop working? AAARRRGGG!!!

I just rebooted the computer and everything is working again.

Thanks for all the help.
 
Upvote 0
I think I just figured out why none of the functions in my personal add-in were working. I believe I must have failed to clear a breakpoint, so one function was still "running" and Excel couldn't call another one. That would explain why rebooting resolved it.

If I am right, the error messages leave a lot to be desired.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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