OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Thread Tools Search this Thread Display Modes
Old 18-07-2006, 15:28
ddonnahoe's Avatar
ddonnahoe ddonnahoe is offline
Senior Member
Join Date: Jan 2004
Location: Louisville, KY
Posts: 552
Send a message via ICQ to ddonnahoe Send a message via AIM to ddonnahoe Send a message via MSN to ddonnahoe Send a message via Yahoo to ddonnahoe
Default Hex to Decimal

Alex, I know you are a busy guy, but is there a way to convert Hex code to Decimal in VBA. I am creating a HEX-RGB-CMYK generator that Sallybode wrote about sometime ago. I want to be able to have the txbHEXr_Change() event convert from the users input of hex code to Decimal in another textbox.

My current RGB to HEX sub lloks like this...
Sub HexConverter()
    'convert units to HEX
    txbHEXr.Text = Format(Hex(r1), "00")
    txbHEXg.Text = Format(Hex(g1), "00")
    txbHEXb.Text = Format(Hex(b1), "00")
End Sub
Waiting for a ride in the T.A.R.D.I.S.
Reply With Quote
Old 18-07-2006, 20:24
Alex's Avatar
Alex Alex is offline
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4

That's easy. You need to use the Val function and prepend the hexadecimal value with &h like this:

Dim sHex As String
Dim nDec As Long
sHex = "3FF"
nDec = Val("&h" & sHex)
There is only one catch. When the hex value is in the range of 8000-FFFF you will get a negative value, even though you might expect a Long.

You can add this line to correct for that:

If nDec < 0 And Len(sHex) < 5 Then nDec = nDec + 65536
By the way, I don't think your expression Format(Hex(r1), "00") will give the correct results for 1 digit values in the range of 0A-0F. I would use is this instead:

Right("00" & Hex(r1), 2)
Or more generally:

Function FormatHex(ByVal v As Long, ByVal nNumDigits As String) As String
    FormatHex = Right(String(nNumDigits, "0") & Hex(v), nNumDigits)
End Function
So you can use it like:

txbHEXr.Text = FormatHex(r1, 2)

Last edited by Alex; 18-07-2006 at 20:31.
Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Feet Inches to Inches and Decimal Function shelbym CorelDRAW/Corel DESIGNER VBA 1 06-06-2005 12:01

All times are GMT -5. The time now is 13:57.

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com