+ Reply to Thread
Results 1 to 14 of 14

Multi-coloured string

  1. #1
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Multi-coloured string

    I want to change the colour of the characters in a string, following a change event which is: "when the value any character is less than unity".
    The value of a character is defined based on the font except for the special cases 2 & 3 given in Table 1.
    All character values are in the subset {1, 2/3, 1/3, 1/6}.

    To explain the same thing in a different way, the criteria for the colour change, from one character to the next is:
    * When the next character has a value less than 1, change colour.
    If the next colour has a value of 1 then colour is the same as the previous character.

    The colour of each character is based on the absolute cumulative value sum. Let x be any whole number, then colour is:
    - RGB(0, 255, 0): x (any whole number)
    - RGB(255, 0, 0): x + 1/6
    - RGB(204, 51, 0): x + 1/3
    - RGB(153, 102, 0): x + 1/2
    - RGB(102, 153, 0): x + 2/3
    - RGB(51, 204, 0): x + 5/6

    The font, character and value combinations are in Table 1:

    Please Login or Register  to view this content.
    I have posted a similar question previously but this is more complicated.
    https://www.excelforum.com/excel-pro...e-strings.html

    As an example:
    Arial.Arial,ArialA-NC-N

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

    Re: Multi-coloured string

    Hi, you have to reprocess every character and set the font and color per character everytime a change is made.
    That is the only way you will be able to achieve that in a character string.
    ---
    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
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Multi-coloured string

    Seems like a lot of effort is being put in to use excel in the opposite way to how it is supposed to be used.
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Multi-coloured string

    Can you post a few examples of strings and the way that you want them colored?
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

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

    Re: Multi-coloured string

    Your explanation is very bad but this is the general idea, tweak as you like
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Multi-coloured string

    @scottiex, agreed I must be mad to do this.
    @mikerickson, some examples are shown below.
    @Keebellah, thx for the input, however the colour change in your work is based on the character. It needs to be based on the cumulative sum of character values. Maybe the examples will explain further.

    First, the Colour Legend
    - RGB(0, 255, 0) or #00FF00: x (any whole number)
    - RGB(255, 0, 0) or #FF0000: x + 1/6
    - RGB(204, 51, 0) or #CC3300: x + 1/3
    - RGB(153, 102, 0) or #996600: x + 1/2
    - RGB(102, 153, 0) or #669900: x + 2/3
    - RGB(51, 204, 0) or #33CC00: x + 5/6
    - RGB(0, 255, 0) or #00FF00: x + 1 (any whole number)

    EXAMPLE 1
    This font is Arial, OK ....
    - The comma is a trigger for change, value is "1/3", colour is "RGB(204, 51, 0)".
    - Following characters are the same colour, until the next change, which is a series of dots, value is "1/6" each. Cumulative sum becomes "x + 1/2", then "x + 2/3", then "x + 5/6", lastly "x + 1" which is a whole number, so colour is green or "RGB(0, 255, 0)" for this last dot.

    EXAMPLE 2
    This_is_Courier_New
    - This time, each character is a trigger for change, because the font is Courier New, see Table 1 in original post. Each change is 1/3 because the value of each character is 1/3. Relevant colours are then "RGB(0, 255, 0)", "RGB(204, 51, 0)" and "RGB(102, 153, 0)".

  7. #7
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Multi-coloured string

    After reading post #6, now it is clear why I can't understand the logic for your post #1 example.

    You write :
    Quote Originally Posted by Excel_&_Help View Post
    Please Login or Register  to view this content.
    while it really should be :
    Please Login or Register  to view this content.

    I cannot put the code here (the Sucuri is complained about something like SQL injection, as always), so look the code inside the attachment.
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

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

    Re: Multi-coloured string

    Looks good just allow for more rows in column A and the poster can be happy

  9. #9
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Multi-coloured string

    Don't worry, I have prepared some chocolate and candy in case the poster is not happy (though it's not halloween yet).

  10. #10
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Multi-coloured string

    @karedog, genius!
    My mistake in post #6, it should have been 2/3, it was easy to change in your code.

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Multi-coloured string

    Thanks for marking the thread as solved.

    Regards

  12. #12
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Multi-coloured string

    @karedog
    With the code at post #7.
    I've extended it to include other fonts, but funny things start to happen such as:
    - Excel freezes up, typically if selecting more than one cell.
    - Extra characters show up on the screen, but when you click the Formula bar, the correct number of characters are there.
    - I have added some private use characters in my fonts and these do not show up, an alternative character shows up.

    EDIT: After some further testing, this seems to happen when the first character is not, for example Font 01.
    Please Login or Register  to view this content.

    Some questions.
    - What is SQL injection? Is this a threat to my work? Why would another forum member complain about it? Which part of the code is SQL injection? If so, is there a solution without it.
    - The cases for {1/6, 1/3, 1/2, 2/3, 5/6, 1} are represented in VBA by {0.167, 0.333, 0.5, 0.667, 0.833, 1}. Is this a source of the problem?
    Last edited by Un-Do Re-Do; 07-29-2017 at 11:44 AM.

  13. #13
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Multi-coloured string

    >>> Excel freezes up, typically if selecting more than one cell.
    It shouldn't. If the code is worked for one cell, it should worked for other cells too, only with longer time to do the calculation and changes

    >>> Extra characters show up on the screen, but when you click the Formula bar, the correct number of characters are there.
    >>> I have added some private use characters in my fonts and these do not show up, an alternative character shows up.
    I have no idea about this, as you see in the code, all the macro doing is to change the font color only, no statement for adding new characters at all. Maybe the culprit is your "self made" fonts is not worked very well with Excel.

    You also must put all of the fonts being used in the select case statement, or the macro won't worked.
    Please Login or Register  to view this content.
    >>> What is SQL injection? Is this a threat to my work? Why would another forum member complain about it? Which part of the code is SQL injection? If so, is there a solution without it.
    Read this article to get the picture : https://en.wikipedia.org/wiki/SQL_injection
    The bad guys (a.k.a. crackers) are always trying to break the website they targeting, one of the technique is adding some texts to query that user send to the webserver, in hope that the webserver gives response that actually shouldn't given to a normal user (such as admin privilleges, some secret data, etc). It is the job of the owner of the website (or his team) to always protect their website and update/patch the security holes/bugs/other flaws. But instead of doing this by himself, he also can "hire" someone/some company that offering the service to do this. And ExcelForum choose Sucuri (https://sucuri.net/) for this. But now, it is found out that Sucuri is giving too many "false alarm". Some texts that user send back to the server (our posts) is treated as an attempt to break the server. Some basic word like Replace immediately followed by open bracket, html tags, or other things (too many of them) is treated like this. So of course thing like this make the forum members feel very annoyed. They definitely are not crackers, they only posting some text (formulas or macros or data), but Sucuri page is popped up with annoying message.

    The solution : One of the simplest way is to put the troubled texts (formulas/macros/data) inside a file (could be a text file, zip file, Excel file) and let the other forum members download this file and read the file, instead reading directly on the webpage. Sucuri will not check the file, it only checks the texts.

    >>> The cases for {1/6, 1/3, 1/2, 2/3, 5/6, 1} are represented in VBA by {0.167, 0.333, 0.5, 0.667, 0.833, 1}. Is this a source of the problem?
    No, because the previous statement is :
    Please Login or Register  to view this content.
    The reason I do this is because you want to continously add the floating point numbers, and after sometime, Excel will loose the digit of precession.
    Take a look at this code :
    Please Login or Register  to view this content.
    If you run this code and examine the immediate window, you will notice that the last three numbers are :
    1666.33333333353
    1666.5000000002
    1666.66666666687
    You see, they supposed to be (but are not) :
    1666.33333333333
    1666.5
    1666.66666666667
    So, to avoid the problem like this, I choose to use the "Select Case Round(tot, 3)" method.

  14. #14
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Multi-coloured string

    OK thank you.

+ 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] extract multi-letter string bits from string depending on size and case
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-18-2017, 10:41 PM
  2. Coloured letters within string
    By sparx in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-04-2017, 05:20 AM
  3. nth occurrence of string in multi-column range
    By Gerralt_Tarrant in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-01-2014, 11:03 AM
  4. Searching for a string in a multi-lined row
    By Saken2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-07-2014, 01:52 AM
  5. How can I count coloured cells (coloured using Conditional Formatting)
    By franfry in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-15-2013, 02:40 PM
  6. [SOLVED] how I make multi value to one variable of string in VBA
    By zalora in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-27-2013, 12:08 AM
  7. Replies: 0
    Last Post: 04-13-2011, 06:48 AM

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