Consulting

Results 1 to 4 of 4

Thread: by click on cell of column a the color of row change

  1. #1
    VBAX Mentor
    Joined
    Feb 2012
    Posts
    406
    Location

    Question by click on cell of column a the color of row change

    I need VBA code that when I click on column a on each cell a1 to a... the color of that row will be yellow .
    and if click back the color will not change .
    example :
    when click on column a cell a1 the color of row that row will be yellow.
    thank you

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    To do this with a single left-click would be possible with a selection_change event but it wouldn't be just a click that would cause a colour change; moving around the sheet with the arrow keys would also trigger a colour change.
    If your happy to accept ewither a right-click or a double-click them one of these macros in the sheet concerned's code-module might do it for you:[VBA]Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 1 Then
    Cancel = True
    Target.EntireRow.Interior.ColorIndex = 6
    End If
    End Sub
    [/VBA]or
    [VBA]Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column = 1 And Target.Cells.Count = 1 Then
    Cancel = True
    Target.EntireRow.Interior.ColorIndex = 6
    End If
    End Sub
    [/VBA]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Another possibility is you could also use a modeless userform with a button to change the color of the row of the Activecell.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    In both of p45cal's subs, change this line[vba]Target.EntireRow.Interior.ColorIndex = 6[/vba]
    To this code[vba]With Target.EntireRow.Interior
    If .ColorIndex = 6 Then
    .ColorIndex = xlColorIndexNone
    Else
    .ColorIndex = 6
    End If
    End With[/vba]
    Since you have to double click a cell to edit the contents, (as opposed to replacing them,) and since you have to right click a cell to do many things to the cell, you may want more control of when to color the rows.

    This sub requires that you first double click one cell in Column "A" before right clicking the same cell.[vba]Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    'Must first double click one cell in column "A" for this to run

    If Target <> ActiveCell Then Exit Sub
    Cancel = True

    With ActiveCell.EntireRow.Interior
    If .ColorIndex = 6 Then
    .ColorIndex = xlColorIndexNone
    Else
    .ColorIndex = 6
    End If
    End With

    End Sub
    [/vba]
    You can combine the two Mouse events by first double clicking a cell to Activate it then right clicking it to trigger the color change sub only on the Active Cell.
    Last edited by SamT; 07-15-2013 at 08:07 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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