Working with Hex numbers in VBA

CMIIandExcel

Board Regular
Joined
Sep 4, 2009
Messages
190
Hi all

I am having some issues in passing Hex numbers to a function:

the call which is showing a syntax error is this
Code:
SetLabel FormControls.lblHeader, 2, iBarSidePad, (iFormHeaderHight - 2), iBarWidth, _
          "Header", &H999999, &HO00000
the function itself is this:
Code:
Private Function SetLabel(ByVal lblLabel As Control, ByVal iTop As Long, ByVal iLeft As Long, ByVal iHeight As Long, ByVal iWidth As Long, _
                          strText As String, ByVal BackColour As Byte, ByVal ForeColour As Byte)
With lblLabel
  .top = iTop
  .left = iLeft
  .Height = iHeight
  .Width = iWidth
  .Caption = strText
  .BackColor = BackColour
  .ForeColor = ForeColour
End With

End Function
I have tried a number of varible types for the 'Back and Fore Colour varibles, if i work with the hex numbers as strings the code complies but falls over at run time.

I know i could replace the hex number with the RGB function, but i would like to get my head around working with hex numbers

and thoughts and advice?

Regards

Mike
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Code:
Private Function SetLabel(ByVal lblLabel As Control, _
                          ByVal iTop As Long, _
                          ByVal iLeft As Long, _
                          ByVal iHeight As Long, _
                          ByVal iWidth As Long, _
                          strText As String, _
                          ByVal BackColour As Byte, _
                          ByVal ForeColour As Byte)
Your problem is with the declaration of the last two arguments as Byte data types. A Byte can be assigned a value of 0 to 255... you are trying to assign &H999999 (base 10 value of 10066329) to the BackColor argumant which is just a smidgeon too large:eek2: to fit in the a Byte variable. Hex representation is just an alternate way of representing "normal" numbers. Just change those two Byte declarations to Long and everything should probably be fine (VB will handle the necessary conversions behind the scenes for you).
 
Upvote 0
The uppercase 'O' in the second one also turns the code a pretty color of red...:rolleyes:

&HO00000
 
Upvote 0
GTO, Rick

Thanks for the quick responses, I think it was that 'O' which was causing me the headache.

Cheers guys, have a good Friday and a great weekend

Mike
 
Upvote 0
I think it was that 'O' which was causing me the headache.
Well, maybe one of your headaches... you still cannot stuff a value larger than 255 into a Byte variable/argument. Since you named those last two arguments BackColour and ForeColour, I'm guessing you will want a larger value than 255 every now and then for them, so I think you should still change their declarations from Byte to Long.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,718
Members
452,939
Latest member
WCrawford

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