1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Excel 2007 macro - locate specific word & underline it

Discussion in 'Business Applications' started by Brad3, Jun 15, 2011.

Thread Status:
Not open for further replies.
  1. Brad3

    Brad3 Thread Starter

    Jun 15, 2011
    How do you make a macro that does the following:
    1. go to next cell down in Column D
    2. copy contents (a single word) of that cell
    3. select entire Column A
    4. find first instance, in Column A, of the word that was copied from Column D, above, (the word is part of a sentence in a single cell in Column A),
    5. underline just that word (not the whole sentence) in Column A
    6. loop
  2. Keebellah

    Keebellah Hans Trusted Advisor

    Mar 27, 2008
    Hi Brad, welcome to the forum

    I just recorded a amcro that does that and translated it to vba code

    Function ULinetxt(cVal As String, uStr As String)
    Dim tp1 As Integer, tp2 As Integer
    If InStr(1, LCase(cVal), LCase(uStr)) > 0 Then
        tp1 = InStr(1, cVal, uStr)
        tp2 = Len(uStr)
        With ActiveCell.Characters(Start:=tp1, Length:=tp2).Font
            .Underline = xlUnderlineStyleSingle
        End With
    End If
    End Function
    Sub test()
        Call ULinetxt(ActiveCell.Value, "underline")
    End Sub
    You could use this function in vba macro and it's done

    Select a cell with your data string and rin the test macro, replace the word "underline" with the text you want underlined and the first occurrence (if found) will be underlined, case is not matched.

    Let me know it it works for you.
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1002417