+ Reply to Thread
Results 1 to 10 of 10

Changing target cell formatting

Hybrid View

  1. #1
    Registered User
    Join Date
    03-25-2004
    Posts
    54

    Changing target cell formatting

    Hey, guys. What's up?

    I'd like to know if it's possible to do something like this...

    1) I click or simply move the cursor to a certain cell, say, B10.
    2) That cell (B10) gets highlighted (by highlighted, I mean, its background color gets changed into red, for instance).
    3) I leave the cell (B10) by clicking or moving the cursor to another cell, say, B11.
    4) The cell I left (B10) gets its formatting back; same cell formatting it had before I clicked on it.

    teylyn has already helped me with something similar. Take a look, please.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim lrow As Long
    On Error Resume Next
    lrow = Range("A18").End(xlDown).Row
    Application.ScreenUpdating = False
    If Intersect(Target, Range("A18", Range("G" & lrow))) Is Nothing Then
        Rows("18:" & lrow).RowHeight = 13.25
    Else
        Rows("18:" & lrow).RowHeight = 13.25
        Target.RowHeight = 20
    End If
    Application.ScreenUpdating = True
    End Sub
    By the way, it's easy to do this when I already know the target cell formatting. However, if I have a big range of cells with different formatting, it's more complicated (to me, of course). That's why I want to know if it's possible to do it automatically for the whole formatting (background color, font color, borders, everything).

    It would be something like... When I click a cell, Excel copies the formatting of that cell and then changes its background color (so it gets highlighted). Then, when I leave that cell, Excel pastes that formatting back on it.

    I hope you can help. I really don't know how to do that.

    Thank you very much for your attention, guys.

    brunces
    Last edited by brunces; 04-18-2016 at 08:04 PM.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Changing target cell formatting

    Maybe something like this?
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Static col As Long, adr As String
        If Target.Count = 1 Then ' So it works only if 1 cell is selected
            If adr = "" Then
                'reset your range to default format here?
                
            Else
                Range(adr).Interior.Color = IIf(col = 16777215, xlNone, col)
            End If
            col = Target.Interior.Color
            adr = Target.Address
            Target.Interior.Color = 255 ' highlight selected cell red
        End If
    End Sub
    多么想要告诉你 我好喜欢你

  3. #3
    Registered User
    Join Date
    03-25-2004
    Posts
    54

    Re: Changing target cell formatting

    Hey, millz.

    Thank you for your reply.

    I'm trying to adapt your code to my reality here, but it won't work. I don't really know what I'm doing wrong. If you don't mind, could you please give me some help?

    I've attached a file so it gets easier to understand what I want.

    By the way, my intention is to both get the row "zoomed" and the cell "highlighted".

    Thanks again. I appreciate the support.

    brunces
    Attached Files Attached Files

  4. #4
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Changing target cell formatting

    There were several mistakes:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.Count <> 1 Then Exit Sub ' <- use Count, not CountLarge. If you want to exit, then it should be <> 1, not = 1
    
        Static lngColor As Long, lngLastRow As Long, strAddress As String
    
        On Error Resume Next
    
        lngLastRow = Range("A5").End(xlDown).Row
    
        Application.ScreenUpdating = False
        If Not Intersect(Target, Range("A5", Range("G" & lngLastRow))) Is Nothing Then
            If strAddress = "" Then
                'do not exit sub here. If you exit sub, the address would never get saved, not even for the first time.
            Else
                Rows("5:" & lngLastRow).RowHeight = 15
                Range(strAddress).Interior.Color = IIf(lngColor = 16777215, xlNone, lngColor)
            End If
            strAddress = Target.Address
            lngColor = Target.Interior.Color
            Target.Interior.Color = 13421823
            Target.RowHeight = 25
            
        Else ' Added this segment. necessary to reset color if clicking out of "designated range"
            If strAddress = "" Then
                'do not exit sub here.
            Else
                Rows("5:" & lngLastRow).RowHeight = 15
                Range(strAddress).Interior.Color = IIf(lngColor = 16777215, xlNone, lngColor)
            End If
            
        End If
        Application.ScreenUpdating = True
    
    End Sub

  5. #5
    Registered User
    Join Date
    03-25-2004
    Posts
    54

    Re: Changing target cell formatting

    millz, thank you very, very much for your attention. I really appreciate it. Cheers.

    brunces

  6. #6
    Registered User
    Join Date
    03-25-2004
    Posts
    54

    Re: Changing target cell formatting

    Just out of curiosity, millz, could you please explain this to me?

    IIf(lngColor = 16777215, xlNone, lngColor)

    What does this function do? Mainly the numer 16777215. Why that specific number?

    Thanks again.

    brunces

    --- EDITED ---

    By the way, I'm having problems with logging in/posting/editing here. Is this forum facing any kind of problems? :/
    Last edited by brunces; 04-19-2016 at 10:16 AM.

  7. #7
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Changing target cell formatting

    That number represents white. Normally nobody would purposefully colour a cell white, so I take it as anything with that is the default coloured cell. But if you leave that part out, the code would colour a cell back to white, and you may lose some default cell borders. Just manually fill an empty cell white and you will know what I mean.

  8. #8
    Registered User
    Join Date
    03-25-2004
    Posts
    54

    Re: Changing target cell formatting

    Good to know, millz. Thanks again.

  9. #9
    Registered User
    Join Date
    03-25-2004
    Posts
    54

    Re: Changing target cell formatting

    millz, what's up?

    I faced a problem here. When I select the whole sheet (all cells), I get a VBA error and the highlighted line, in yellow, is:

    If Target.Count <> 1 Then Exit Sub
    Is there any way to get rid of this?

    Thanks.

    brunces

  10. #10
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Changing target cell formatting

    It seems using .CountLarge would solve the problem. Apparently it's not doing what it's described as in the help file. My bad on that.

    Range.CountLarge Property - Counts the largest value in a given range of values. Read-only Variant.

    It's actually working the same way as .Count, but with a larger capacity.

+ 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] Changing row height of target cell
    By brunces in forum Excel General
    Replies: 2
    Last Post: 04-14-2016, 07:25 PM
  2. Solver is not changing values in target cell using through VBA
    By brajpalshishodia in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-19-2015, 02:52 PM
  3. Changing Formula based on what is in the target cell
    By 5upgraphics in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-02-2014, 05:54 PM
  4. Replies: 5
    Last Post: 01-22-2013, 11:38 AM
  5. Date format in cell Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    By Hilton1982 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-15-2012, 03:22 AM
  6. Is target.offset my answer? Adapting code to format an adjacent cell.
    By thespianchef in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-19-2012, 04:18 PM
  7. Replies: 5
    Last Post: 04-29-2010, 07:11 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