+ Reply to Thread
Results 1 to 14 of 14

VB Code to highlight the cell

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    VB Code to highlight the cell

    Hello:

    Please refer to attached file.
    I have data in column B,C and D
    I need some VB Code and need to know how to implement so that when enter is pressed at any of the cells in the range then
    it needs to hightlight to yellow.
    Example:

    If i have cursor position at say cell B3 and i pressed enter then that cell B3 needs to be highlighted

    Let me know if you have any questions.
    Thanks.
    Riz
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VB Code to highlight the cell

    Perhaps
    Option Explicit
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim LR  As Long
       If ((Target.Column < 2) And (Target.Column > 4)) Then Exit Sub
       LR = Cells(Rows.Count, 2).End(3).Row
       If ((Target.Row < 2) And (Target.Row > LR)) Then Exit Sub
       Target.Interior.Color = 49407
    End Sub
    Attached Files Attached Files
    - Battle without fear gives no glory - Just try

  3. #3
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to highlight the cell

    Hello PCI:

    Thanks but i need to highlight ONLY when i use ENTER Key on the Cell.
    Please let me know if you have any questions.
    Thanks.

    Riz

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VB Code to highlight the cell

    A remake
    It works for all sheets then it can be done a selection
    in module ThisWorbook

    Option Explicit
    Private Sub Workbook_Open()
        Application.OnKey "~", "ColorCell"
    End Sub
    in a module put
    Option Explicit
    
    Sub ColorCell()
    Dim LR  As Long
       If ((ActiveCell.Column < 2) Or (ActiveCell.Column > 4)) Then Exit Sub
       LR = Cells(Rows.Count, 2).End(3).Row
       If ((ActiveCell.Row < 2) Or (ActiveCell.Row > LR)) Then Exit Sub
       ActiveCell.Interior.Color = 49407
    End Sub
    Attached Files Attached Files
    Last edited by PCI; 04-17-2016 at 03:06 AM. Reason: Text changed to be more explicite

  5. #5
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to highlight the cell

    Hi PCI:

    Tried your attached file and does not seem to work.
    Please refer to attached pic after doing ENTER on cell
    Please let me know if you have any questions.
    Thanks.

    Riz
    Attached Images Attached Images

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,628

    Re: VB Code to highlight the cell

    Works for me. However, you should note that a) it affects all workbooks and b) the key is not cleared on exit.

    Ideally, the Onkey code should be initiated and cleared on worksheet activate and deactivate respectively.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  7. #7
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VB Code to highlight the cell

    After checking again the file it works ....
    To go forward and to care about TMS' comments here a remake.

    in ThisWorkbook

    Option Explicit
    Private Sub Workbook_Open()
        Application.OnKey "~", "ColorCell"
        FileName = ActiveWorkbook.Name
    End Sub
    in a module put
    Option Explicit
    Public FileName  As String
    Sub ColorCell()
       If (Intersect(ActiveCell, Range("B2:D14")) Is Nothing) Then Exit Sub
       If (ActiveSheet.Name <> "Data") Then Exit Sub
       If (ActiveWorkbook.Name <> FileName) Then Exit Sub
       ActiveCell.Interior.Color = 49407
    End Sub
    Attached Files Attached Files
    Last edited by PCI; 04-17-2016 at 07:17 AM.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,628

    Re: VB Code to highlight the cell

    I think you also need this:

    Option Explicit
    Private Sub Workbook_Close()
        Application.OnKey "~"
        FileName = ActiveWorkbook.Name
    End Sub

  9. #9
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to highlight the cell

    Hi PCI and TMS:

    Seems to work.
    Is there a way that it can only highlight if any changes to the cell is made.
    Please let me know if you have any questions.
    Thanks.

    Riz

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,628

    Re: VB Code to highlight the cell

    That would be a Worksheet Change event handler. You specifically said, highlight the cell when you press Enter. A WSCeh would be a whole lot easier.

  11. #11
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VB Code to highlight the cell

    Is next code gives what you need ...?

    Private Sub Worksheet_Change(ByVal Target As Range)
       If (Intersect(Target, Range("B2:D14")) Is Nothing) Then Exit Sub
       Target.Interior.Color = 49407
    End Sub

  12. #12
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to highlight the cell

    Hi PCI:

    Wonderful.....Thanks a lot
    TMS is also greatly appreciated.

    Riz

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,628

    Re: VB Code to highlight the cell

    You're welcome. Thanks for the rep.


    If you'd said this
    Is there a way that it can only highlight if any changes to the cell is made. (post #9)
    in post #1 and/or post #3, you'd have got the WSCeh from me way back. Credit to PCI for his solution ... I wouldn't have thought of that, and I'm not really keen on using OnKey.

  14. #14
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VB Code to highlight the cell

    Good news
    Enjoy Excel

+ 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] Highlight cell based on selected cell - Code required
    By SharpL in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-24-2014, 09:48 AM
  2. [SOLVED] Sheet code to highlight changes to cell and first cell in row
    By arleutwyler in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-21-2013, 10:46 AM
  3. [SOLVED] VBA code to highlight a range when cell value is FB01
    By fatalcore in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-30-2013, 12:21 AM
  4. [SOLVED] VBA code to highlight rows based on a cell
    By mugs62 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-11-2012, 07:08 AM
  5. Replies: 1
    Last Post: 07-12-2012, 03:41 PM
  6. code not working to highlight a cell if blank
    By curbster in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-27-2009, 09:07 PM
  7. Code to highlight cell
    By Denzil in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-12-2008, 11:25 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