+ Reply to Thread
Results 1 to 16 of 16

Highlight active row but after leaving it give its previous color back

  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Highlight active row but after leaving it give its previous color back

    Hi

    In the attached workbook I have a table to a specific row but after that table I have another section with blue background. I am using the bellow code to highlight the activerow to specific number of columns.
    Please Login or Register  to view this content.
    PROBLEM: The code is working fine as far the table is concerned. but when my selection goes down in the blue section it overright the color
    What I want is to set the color back to the range's previous interior color

    Help would be greatly appreciated.


    Best Regards
    Imran Bhatti
    Attached Files Attached Files
    Last edited by ImranBhatti; 09-15-2017 at 10:50 AM. Reason: correction in code
    Teach me Excel VBA

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Highlight active row but after leaving it give its previous color back

    Something like this:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Highlight active row but after leaving it give its previous color back

    Hi XlAdept

    Lemme be more clearer
    My mistake.

    I included the table just to show that the code works on the table but have a deficiency when it comes to ordinary Range . My original workbook does not have a table for some reasons.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Highlight active row but after leaving it give its previous color back

    Well, I wrote "something like this". The idea is to exit the procedure before reassigning the static range.

    Without the actual situation it's difficult to assess the problem

  5. #5
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Highlight active row but after leaving it give its previous color back

    In the range you are wanting this to apply

    Are all the cells in the same row all the same colour?

    Different rows can be different colours - I am asking about each row separately
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  6. #6
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Highlight active row but after leaving it give its previous color back

    xladept and kev thanks for the review

    Range("A" & Target.Row & ":I" & Target.Row) all the 9 cells in this row will have only one color but the next row may have different color than the previous one .

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Highlight active row but after leaving it give its previous color back

    Does this help?

    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Highlight active row but after leaving it give its previous color back

    Here is a way to do what you have described using conditional formatting (CF)
    - CF avoids requirement to capture the existing range colour etc

    - one CF rule is used - based on value in column A (which is hidden)
    - would probably need to add some VBA to force this CF rule to top priority if you are already using CF

    and the VBA:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by kev_; 09-15-2017 at 07:02 PM.

  9. #9
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Highlight active row but after leaving it give its previous color back

    Here is a method NOT using conditional formatting which
    - uses 2 named ranges to store previous "row range" and original "colour" information

    Test in attached workbook

    Please Login or Register  to view this content.
    NOTE - do not delete a row or the VBA will crash if the named range reference is lost - I will post an update within the next few days to fix this issue
    Attached Files Attached Files

  10. #10
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Highlight active row but after leaving it give its previous color back

    Hi kev
    Intelligent move. Creating separate named ranges is the way to exit sub as xladept said


    Quote Originally Posted by xladept View Post
    Well, I wrote "something like this". The idea is to exit the procedure before reassigning the static range.

    Without the actual situation it's difficult to assess the problem
    It worked perfectly.Thanks to all reviewed my problem.

    Best Regards
    Imran Bhatti

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Highlight active row but after leaving it give its previous color back

    You're welcome!

  12. #12
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Highlight active row but after leaving it give its previous color back

    I looked back at my code and realised I should have used Evauate to make it simpler
    And (to simplifyfurther) named range "PreviousRow" value is now the row number" not a range
    I suggest you edit named range "PreviousRow" value from a range to the previous row number before you amend the code in your file


    working code in attached file
    Please Login or Register  to view this content.
    BEWARE - if you delete a row it is likely that named range "PreviousRow" may contain incorrect value - I will update later
    Attached Files Attached Files
    Last edited by kev_; 09-16-2017 at 02:18 PM.

  13. #13
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Highlight active row but after leaving it give its previous color back

    Hi kev

    I had modified your code with Evaluate to make it simpler and now as I visited with a mind to mention that you already did. emmm Telepathy....


    Thanks again.

  14. #14
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Highlight active row but after leaving it give its previous color back

    What I want to know is:
    - who sent the telepathic message? You or me?

    Final revision which handles row deletion by user (see note below *** if you are deleting rows via VBA)

    What the code does:
    - current "highlight" is removed when a different cell or range is selected
    - resets named range values to match active row

    If selection is more than one cell:
    (this is to prevent issues where rows are deleted)
    - named ranges used are reset to a row outside used range
    - cell used is A999999 (amend this value to 60,000 if you are using older Excel files where last row was 65,536)
    - values are not placed in that cell so it should not increase size of file (not sure with older Excel files )
    - you could replace 999999 to a smaller number outside your anticipated used range
    - if using more than one file type replace 999999 with something like Rows.Count - 10000

    Please Login or Register  to view this content.
    *** Deleting rows in VBA
    - if you delete or insert rows with VBA then you must deal with impact in VBA
    - one way would be to select cell A999999 (with Range("A999999").Select)
    - puts active cell outside used range and resets named ranges accordingly
    - then disable events (with Application.EnableEvents = False)
    - enable events at end of VBA
    Last edited by kev_; 09-18-2017 at 01:34 AM.

  15. #15
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Highlight active row but after leaving it give its previous color back

    And here is a very simple way with conditional formatting
    - Named Range "ActiveRow" refreshed with active row value
    - CF rule AppliesTo A$:I$ , Formula =ROW(A1)=ActiveRow

    Please Login or Register  to view this content.
    attached workbook also includes code setting CF rule as first priority if required
    Attached Files Attached Files

  16. #16
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,784

    Re: Highlight active row but after leaving it give its previous color back

    As your revelations continues , the code is getting better and better. Thank you very much indeed.Now the researchers would find this useful. And be grateful to you....


+ 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. Changing Background Color for Active Cells, then back to Previous Color...
    By TMCinDC in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-13-2016, 11:35 AM
  2. Replies: 11
    Last Post: 01-02-2015, 09:21 PM
  3. Go back to previous active sheet
    By gopal baheti in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-02-2014, 09:43 PM
  4. [SOLVED] back to previous active sheet ?
    By Lukael in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2014, 04:45 PM
  5. [SOLVED] Highlight active cell, un-highlight when no longer active?
    By cdterry in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2013, 02:39 PM
  6. [SOLVED] Go back to previous active sheet (Excel 2007)
    By JawD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-09-2012, 08:00 AM
  7. [SOLVED] change highlight color on active cells
    By Louis in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 11-22-2005, 04:45 PM

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