+ Reply to Thread
Results 1 to 6 of 6

Inherit bolded substring from another cell

  1. #1
    Registered User
    Join Date
    01-08-2021
    Location
    China, Jiangsu
    MS-Off Ver
    2016
    Posts
    27

    Inherit bolded substring from another cell

    Hello,
    I want to do something like the image attached: I have a couple of columns to insert and store data, and a large column to display the data combined together for a better view, which is generated by VBA. There are some substrings in the input cells that are bolded, and I want to inherit the "bold" in my display cell.

    I don't know if there is a method to get the string from a cell and put it to another, without messing up the partially bold format.

    What I have in mind is to loop through every single character in every single input cells, check if bolded, then bold the corresponding character in the display cell. This should be able to solve the problem, but I just don't really like this character-by-character method (feels like a slow program). Are there better ways to do this?

    Sample code for my thought
    Please Login or Register  to view this content.

    Thank you very much.
    Attached Images Attached Images

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Inherit bolded substring from another cell

    Formatting like this is applied as an attribute of individual characters or strings of characters. There is no way to find out what characters are bold without checking each one. For example, there is no built-in way to get the index of the first and last bold characters.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    01-08-2021
    Location
    China, Jiangsu
    MS-Off Ver
    2016
    Posts
    27

    Re: Inherit bolded substring from another cell

    Thank you for reply.
    So this means my approach is actually the best we can do?

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Inherit bolded substring from another cell

    What you did is how I would do it. I will add that someone may have an idea of how to improve it, but like I said there is nothing built into to relieve the pain.

    It would be interesting to write a class that would provide a utility for formatting text like this, but it would be a lot more code and less efficient. What you have is probably optimized for your particular situation.

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,222

    Re: Inherit bolded substring from another cell

    dexiongmao, in my opinion, the code execution time can be significantly shortened.

    Notice:
    1. When no characters in Cell are bold, Cell.Font.Bold returns False.
    2. When all characters are bold, Cell.Font.Bold returns True.
    3. When some characters are bold, Cell.Font.Bold returns Null.

    With this knowledge, you can speed up the execution of the code when the second condition is met, because we know that the whole string from 1 to Len(Cell.Value) should be bold. If the third condition is met, you can also speed up the code. Do not change sign by sign, but search and change entire fragments (reading a property itself is several times faster than changing this property). You read many times but only change once.
    By following the above rules, I think that the code execution time can be reduced by at least half.

    I don't like the following code, but let it be an inspiration to optimize your code.
    Please Login or Register  to view this content.
    In my tests, I got a performance increase of 2-3 times (and even 6 to 13, depending on the complexity of the input texts). So it's probably worth fighting for.

    Artik

  6. #6
    Registered User
    Join Date
    01-08-2021
    Location
    China, Jiangsu
    MS-Off Ver
    2016
    Posts
    27

    Re: Inherit bolded substring from another cell

    Thanks a lot!
    This is exactly what I want. I had a strong feeling that my original code could be improved a lot.

+ 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] If cell is bolded then font color red
    By dedonx in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-18-2019, 10:53 AM
  2. Replies: 8
    Last Post: 03-09-2018, 11:39 AM
  3. Macro to search cells for substring and replace contents of cell if substring is found
    By robbyvegas in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2015, 06:40 AM
  4. VBA to highlight row if cell is bolded
    By devilchild99 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-12-2013, 01:12 AM
  5. [SOLVED] Extract Substring, then Ignore that Substring, while collecting data from Other substrings
    By Sameki121 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-17-2012, 05:21 PM
  6. Replies: 2
    Last Post: 03-30-2008, 03:05 PM
  7. how to sort list, bolded items from no bolded?
    By tcd50 in forum Excel General
    Replies: 1
    Last Post: 07-09-2005, 06:05 PM

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