Consulting

Results 1 to 7 of 7

Thread: Solved: highlighting a row in excel if selected

  1. #1

    Arrow Solved: highlighting a row in excel if selected

    Hi everybody,

    I just wanna get some help about highlighting a row with a VBA code on excel if a cell of that row is selected. I got that code and that works but I don't want the entire row! For example, from A3 to AC3 and only in the rows 3 to 88. Wherever I click, the entire row is highlighted and even the highest row with my titles and the row underneath my tab change color as well. I just want A to AC and row 3 to 88. Is that possible with that formula to change something to get what I want... or I'm gonna need something else or that just impossible.

    Thx for your help!


    [VBA]Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Static rr
    Static cc
    If cc <> "" Then

    With Rows(rr).Interior
    .ColorIndex = 50
    End With
    End If
    r = Selection.Row
    c = Selection.Column
    rr = r
    cc = c
    With Rows(r).Interior
    .ColorIndex = 20
    .Pattern = xlSolid
    End With
    End Sub[/VBA]

  2. #2
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    Try this one;
    [vba]Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Static prevRange As Range

    If Not prevRange Is Nothing And prevRange.Row <= 88 And prevRange.Row >= 3 Then prevRange.Interior.ColorIndex = 50

    If Not Intersect(Target, Range("a3:ac88")) Is Nothing Then
    Range(Cells(Target.Row, "a"), Cells(Target.Row, "ac")).Interior.ColorIndex = 20
    End If

    Set prevRange = Range(Cells(Target.Row, "a"), Cells(Target.Row, "ac"))


    End Sub
    [/vba]
    Last edited by mbarron; 02-17-2010 at 09:53 AM. Reason: Added row limitation to prevRange

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Hi,

    Thx for your quick reply but I tried and It doesn't work... There's a bug about that line :

    If Not prevRange Is Nothing And prevRange.Row <= 91 And prevRange.Row >= 3 Then prevRange.Interior.ColorIndex = 50

    Thx for helping me

    Quote Originally Posted by mbarron
    Try this one;
    [vba]Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Static prevRange As Range

    If Not prevRange Is Nothing And prevRange.Row <= 88 And prevRange.Row >= 3 Then prevRange.Interior.ColorIndex = 50

    If Not Intersect(Target, Range("a3:ac88")) Is Nothing Then
    Range(Cells(Target.Row, "a"), Cells(Target.Row, "ac")).Interior.ColorIndex = 20
    End If

    Set prevRange = Range(Cells(Target.Row, "a"), Cells(Target.Row, "ac"))


    End Sub
    [/vba]

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Put the curser in the word "Not" on your code page and hit the F1 key.

    SamT

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    TRy this

    [vba]

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Static prevRange As Range

    If Not prevRange Is Nothing Then

    If prevRange.Row <= 88 And prevRange.Row >= 3 Then prevRange.Interior.ColorIndex = xlColorIndexNone
    End If

    If Not Intersect(Target, Range("a3:ac88")) Is Nothing Then
    Range(Cells(Target.Row, "a"), Cells(Target.Row, "ac")).Interior.ColorIndex = 20
    End If

    Set prevRange = Range(Cells(Target.Row, "a"), Cells(Target.Row, "ac"))
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7

    It's working!

    Thx everybody for your help, it was my first visit on that website and it's helpful and very very quick. So thx a lot, it has been awesome guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •