+ Reply to Thread
Results 1 to 9 of 9

Adding a macro?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-23-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    5

    Adding a macro?

    First off, I'm a relative newbie. I found this macro(?) below and would like to add it to a worksheet because it is supposed to make it possible for me to highlight different words. To customize it to my needs:
    1. where in the string do I add the words that I want highlighted
    2. does it matter if I want to only highlight in one column
    3. once customized how do I make it work on my worksheet.

    Option Explicit
    Sub HighlightCells()
        Dim Lookin As Range, ff As String
        Dim i As Long
        Dim Fnd As Variant
        Dim fCell As Range
        Dim ws As Worksheet
        Dim xitem As Variant
        
       ' Fnd = Array("default", "Google")
     Fnd = Array("Ad hoc", "Adaptable", "Adequate", "And/or", "Approximately", "As a minimum", "As applicable" _
         , "As appropriate", "As possible")
    
        For Each ws In Worksheets
            With ws
                For Each xitem In Fnd
                    Set Lookin = .Cells.Find(xitem, Lookin:=xlValues, LookAt:=xlPart)
                    If Not Lookin Is Nothing Then
                        ff = Lookin.Address
                        Do
                           Lookin.Characters(InStr(1, Lookin, xitem), Len(xitem)).Font.ColorIndex = 3
                            Set Lookin = .Cells.FindNext(Lookin)
                        Loop Until ff = Lookin.Address
                    End If
                    Set Lookin = Nothing
                Next
            End With
        Next
    
    End Sub
    Many thanks!
    Last edited by arlu1201; 10-23-2012 at 01:54 PM. Reason: Use code tags in future.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Adding a macro?

    attach a sample file with before and after desired macro running
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    10-23-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Adding a macro?

    I haven't done anything yet. All I have is a worksheet that I'd like to apply it to. Maybe there's a simpler way to highlight two different words in one column.

  4. #4
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    Re: Adding a macro?

    Start the other way around. Open your file in Excel, press Alt+F11. VBA edito. Look at the left pane which should be a sort of tree representation of the elements. Right click the projec name, select Insert... module. Double click the new module. It's blank. Copy the code above and paste in the module.

    Now you are ready to amend and test. The line starting Fn=Array... has a number of strings in quotes separated by commas. Insert new, change or delete existing stings for your selection of words. Press F5 - the macro will work.

  5. #5
    Registered User
    Join Date
    10-23-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Adding a macro?

    Thank you brynbaker. It worked....accept when a cell's text included something like DIRECTOR OF SALES (sales was chosen to be one of the highlighted words) only DIREC was highlighted not the word sales. It would only do the first few letters like this in other similiar cells in which the chosen highlighted word was not the first word. Any ideas on how to fix this? Thank you!

  6. #6
    Registered User
    Join Date
    10-23-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Adding a macro?

    Quote Originally Posted by brynbaker View Post
    Start the other way around. Open your file in Excel, press Alt+F11. VBA edito. Look at the left pane which should be a sort of tree representation of the elements. Right click the projec name, select Insert... module. Double click the new module. It's blank. Copy the code above and paste in the module.

    Now you are ready to amend and test. The line starting Fn=Array... has a number of strings in quotes separated by commas. Insert new, change or delete existing stings for your selection of words. Press F5 - the macro will work.
    Thank you brynbaker. It worked....accept when a cell's text included something like DIRECTOR OF SALES (sales was chosen to be one of the highlighted words) only DIREC was highlighted not the word sales. It would only do the first few letters like this in other similiar cells in which the chosen highlighted word was not the first word. Any ideas on how to fix this? Thank you!

  7. #7
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    Re: Adding a macro?

    That's nice piece of code you found. The line that's giving you hassle is obviously
    Lookin.Characters(InStr(1, Lookin, xitem), Len(xitem)).Font.ColorIndex = 3
    but I can't fault it. It gets the length of highlight right and the start position should be correct with (InStr(1, Lookin, xitem).

    You asked about single column use; the line to change is
    Set Lookin = .Cells.Find(xitem, Lookin:=xlValues, LookAt:=xlPart)
    and I think it would be to replace .Cells. with .columns(3). [put the column number you need].

  8. #8
    Registered User
    Join Date
    10-23-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Adding a macro?

    So there is no way to change the way it is being highlighted?? (Don't forget -- I'm a xl newbie). Could you do me a favor and write out how the code should read to make the changes? Thank you!

    Quote Originally Posted by brynbaker View Post
    That's nice piece of code you found. The line that's giving you hassle is obviously
    Lookin.Characters(InStr(1, Lookin, xitem), Len(xitem)).Font.ColorIndex = 3
    but I can't fault it. It gets the length of highlight right and the start position should be correct with (InStr(1, Lookin, xitem).

    You asked about single column use; the line to change is
    Set Lookin = .Cells.Find(xitem, Lookin:=xlValues, LookAt:=xlPart)
    and I think it would be to replace .Cells. with .columns(3). [put the column number you need].

  9. #9
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    Re: Adding a macro?

    To limit to one line:
    Set Lookin = .Cells.Find(xitem, Lookin:=xlValues, LookAt:=xlPart)
    I can't fix the highlighting. It could be made to highlight the offending cell - replace the following lines
     
                        Do
                           Lookin.Characters(InStr(1, Lookin, xitem), Len(xitem)).Font.ColorIndex = 3
                            Set Lookin = .Cells.FindNext(Lookin)
                        Loop Until ff = Lookin.Address
    with
                     ff.interior.colorindex = 6
    but that's not what you want, and the existing code should work anyway.
    Best I can do, sorry.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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