+ Reply to Thread
Results 1 to 5 of 5

Excel 2007 : Highlighting Specific Collumns Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    05-31-2012
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Highlighting Specific Collumns Macro

    I am trying to use a macro that will highlight columns E and I of the rows that collumn L equals "XXXXXXX." How can I designate the columns to tie into the row?

    Sub highlight()
    
    Dim rg As Range, c As Range
      Dim firstAddress As String
        Set rg = Range("L9", "L5000")
        Application.ScreenUpdating = False
    With rg
        Set c = .Find("XXXXXXX", lookat:=xlWhole, LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Columns.Range("E", "I").Interior.ColorIndex = 4 "ISSUE IS HERE
                
                Set c = .FindNext(c)
                If c Is Nothing Then Exit Do
            Loop While c.Address <> firstAddress
        End If
    Application.ScreenUpdating = True
    End With
    
    End Sub
    Last edited by Paul; 06-04-2012 at 06:09 PM. Reason: Added CODE tags for new user. Please do so yourself in the future.

  2. #2
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Highlighting Specific Collumns Macro

    Here you go, this should fix it!
    
    Sub highlight()
    
    Dim rg As Range, c As Range
    Dim firstAddress As String
    Set rg = Range("L9", "L5000")
    Application.ScreenUpdating = False
    With rg
    Set c = .Find("XXXXXXX", lookat:=xlWhole, LookIn:=xlValues)
    If Not c Is Nothing Then
    firstAddress = c.Address
    Do
    'column I is -3 columns from L
    c.Offset(0, -3).Interior.ColorIndex = 4
    
    'column I is -7 columns from L
    c.Offset(0, -7).Interior.ColorIndex = 4
    
    Set c = .FindNext(c)
    If c Is Nothing Then Exit Do
    Loop While c.Address <> firstAddress
    End If
    Application.ScreenUpdating = True
    End With
    
    End Sub
    Let me know if this works for you

  3. #3
    Registered User
    Join Date
    05-31-2012
    Location
    Philadelphia, PA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Highlighting Specific Collumns Macro

    Yes that worked. Thanks for the help!

  4. #4
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Highlighting Specific Collumns Macro

    I think using conditional formatting in this case will be a lot easier than VBA.

    I would create conditional formatting using row relative cell reference for columns E and I and then copy it down.

    for example for E5: if $L5 = "XXXXXX" then apply conditional format (note missing "$" sign before 5)
    Regards,
    Vandan

  5. #5
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Highlighting Specific Collumns Macro

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    HTH
    Regards, Jeff

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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