+ Reply to Thread
Results 1 to 6 of 6

Trying to find text and replace it with the same text only in italics in a worksheet

  1. #1
    Registered User
    Join Date
    08-18-2013
    Location
    Carrollton, Georgia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Trying to find text and replace it with the same text only in italics in a worksheet

    Hello all,
    I am trying to find specific text throughout an excel document (2007) and replace it with the same text but in italics. I tried using the options/format function and selecting italics for the "replace" text but it replaces the entire cell text in italics instead. Can someone offer some assistance?

    Thank you,
    Dianne

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Trying to find text and replace it with the same text only in italics in a worksheet

    I'm not aware of a method to do this without using a macro, although there might be one. a VBA solution would be something like:
    Please Login or Register  to view this content.
    You need to press alt+f11 to bring up the vba editor and then copy and paste the code into a new module and run it (F5).

  3. #3
    Registered User
    Join Date
    08-18-2013
    Location
    Carrollton, Georgia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Trying to find text and replace it with the same text only in italics in a worksheet

    I copied this code into the vba editor and received the following error message when trying to run it:

    Run-time error '91':

    Object variable or With block variable not set

    It highlights the following line of code:

    rng.Characters(InStr(1, rng, search_term), Len(search_term)).Font.Italic = True

    Can you help?

    Thanks,
    Dianne
    Last edited by Dianne Reed; 08-20-2013 at 06:39 PM. Reason: Adding additional info

  4. #4
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Trying to find text and replace it with the same text only in italics in a worksheet

    It worked ok for me when I tested it, you might get that error if the search term doesn't exist? Maybe include an error handling for that:
    Sub macro_1()
    Dim rng, first_rng, search_term, ws
    search_term = "example search text" 'change as appropriate
    For Each ws In ActiveWorkbook.Sheets
    Set rng = ws.Cells.Find(search_term)
    Set first_rng = rng
    if not rng is nothing
    Do
    rng.Characters(InStr(1, rng, search_term), Len(search_term)).Font.Italic = True
    Set rng = ActiveSheet.Cells.FindNext(rng)
    Loop Until first_rng.Address = rng.Address
    end if
    Next
    End Sub

  5. #5
    Registered User
    Join Date
    08-18-2013
    Location
    Carrollton, Georgia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Trying to find text and replace it with the same text only in italics in a worksheet

    I replaced the original code with the new code that you sent and received a syntax error with the following line highlighted:

    if not rng is nothing

    I also double-checked and there are several instances of the search term throughout the document.

    Any other ideas?

    Thank you,
    Dianne

  6. #6
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Trying to find text and replace it with the same text only in italics in a worksheet

    Sorry I don't have any other ideas, I tested it on my machine and it seemed to work ok.

+ 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] VBA to Search a Text String, Find a Value listed in another column and replace the text
    By Jschrod303 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-16-2012, 08:56 PM
  2. find in excel replace in word: find/replace text in text boxes and headers
    By dean.rogers in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-10-2012, 12:40 PM
  3. Find and Replace Text in Formula on a given worksheet in an excel workbook using VBA
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-08-2012, 08:32 PM
  4. Replies: 7
    Last Post: 03-03-2008, 11:48 AM
  5. Replies: 3
    Last Post: 05-22-2006, 02:25 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