+ Reply to Thread
Results 1 to 9 of 9

Conversion Hexadecimal to RGB value VBA Macro

  1. #1
    Registered User
    Join Date
    10-25-2021
    Location
    Allemagne
    MS-Off Ver
    MS Office 2020
    Posts
    5

    Red face Conversion Hexadecimal to RGB value VBA Macro

    Hello,
    I am a student and for a project I need to to adapt the look and feel of my application. To do this, I would first have to analyse some code and enter the colour values in order in an Excel table.
    For this I need a column with the hex value of the colour contained in the source code file, the RGB value and a third column that is coloured in the corresponding colour. If the hex or RGB value is changed, the third cell should be changed accordingly. Additionally, a column with the name of the function in which the colour is set.

    I enclose here the draft of my work on it.
    Today I can convert either from HEX to RGB or RGB to Hex with the help of excel formulas. I tried to convert from rgb to hex with a macro but it doesnīt work.

    Can you please help me
    Attached Files Attached Files

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Conversion Hexadecimal to RGB value VBA Macro

    Hi, welcome to the forum.
    Take a look here, maybe it helps
    https://worksheetsvba.com/en/vba-color-conversion
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Conversion Hexadecimal to RGB value VBA Macro

    I placed the code in your worksheet, removed some of your modules too
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    10-25-2021
    Location
    Allemagne
    MS-Off Ver
    MS Office 2020
    Posts
    5

    Re: Conversion Hexadecimal to RGB value VBA Macro

    Thanks you for the welcome !
    And Thank you for the tip but I already know it but I donīt really understand the code. I tried to implement this code :

    Public Function ConvertHexToRGB(sHexColor As String, sRGB As String) As Integer
    sHexColor = Replace(sHexColor, "#", "")
    Select Case UCase(sRGB)
    Case "R"
    ConvertHexToRGB = CInt("&h" & Left(sHexColor, 2))
    Case "G"
    ConvertHexToRGB = CInt("&h" & Mid(sHexColor, 3, 2))
    Case "B"
    ConvertHexToRGB = CInt("&h" & Right(sHexColor, 2))
    End Select
    End Function

    But It doesnīt change anything on my excel sheet

    I am so bad at excel I am trying to improuve

  5. #5
    Registered User
    Join Date
    10-25-2021
    Location
    Allemagne
    MS-Off Ver
    MS Office 2020
    Posts
    5

    Re: Conversion Hexadecimal to RGB value VBA Macro

    Thank you !!

    Now a new problem : if I would like to change the hex value without changing the formula how do I do ? Do I have to use this code to switch from HEX value to RGB value ?

    Public Function ConvertHexToRGB(sHexColor As String, sRGB As String) As Integer
    sHexColor = Replace(sHexColor, "#", "")
    Select Case UCase(sRGB)
    Case "R"
    ConvertHexToRGB = CInt("&h" & Left(sHexColor, 2))
    Case "G"
    ConvertHexToRGB = CInt("&h" & Mid(sHexColor, 3, 2))
    Case "B"
    ConvertHexToRGB = CInt("&h" & Right(sHexColor, 2))
    End Select
    End Function

  6. #6
    Registered User
    Join Date
    10-25-2021
    Location
    Allemagne
    MS-Off Ver
    MS Office 2020
    Posts
    5

    Re: Conversion Hexadecimal to RGB value VBA Macro

    Because the main goal here is if I want to change the RGB value it will modify the HEX values and the color and If I modify the HEX value it will change the RGB value and so the color I donīt know if you know how to do it ?

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Conversion Hexadecimal to RGB value VBA Macro

    You cannot change the HEX because it's created by the formula.
    The only way is just enter the number in one of the three RGB columns.
    You should also include some checking code to see that the values are always between 1 and 255 and in the formula a check to see if all RGB values have been filled before processing.
    Another thing, Why lop through all cells if you can do the check per row changed?

  8. #8
    Registered User
    Join Date
    10-25-2021
    Location
    Allemagne
    MS-Off Ver
    MS Office 2020
    Posts
    5

    Re: Conversion Hexadecimal to RGB value VBA Macro

    This is a very good question This is the only way I know to check and change the modified cells

    And do you have an idea on how can we change the HEX value. I was thinking for example with a macro were you can convert Hex to RGB ?

    Could it work ?

  9. #9
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,743

    Re: Conversion Hexadecimal to RGB value VBA Macro

    Crossposted: https://chandoo.org/forum/threads/co...cro-vba.47080/

    You have violated two or our forum rules. Please take a few minutes to read the rules you agreed to when you joined.

    Rule 03: Cross-posting Without Telling Us

    Your post does not comply with Rule 3 of our Forum RULES. Do not cross-post your question on multiple forums without telling us about your threads on other forums.

    Post a link to any other forums where you have asked the same question.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Read this to understand why we ask you to do this.

    I have added the crosspost reference for you today. Please comply with this and all our rules in the future


    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Hexadecimal to decimal.
    By eemilv in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 09-17-2020, 10:19 AM
  2. Hexadecimal to Decimal
    By audi321 in forum Excel Formulas & Functions
    Replies: 32
    Last Post: 01-26-2017, 02:11 PM
  3. [SOLVED] Hexadecimal to binary
    By me@home.net in forum Excel General
    Replies: 10
    Last Post: 06-16-2016, 10:32 AM
  4. Macro to change hexadecimal values to normal
    By toravietl in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-11-2014, 11:36 AM
  5. [SOLVED] Alphanumeric string conversion into hexadecimal then report the number of bytes
    By Talamon in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-24-2014, 03:38 PM
  6. string to hexadecimal?
    By Mark121 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-01-2008, 10:26 AM
  7. [SOLVED] Hexadecimal to Binary Conversion
    By sean_f in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-04-2006, 04:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1