• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Find and replace text by color

Ben4576

New Member
I want to build a macro to search my worksheet for Red text and replace the text with a line of code to link the text. Is there any way to find and replace text by color?
 
Hi, Ben4576!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about your question...


If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.


Despite of these, you'd surely want to give a look at this file:

https://dl.dropboxusercontent.com/u/60558749/Find%20and%20replace%20text%20by%20color%20%28for%20Ben4576%20at%20chandoo.org%29.xlsm


It uses a dynamic named range ReferenceTable located in the Reference worksheet, which can be changed within the code. The Update Refs button will search in all worksheets different from Reference cells with font color in red, will add them in the ReferenceTable, and set crossed links from and to the reference and the referrer.

This is the code:

-----

Option Explicit

Sub UpdateRefs()
' constants
Const ksWS = "Reference"
Const ksReference = "ReferenceTable"
Const ksHyperlink1 = "=HYPERLINK("
Const ksLinkPrefix = "#"
Const ksAdmiral = "!"
Const ksComma = ","
Const ksQuote = """"
Const ksHyperlink2 = ")"

' declarations
Dim ws As Worksheet, wsR As Worksheet, rng As Range
Dim lReference As Long, sName As String, sLink As String, sText As String
Dim I As Long, J As Long, C As Range
' start
Set wsR = Worksheets(ksWS)
Set rng = wsR.Range(ksReference)
lReference = rng.Rows.Count
' process
With rng
For I = 1 To ThisWorkbook.Worksheets.Count
Set ws = ThisWorkbook.Worksheets(I)
sName = ws.Name
If sName <> ksReference Then
For Each C In ws.UsedRange
If C.Font.Color = vbRed Then
' position
lReference = lReference + 1
.Cells(lReference, 1).Value = sName
.Cells(lReference, 2).Value = C.Address(False, False, xlA1)
' formula or value
If C.HasFormula Then
.Cells(lReference, 3).Value = C.Formula
Else
.Cells(lReference, 3).Value = C.Value
End If
' links
' in reference
sText = ksHyperlink1 & _
ksQuote & ksLinkPrefix & ws.Name & ksAdmiral & _
C.Address(False, False, xlA1) & _
ksQuote & ksComma & ksQuote & _
Format(C.Value) & _
ksQuote & ksHyperlink2
.Cells(lReference, 3).Formula = sText
' in worksheet
sText = ksHyperlink1 & _
ksQuote & ksLinkPrefix & wsR.Name & ksAdmiral & _
.Cells(lReference, 3).Address(False, False, xlA1) & _
ksQuote & ksComma & ksQuote & _
Format(C.Value) & _
ksQuote & ksHyperlink2
C.Formula = sText
End If
DoEvents
Next C
End If
Set ws = Nothing
Next I
End With
' end
Application.CutCopyMode = False
Set C = Nothing
Set rng = Nothing
Set wsR = Nothing
Beep
End Sub

-----


Just advise if any issue.


Regards!
 
I apologize for breaching any netiquette rules, this is my first stab at posting in a forum. I did perform searches both internal and external of chandoo, but came up fruitless. The coding you provided is, by far, the closest I have gotten to a solution yet.


My excel sheet uses red to designate certain text to be hyperlinked within lines of text in the same cell. The formula you suggested only selects red text for hyperlinking when it does not share a cell with text in other colors. Is it possible to do that?


My end goal is to have the ability to search an excel sheet that is full of text to select the text colored red and place hyperlink coding on either side, without eliminating any text within the cells.
 
Hi, Ben4576!

No problem about the rules, it was your first time here and it was only a guide to help you.

You're now changing and not slightly the specs you firstly wrote. Let me see if I get you now:

- you have cells with text, e.g. "the quick brown fox jumped over the lazy dog"

- as per your original request if all the cell was in read then it should be replaced by an hyperlink and added to a list

- now you say that if only "brown fox" it's in red, the hyperlink appears, for all the cell or just for the text in red?, and if "lazy dog" is in red too?

Regards!
 
Back
Top