+ Reply to Thread
Results 1 to 29 of 29

Change colour in given range when any cell is selected remotely.

  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Change colour in given range when any cell is selected remotely.

    I'm hoping someone can offer some VBA advice for something I'm trying to do in my worksheet.

    Essentially , if any cell in the range A3:O27 is selected , then cells in the range C1:O1 would be coloured green. Where no cell is selected in A3:O27 is selected then C1:O1 would have no colour.

    Can someone suggest some VBA to achieve this? I was trying with CF but couldn't find a way.

    Grateful for any help.

    Using Excel , Word and Access 2003 - For the whole of 2024

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Change colour in given range when any cell is selected remotely.

    Like this:

    In worksheet module:
    PHP Code: 
    Option Explicit
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Range("C1:O1").Interior
        
    If Intersect(TargetRange("A3:O27")) Is Nothing Then
            
    .Color xlNone
        
    Else
            .
    Color vbGreen
        End 
    If
    End With
    End Sub 
    Quang PT

  3. #3
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Many thanks.

    It successfully colours C1:O1 green when any cell in A3:O27 is selected.

    Unfortunately , when a cell outside A3:O27 is selected it doesn't change C1:O1 back to it's previous state. It leaves C1:O1 coloured lilac , and without gridlines.

    Can you suggest an amendment to toggle it between green and it's original state?

    I'll attach a file to show what I mean.

    Attached Files Attached Files

  4. #4
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Change colour in given range when any cell is selected remotely.

    I tried to copy original color to other cell, i.e, S1. then use S1 cell as source cell.
    The using this:
    PHP Code: 
    Option Explicit
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Range("C1:O1").Interior
        
    If Intersect(TargetRange("A3:O27")) Is Nothing Then
            
    .Color Range("S1").Interior.Color
        
    Else
            .
    Color vbGreen
        End 
    If
    End With
    End Sub 
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Ok thanks.

    This does reinstate the automatic colour to C1:O1 , but not the gridlines. So the gridlines are blank.

    I wonder if there is a way to copy the formatting from S1 also.

    I ran it on sheet2 of my sample so you can see the result attached.

    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Change colour in given range when any cell is selected remotely.

    CDandVinyl,

    try
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Thanks. This does resolve the issue with the gridlines and successfully toggles between green and the previous format in c1:o1.

    For some reason it's not triggering the colour when the cell in the range is selected via a link. It works fine when the cell is selected via the mouse.

    I'll do some more experimenting...

  8. #8
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Change colour in given range when any cell is selected remotely.

    Quote Originally Posted by CDandVinyl View Post
    Ok thanks.

    This does reinstate the automatic colour to C1:O1 , but not the gridlines. So the gridlines are blank.

    I wonder if there is a way to copy the formatting from S1 also.

    I ran it on sheet2 of my sample so you can see the result attached.

    Try ColorIndex instead of Color
    PHP Code: 
    Option Explicit
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Range("C1:O1").Interior
        
    If Intersect(TargetRange("A3:O27")) Is Nothing Then
            
    .ColorIndex Range("S1").Interior.ColorIndex
        
    Else
            .
    Color vbGreen
        End 
    If
    End With
    End Sub 

  9. #9
    Banned User!
    Join Date
    05-03-2022
    Location
    HumptyDoo, Australia
    MS-Off Ver
    365
    Posts
    101

    Re: Change colour in given range when any cell is selected remotely.

    Deleted...already stated above.
    Last edited by GameChanger; 10-31-2023 at 03:28 AM.

  10. #10
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Thanks guys -that's working now. Grateful for your assistance.

    There remains the issue of triggering the colour when the cell is selected via link rather than by mouse. By mouse , the colour is triggered. Selection by link doesn't trigger the colour.

  11. #11
    Banned User!
    Join Date
    05-03-2022
    Location
    HumptyDoo, Australia
    MS-Off Ver
    365
    Posts
    101

    Re: Change colour in given range when any cell is selected remotely.

    Quote Originally Posted by CDandVinyl View Post
    Thanks guys -that's working now. Grateful for your assistance.

    There remains the issue of triggering the colour when the cell is selected via link rather than by mouse. By mouse , the colour is triggered. Selection by link doesn't trigger the colour.
    It works for me. Can you put up a workbook where selection via link doesn't work?

  12. #12
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Ok I've attached a sample file to illustrate the issue. Enter a search number in B4 to search in worksheets. Also click A13 to follow link.

    The idea is to have C1:O1 show green on the affected worksheet when the search is successful and the cell is selected in the range A3:O27 , and clear it where no cell is selected in the range or the worksheet is closed.

    Presently , where the search number is found in the worksheets , it highlights the cell , but does not trigger the colour in C1:O1. I'm trying to find a consistent application of the colour. It seems to work better on the first worksheet and less so on others.

    Grateful for any advice.
    Attached Files Attached Files

  13. #13
    Banned User!
    Join Date
    05-03-2022
    Location
    HumptyDoo, Australia
    MS-Off Ver
    365
    Posts
    101

    Re: Change colour in given range when any cell is selected remotely.

    The reason is because you disable events in your double click event.

  14. #14
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Thnaks for getting back. I tried remming the enable / disable events switches but it's not resolving the issue I'm afraid.

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Change colour in given range when any cell is selected remotely.

    You need to delete 2 lines from Worksheet_BeforeDoubleClick not from change event code.

  16. #16
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Yes , that was my understanding.

    I have already deleted lines from Worksheet_BeforeDoubleClick

    Application.EnableEvents = False

    and

    Application.EnableEvents = False

    but the colour is still not triggered in worksheets where the cells are selected via a link. It does seem to work on the first sheet , but not in others.

    Maybe I'm not understanding. Perhaps a reworked version of the sample file with the fix in place would clarify.


  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Change colour in given range when any cell is selected remotely.

    Strange, it is working.
    Attached Files Attached Files

  18. #18
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Thanks for getting back. It still stubbornly resists here , I'm afraid.

    As an example , it if enter '3456' into B4 on the first sheet and press return , it goes to Spare1!D12 and selects D12 in red. No green response in C1:O1.

    Other presses and clicks do give a response , so it's strange that it only works sporadically.

    At any rate , I'm grateful for your help guys , even though we couldn't quite get to the bottom of it.


  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Change colour in given range when any cell is selected remotely.

    Ahh, I didn't consider that one.
    try replace Worksheet_Change event code with below.
    Please Login or Register  to view this content.

  20. #20
    Banned User!
    Join Date
    05-03-2022
    Location
    HumptyDoo, Australia
    MS-Off Ver
    365
    Posts
    101

    Re: Change colour in given range when any cell is selected remotely.

    Quote Originally Posted by CDandVinyl View Post
    Thanks for getting back. It still stubbornly resists here , I'm afraid.

    As an example , it if enter '3456' into B4 on the first sheet and press return , it goes to Spare1!D12 and selects D12 in red. No green response in C1:O1.

    Other presses and clicks do give a response , so it's strange that it only works sporadically.

    At any rate , I'm grateful for your help guys , even though we couldn't quite get to the bottom of it.

    I think I understand your issue.

    Don't forget that the selection change event on a sheet is only triggered if the selection CHANGES not on sheet activation.

    So if you disable macros and select box 7780 on sheets Spare2 (for example) then save the workbook, box 7780 will still be the selected cell on that sheet and it will not show green because you disabled macros.
    The next time you open your workbook, if you find box 7780, the selection will not be 'changed', so your selection change event will not trigger and you won't get those cells turned green.

    This scenario will also cause your workbook open event to fail with an error 1004. To cover all bases, in addition to the code mods above, change your workbook open event to;
    Please Login or Register  to view this content.
    Last edited by GameChanger; 11-01-2023 at 08:52 PM.

  21. #21
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Thanks guys. I put the amendments in place and it's running fine now.

    With search terms that are found it successfully makes C1:O1 green and cancels this on return to B4. This happens on all sheets which is perfect.

    Small anomaly. When an unknown search term is entered , the message box fires and C1:O1 goes green. On cancelling the message box , the cursor returns to B4 but C1:O1 stays green and doesn't clear. Strange.

    It would be better if C1:O1 were red for search terms that were not found at any rate , but I'm puzzled as to why it doesn't clear when it selects B4 after closing the message box.

    .
    Attached Files Attached Files
    Last edited by CDandVinyl; 11-01-2023 at 09:32 PM.

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Change colour in given range when any cell is selected remotely.

    See if the attached works as you expect.
    Attached Files Attached Files

  23. #23
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Thanks jindon.

    The issue occurs after a search is not found.

    Enter 9999 to B4 and cancel the message box. C1:O1 goes green and doesn't cancel on return to B4.

    I don't know why it doesn't cancel.

    It should have no colour or go red after the message box.

    Last edited by CDandVinyl; 11-01-2023 at 11:01 PM.

  24. #24
    Banned User!
    Join Date
    05-03-2022
    Location
    HumptyDoo, Australia
    MS-Off Ver
    365
    Posts
    101

    Re: Change colour in given range when any cell is selected remotely.

    It is because your selection change is triggered and you are exiting if the intersect with B4 is Nothing. Which is FALSE

    It should say if NOT intersect...is nothing. That will then be TRUE and will exit as expected.

  25. #25
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Thanks GameChanger. Good idea!

    I imagine amending intersects might affect some other behaviours though. Perhaps an intersect to deal with message box issue. I'll give it a go.

  26. #26
    Banned User!
    Join Date
    05-03-2022
    Location
    HumptyDoo, Australia
    MS-Off Ver
    365
    Posts
    101

    Re: Change colour in given range when any cell is selected remotely.

    Do you understand?

    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    in your Selection Change event handler.

  27. #27
    Banned User!
    Join Date
    05-03-2022
    Location
    HumptyDoo, Australia
    MS-Off Ver
    365
    Posts
    101

    Re: Change colour in given range when any cell is selected remotely.

    Quote Originally Posted by CDandVinyl View Post
    Thanks GameChanger. Good idea!

    I imagine amending intersects might affect some other behaviours though. Perhaps an intersect to deal with message box issue. I'll give it a go.
    Well, it may well do. That is what testing is all about and you know the behaviour you want, we are just guessing.

    You can disable events just after the message display (before the select B4), then enable events immediately after.'

    Plenty of ways to skin this cat.
    Last edited by GameChanger; 11-01-2023 at 10:59 PM.

  28. #28
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    Yes , I agree.

    After a successful search triggers the colours , it's the return to B4 which cancels the colours. This works perfectly.

    After an unsuccessful search and the message box to show this , the return the B4 doesn't cancel the colours. So that's the area to test , as you say.

    Presently , the intersect changes I'm trying are having wider effects than just on the unsuccessful search issue , but I'll keep at it. There will be a solution I'm sure , just a question of finding it.


  29. #29
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Change colour in given range when any cell is selected remotely.

    OK I've managed to fix the issue where the colour was not resetting for unsuccessful searches.

    I used the count of successful results in B7. If this resolves to zero then this code cancels the colour :

    Please Login or Register  to view this content.
    This resolves the issue without impacting any other function.

    Thanks for your help guys , and for sticking with me. Problem solved!


+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Change Sheet Tab Colour If Any Cell Value In Range is Higher than Adjacent Cell
    By Storm08 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2022, 05:31 AM
  2. Change Background Colour of a Cell, when another cell in the same Row is selected.
    By Logan1968 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-28-2022, 07:45 PM
  3. Change Selected Cell Colour ...only whilst selected
    By mrgrotey in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-27-2017, 07:40 AM
  4. Replies: 6
    Last Post: 04-11-2016, 09:48 AM
  5. have a cell change colour when selected?
    By Farnarkler in forum Excel General
    Replies: 1
    Last Post: 03-04-2008, 12:43 PM
  6. remotely change date range in Pivot Tables
    By AussieExcelUser in forum Excel General
    Replies: 0
    Last Post: 10-20-2006, 01:19 AM
  7. Replies: 3
    Last Post: 07-17-2006, 03:30 AM

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