Office

  • Office 2010
  • Office
  • All forums
Question

event

Hi all,

 

In B57:B96 I have names. In G57:G96 are the same names, but in a different order. When I activate (click) one of the names in G I want the background color of the  B-cell with the same name to become yellow but only so long as the G-cell is activated.

Even better would be that the B-cell background is yellow as long as the cursor is over the G-cell with the same name (so without having to activate that G-cell).

Please help me with the code I need. Thanks in advance.

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation
1 Person had
this question

Was this helpful?

Answer
I wasn't originally thinking of a multiple selection in G57:G96, but that should be accounted for. Here is a multiple selection modification to my originally posted snippet.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range("B57:G96").Interior.Pattern = xlNone
    If Not Intersect(Target, Range("G57:G96")) Is Nothing Then
        For Each gc In Target
            For Each bc In Range("B57:B96")
                If bc.Value = gc.Value Then bc.Interior.Color = 65535
            Next bc
        Next gc
    End If
End Sub

I've edited by original response to reflect this modification.
    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Jeeped

Community Moderator

Was this helpful?

Answer

Hi all,

 

In B57:B96 I have names. In G57:G96 are the same names, but in a different order. When I activate (click) one of the names in G I want the background color of the  B-cell with the same name to become yellow but only so long as the G-cell is activated.

Even better would be that the B-cell background is yellow as long as the cursor is over the G-cell with the same name (so without having to activate that G-cell).

Please help me with the code I need. Thanks in advance.


Here's an approach that uses a variant of a function that is not often useful but is perfect for this scenario.  The code support is minimal and maybe you can even do without it.

The key is to use the CELL function as part of a conditional formatting formula.

My test: Highlight the data in C1:C20 that contained the same value as a selected cell in the range G1:G20.

So, select C1:C20 and create a conditional formatting formula

=IF(CELL("col")=COLUMN(G:G),C1=CELL("contents"),FALSE)

For the c.f. format I picked a background fill color.

Now, click on any cell in G1:G20 and press F9, which calculates the worksheet.  The cells in C1:C20 with the same value as the selected cell in G will have the background fill visible.

 

To skip having to press the F9 key, add the code below in the worksheet's code module:

 

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Target.Calculate
    End Sub

 

 

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Tushar Mehta

Tushar Mehta MVP
Tushar Mehta (MVP Excel)
Excel and PowerPoint tutorials and add-ins
www.tushar-mehta.com

Was this helpful?

1

Vote

Sorry, can't do much with just hovering over the cell.  But we can deal with one being selected.  This is worksheet event code, so to put it into your workbook, open the workbook, select the sheet you need it to work on and right-click the sheet's name tab and choose [View Code] from the popup list.

Copy the code below and paste it into the code module presented to you.  It must be the only

Private Sub Worksheet_SelectionChange(byVal Target As Range)

routine for that worksheet.  If you already have one for it, we'll have to combine the existing code with this code.

 

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim foundMatch As Range
 
  'reset all cells in column B, rows 57 through 96 to "no fill"
  Range("B57:B96").Interior.ColorIndex = xlAutomatic
  If Target.Cells.Count <> 1 Or Target.Column <> 7 Or _
   Target.Row < 57 Or Target.Row > 96 Then
    Exit Sub
  End If
  'one cell in G57:G96 has been selected
  Set foundMatch = Range("B57:B96").Find(what:=Target.Value, LookIn:=xlValues, lookat:=xlWhole)
  If Not foundMatch Is Nothing Then
    If Not IsEmpty(foundMatch) Then
      foundMatch.Interior.ColorIndex = 6 ' yellow
    End If
  End If
End Sub

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

JLLatham

I am free because I know that I alone am morally responsible for everything I do. R.A. Heinlein

Was this helpful?

There is no event triggered by a hover but there is one for a change in selection that should do the job for you.

Right click the worksheet's name tab and select View Code. Paste the following into the right-hand window pane titled something like Book1 - Sheet1 (Code),

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range("B57:G96").Interior.Pattern = xlNone
    If Not Intersect(Target, Range("G57:G96")) Is Nothing Then
        For Each gc In Target
            For Each bc In Range("B57:B96")
                If bc.Value = gc.Value Then bc.Interior.Color = 65535
            Next bc
        Next gc
    End If
End Sub

Press Alt+Q to return to your worksheet.

