+ Reply to Thread
Results 1 to 17 of 17

VBA code for highlight current row

  1. #1
    Forum Contributor keshavtale's Avatar
    Join Date
    03-13-2013
    Location
    india
    MS-Off Ver
    excel 2019
    Posts
    597

    VBA code for highlight current row

    please see attached file in the attached file you can see seventh row highlighted in yellow color like that highlight current row in yellow color if i move to 8 number
    row then highlight that row but color should be disappear of previous row and should be only current row highlight in yellow color if i move to tenth row it should highlighted in yellow color please give me VBA code for that process thanks in advance
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,250

    Re: VBA code for highlight current row

    Try ....

    Please Login or Register  to view this content.
    Click on column D
    Attached Files Attached Files
    Last edited by JohnTopley; 09-24-2024 at 04:34 AM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

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

    Re: VBA code for highlight current row

    You need to store the old color into a variable.
    Copy this code in sheet1 module
    PHP Code: 
    Option Explicit
    Dim r 
    As Long ' Variable to store the previously selected row
    Dim originalColors() As Variant ' 
    Array to store the original background color of each cell in the row

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        
    Dim currentR As Range
        Dim i 
    As Long ' Index for looping through the columns
        Dim numCols As Long ' 
    Number of columns in the row

        
    ' Check if the selected cell is within the range A5:AI100
        If Intersect(Target, Range("A5:AI100")) Is Nothing Then Exit Sub

        ' 
    Determine the number of columns in the row (from column A to column AI)
        
    numCols Range("A5:AI5").Columns.Count

        
    ' If r > 0 (meaning a row was previously selected), restore the color of the previous row
        If r > 0 Then
            Set currentR = Range(Cells(r, 1), Cells(r, "AI"))
            For i = 1 To numCols
                ' 
    Restore the original color of each cell in the previous row
                currentR
    .Cells(1i).Interior.Color originalColors(1i)
            
    Next i
        End 
    If

        
    ' Update r to the currently selected row
        r = Target.Row

        ' 
    Save the original background color of each cell in the newly selected row
        ReDim originalColors
    (1 To 11 To numCols' Initialize the array to store colors
        For i = 1 To numCols
            originalColors(1, i) = Cells(Target.Row, i).Interior.Color
        Next i

        ' 
    Highlight the entire newly selected row in yellow
        Range
    (Cells(Target.Row1), Cells(Target.Row"AI")).Interior.Color vbYellow
    End Sub 
    Attached Files Attached Files
    Quang PT

  4. #4
    Forum Contributor keshavtale's Avatar
    Join Date
    03-13-2013
    Location
    india
    MS-Off Ver
    excel 2019
    Posts
    597

    Re: VBA code for highlight current row

    I do not need to store old color just want to color current row if i am in row 1 only show that row in yellow color if i move to row 2 then only show row 2 in yellow color
    but at that time do not show row 1 in yellow color also i want that if i am moving from any cell for example i am in cell A2 Or B2 Or C2 Or D2 or any cell in row to this code should be work if i am in cell e5 or g5 then only highlight row 5 please do this changes in this VBA code

  5. #5
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,250

    Re: VBA code for highlight current row

    Did you try post #2 code? Could easily be changed to any cellse;lection in a row (rather than just column D).

    Please Login or Register  to view this content.
    Updated code

  6. #6
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,176

    Re: VBA code for highlight current row

    Maybe this would suffice?

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor keshavtale's Avatar
    Join Date
    03-13-2013
    Location
    india
    MS-Off Ver
    excel 2019
    Posts
    597

    Re: VBA code for highlight current row

    Thanks Mr. Bytemarks but i am confuse whom i give reputation point you four person help me but now i giving you reputation point
    Last edited by keshavtale; 09-24-2024 at 05:36 AM.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,034

    Re: VBA code for highlight current row

    @John...
    You can replace...
    Please Login or Register  to view this content.
    With...
    Please Login or Register  to view this content.
    Also best to make use of...
    Please Login or Register  to view this content.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  9. #9
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,250

    Re: VBA code for highlight current row

    @Sintek: having investigated "CountLarge" I view it in the same way as Excel having 1M rows/16K columns: of no value to anyone as volumes of that size would cripple Excel!

    Pure mathematics vs Applied Mathematics!

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,034

    Re: VBA code for highlight current row

    @ John...True...guessing nobody else...like me...Will select all [triangle] in a sheet lol...

  11. #11
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,176

    Re: VBA code for highlight current row

    Quote Originally Posted by keshavtale
    but now i giving you reputation point
    Thank you. Pleased to have contributed.

    Quote Originally Posted by JohnTopley
    having investigated "CountLarge"
    I use it because it's an easy way to avoid the possibility of an overflow error.
    Last edited by ByteMarks; 09-24-2024 at 06:30 AM.

  12. #12
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,250

    Re: VBA code for highlight current row

    @bytemarks: agree but an overflow error of that magnitude is unlikely (IMHO). Equally, I except it is best (good) practice and does no harm!

    It would be interesting to know from the forum professioinals what the maximum size spreadsheet that they have encountered : in partucular number of columns as we do see 500K+ rows occassionally but I have yet to see even 1000 columns.

  13. #13
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,034

    Re: VBA code for highlight current row

    but I have yet to see even 1000 columns.
    Cannot imagine the need for this...not even with a database...but then again...with the amount of unusual postings on this forum...anything is possible lol...

  14. #14
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,176

    Re: VBA code for highlight current row

    It would be interesting to know. Never even come close to 500 columns. I think the max I've encountered legitimately is around 150. The most rows was 800k some years ago in Excel 2007 which was...er...interesting.

  15. #15
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,250

    Re: VBA code for highlight current row

    @sintek/@bytemarks: Thank you for your responses

    .... so I wonder what were MS thinking when they increased the row/column sizes to the current values ?

  16. #16
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,034

    Re: VBA code for highlight current row

    ...Interesting...

  17. #17
    Forum Expert ByteMarks's Avatar
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,176

    Re: VBA code for highlight current row

    Quote Originally Posted by JohnTopley
    I wonder what were MS thinking when they increased the row/column sizes to the current values
    I think Bill Gates just looked at his current bank balance before the decision meeting and chose a number of cells to match.

+ 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. VBA code to highlight the individual column in range based on current date
    By UNNI_UNNI in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-12-2023, 02:31 PM
  2. Highlight current row
    By tcebob in forum Excel General
    Replies: 8
    Last Post: 09-07-2018, 09:49 PM
  3. [SOLVED] Highlight current row
    By keshavtale in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-13-2018, 12:44 AM
  4. Replies: 3
    Last Post: 09-16-2017, 01:03 PM
  5. Code to insert current date (but NOT current time)
    By mjwillyone in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-13-2013, 09:37 AM
  6. [SOLVED] add current time (hh:mm:ss) as well as current date (dd:mm:yyyy) into the code
    By papasideris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2012, 02:29 PM
  7. How do I highlight the current cell?
    By psilzle in forum Excel General
    Replies: 2
    Last Post: 08-25-2005, 03:05 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