+ Reply to Thread
Results 1 to 4 of 4

Copy only the text of a certain color from a cell

Hybrid View

  1. #1
    Registered User
    Join Date
    10-30-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    1

    Copy only the text of a certain color from a cell

    Hi everyone!

    I have an Excel problem which I have been trying to solve for a while but so far I have had no luck. A friend recommended this forum, so I am really hoping you guys can help!

    I need a VBA script that can copy only text that has a font color of red from a cell. The cell has both black and red text. I then need to copy the result into an adjacent cell.

    An Example (A1):-

    This is an example of a cells content.

    It has both red and black text.


    I want the macro to search through column A and copy only the text in each cell that is red.

    The result in (B1) will be:-

    example of a cells

    red



    Could anyone help me?

    Many thanks in advance!

    Mark

  2. #2
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Copy only the text of a certain color from a cell

    Hi
    try this user-defined function
    it assumes the "red" you are refering to is the standard colour red RGB(255,0,0)
    if not, you'll have to tweak it to reflect the colour your are using (there are thousands of variants of "red"!)

    Function extract_colour(rng As Range)
    Dim FontCol, x As Long, txt As String
    FontCol = RGB(255, 0, 0)
    txt = ""
    
    For x = 1 To rng.Characters.Count
        If rng.Characters(x, 1).Font.Color = FontCol Then txt = txt & rng.Characters(x, 1).Text
    Next x
    
    If Len(txt) > 0 Then ActiveCell.Font.Color = FontCol Else ActiveCell.Font.Color = xlAutomatic
    extract_color = txt
    
    End Function

    if you are not familiar with user-defined functions, this is what you do:

    To copy and use a user-defined formula
    press alt+F11 to open the VBA area
    select insert > module
    copy the code from this website and paste it into the VBA module you just inserted
    Return to the spreadheet. You now have a new formula that works like the inbuilt formulas such as =SUM()
    In cell B1, type:
    =extract_colour(A1)
    The formula should copy the red characters

  3. #3
    Forum Expert
    Join Date
    11-26-2013
    Location
    Colac, Victoria, Australia
    MS-Off Ver
    Excel 2016
    Posts
    1,309

    Re: Copy only the text of a certain color from a cell

    Function extract_color(rng As Range)
        Dim FontCol, x As Long, txt As String
        FontCol = RGB(255, 0, 0)
        txt = ""
    
        For x = 1 To rng.Characters.Count
            If rng.Characters(x, 1).Font.Color = FontCol Then txt = txt & rng.Characters(x, 1).Text
        Next x
    
        If Len(txt) > 0 Then ActiveCell.Font.Color = FontCol Else ActiveCell.Font.Color = xlAutomatic
        extract_color = txt
    
    End Function
    Hi angrygorilla and NickyC

    I have made a small change to the macro from Nicky. There was a typo in the code - color was spelled two ways!

    Regards, David


    When you reply please make it clear WHO you are responding to by mentioning their name.
    If this has been of assistance, please advise. A little thanks goes a long way.
    - Please click on the *Add Reputation button at the bottom of helpful responses.
    Please mark your thread as SOLVED:
    - Click Thread Tools above your first post, select "Mark your thread as Solved".

  4. #4
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Copy only the text of a certain color from a cell

    thanks David - I get my Aussie and Excel spellings mixed up sometimes

+ 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. Replies: 4
    Last Post: 12-29-2013, 11:41 PM
  2. [SOLVED] in need of macro to copy a row of data to another sheet based on text color of one cell
    By fredderf81 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-23-2013, 01:11 PM
  3. Replies: 0
    Last Post: 09-26-2012, 01:08 PM
  4. Replies: 0
    Last Post: 02-16-2006, 12:00 AM
  5. change text color based on adjacent cell text color
    By matthewst in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-01-2005, 03:49 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