Selecting any cell within G57:G96 will highlight the corresponding value in B57:B96 with a yellow fill. Selecting a cell outside of G57:G95 will remove all highlighting from B57:B96.


Edit: I combined some commands to shorten the code

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Jeeped

Community Moderator

Was this helpful?

Answer
I wasn't originally thinking of a multiple selection in G57:G96, but that should be accounted for. Here is a multiple selection modification to my originally posted snippet.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Range("B57:G96").Interior.Pattern = xlNone
    If Not Intersect(Target, Range("G57:G96")) Is Nothing Then
        For Each gc In Target
            For Each bc In Range("B57:B96")
                If bc.Value = gc.Value Then bc.Interior.Color = 65535
            Next bc
        Next gc
    End If
End Sub

I've edited by original response to reflect this modification.
    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Jeeped

Community Moderator

Was this helpful?

Answer

Hi all,

 

In B57:B96 I have names. In G57:G96 are the same names, but in a different order. When I activate (click) one of the names in G I want the background color of the  B-cell with the same name to become yellow but only so long as the G-cell is activated.

Even better would be that the B-cell background is yellow as long as the cursor is over the G-cell with the same name (so without having to activate that G-cell).

Please help me with the code I need. Thanks in advance.


Here's an approach that uses a variant of a function that is not often useful but is perfect for this scenario.  The code support is minimal and maybe you can even do without it.

The key is to use the CELL function as part of a conditional formatting formula.

My test: Highlight the data in C1:C20 that contained the same value as a selected cell in the range G1:G20.

So, select C1:C20 and create a conditional formatting formula

=IF(CELL("col")=COLUMN(G:G),C1=CELL("contents"),FALSE)

For the c.f. format I picked a background fill color.

Now, click on any cell in G1:G20 and press F9, which calculates the worksheet.  The cells in C1:C20 with the same value as the selected cell in G will have the background fill visible.

 

To skip having to press the F9 key, add the code below in the worksheet's code module:

 

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Target.Calculate
    End Sub

 

 

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Tushar Mehta

Tushar Mehta MVP
Tushar Mehta (MVP Excel)
Excel and PowerPoint tutorials and add-ins
www.tushar-mehta.com

Was this helpful?

Thanks, Tushar, it works nicely. Especially because your code causes the background color to disappear automatically when another cell is clicked.
    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Was this helpful?

Thanks Jeeped, after I added

Dim bc As Range
Dim gc As Range

it worked like a charm.

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Was this helpful?

Jeeped,

 

I expected there would be an event triggered by a "hoover" because hoovering works as a trigger in the case of a cell comment. Strange.

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Was this helpful?

Indeed JL, foundmatch does the trick.

I expected there would be an event triggered by a "hoover" because hoovering works as a trigger in the case of a cell comment. Strange.

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Was this helpful?

Sorry that I can't mark three replies as an answer, your reply certainly deserved to be marked as an answer ;–(

Hope you will nevertheless assist me in future occasions  ;–)

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

Was this helpful?

Just like high school - first in line, last to get picked <grin>.  Can I get a "Helpful"?

Of course I'll continue to assist you when I can.

I am curious whether or not you needed more than one cell to highlight at a time since my code only did one at a time and Jeeped offered one with multiple highlight capability.

    • Child exploitation or abuse
    • Harassment or threats
    • Inappropriate/Adult content
    • Nudity
    • Profanity
    • Software piracy
    • SPAM/Advertising
    • Virus/Spyware/Malware danger
    • Other Term of Use or Code of Conduct violation

JLLatham

I am free because I know that I alone am morally responsible for everything I do. R.A. Heinlein
<< PreviousPage of 2 Next >>

Message marked as answers cannot be deleted

To delete this message, first unmark this message as an answer, then delete it.

Reason to remove escalation


Merge

Enter the thread ID of the thread you are merging into


Reply will be posted to a public thread

You are replying to a public portion of this thread. To reply privately, click Cancel, click the Private Messages tab, and Reply on that private message.

Don't show this message again

To report abuse, sign in or continue without signing in

Thank you.

Report abuse

Abuse type:

Details (optional):

Report abuse

Abuse type:

Details (required):
Enter the characters you see (required):
Type the numbers that you see in the picture.
Play audio and type the numbers that you hear.
Show a different picture.

Sign in

Hotmail, Xbox Live, Messenger, or msn accounts will also work.

Don't have one of the above accounts?

Signing in...
This page will automatically update after you are signed in.
If you are having problems, you can close this message and try to connect again.