+ Reply to Thread
Results 1 to 13 of 13

Format Cell On Select

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Format Cell On Select

    Hello,
    I have a Range called "Types".
    Each cell of "Types" has a certain formatting.
    The "Types" is a List for "Validated Cells".
    I am trying to match the Types format when a selection is made from the Drop Down Box of the validated cell.
    Please see the attached workbook.
    Could you please advise?
    Thanks
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,937

    Re: Format Cell On Select

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Found As Variant
        
        If Not Intersect(Target, Range("C11:H17")) Is Nothing Then
            Application.EnableEvents = False
            Set Found = Range("types").Find(Target.Value)
            Found.Copy
            Target.PasteSpecial _
                Paste:=xlPasteFormats
            
            Application.CutCopyMode = False
            Application.EnableEvents = True
            Set Found = Nothing
        End If
    End Sub
    Ben Van Johnson

  3. #3
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Re: Format Cell On Select

    protonLeah,
    Ingenious!
    You made it so simple; it works perfect.
    Thank you very much!

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Format Cell On Select

    @ protonLeah
    Thanks for great code
    When clear the contents of any cell withing the target range there is an error message appeared and the code stopped working
    "Object variable or with block variable not set" at this line
    Found.Copy
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,937

    Re: Format Cell On Select

    Modified for cleared cells:
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Found As Variant
        
        If Not Intersect(Target, Range("C11:H17")) Is Nothing Then
            Application.EnableEvents = False
            Set Found = Range("types").Find(Target.Value)
            
            If Not Found Is Nothing Then
                Found.Copy
                Target.PasteSpecial _
                    Paste:=xlPasteFormats
                Application.CutCopyMode = False
                Set Found = Nothing
            Else
                With Target.Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With    'target cell interior clear
            End If  'found
        End If  'in validated cells
        Application.EnableEvents = True
    End Sub

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Format Cell On Select

    Thanks a lot for this perfect modification
    It works well now

  7. #7
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Re: Format Cell On Select

    protonLeah,

    I can't figure out what you did but it is just, perfect.
    It is the best I ever have seen in this forum!
    YasserKhalil --> thanks for your help too!
    Last edited by plans; 07-12-2015 at 04:49 PM.

  8. #8
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Re: Format Cell On Select

    protonLeah,
    What I did not realize, is that when I use this, I will need to insert / delete rows and columns within the Range("C11:H17"), so this range area should be able to extend or shrink.
    When I insert or delete rows, columns, then first I am getting an error message and the cells won't update their formats.
    Is there an easy fix to it?
    Thank you

  9. #9
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,937

    Re: Format Cell On Select

    This is just a guess since I don't know what the actual worksheet looks like:
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim Found As Variant, _
            ValAddress  As String
    
    'branch to Function FindValidationCells to get the address of the block of cells
    'with data validation starting at cell Cll
    
            ValAddress = FindValidationCells
        
        If Not Intersect(Target, Range(ValAddress)) Is Nothing Then
            Application.EnableEvents = False
            Set Found = Range("types").Find(Target.Value)
            
            If Not Found Is Nothing Then
                Found.Copy
                Target.PasteSpecial _
                    Paste:=xlPasteFormats
                Application.CutCopyMode = False
                Set Found = Nothing
            Else
                With Target.Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With    'target cell
            End If  'found
        End If  'in validated cells
        Application.EnableEvents = True
    End Sub

    This function starts at cell C11 and tests each cell in that row until it gets to a column with no
    validation formula. It then goes down the rows of that column until a cell with no validation
    formula is found. It returns the address of the block starting with C11 and ending with the
    address of that cell.

    Function FindValidationCells() As String
        Dim TestValidation  As Variant, _
            ValTest         As Boolean
        
        ValTest = True
        Set TestValidation = Range("c11")
        
        While ValTest = True
            ValTest = False
            Set TestValidation = TestValidation.Offset(0, 1)
            On Error Resume Next
            ValTest = (TestValidation.Validation.Formula1 = "=Types")
        Wend
        Set TestValidation = TestValidation.Offset(0, -1)
    
        Err.Clear
        ValTest = True
        
        While ValTest = True
            ValTest = False
            TestValidation.Select
            Set TestValidation = TestValidation.Offset(1)
            On Error Resume Next
            ValTest = (TestValidation.Validation.Formula1 = "=Types")
        Wend
        Err.Clear
        Set TestValidation = TestValidation.Offset(-1)
        Set TestValidation = Range("C11", TestValidation.Address(0, 0))
        FindValidationCells = TestValidation.Address(0, 0)
        Set TestValidation = Nothing
    End Function

  10. #10
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Re: Format Cell On Select

    Thanks for the response.
    This new modification works the same way as before, but actually I figured a "workaround".
    When I insert a column, I am getting a Run-time error' 13': Type mismatch, so what I do, I save the file, exit it and reopen.
    The newly opened file works fine.
    Thanks

  11. #11
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,937

    Re: Format Cell On Select

    Sorry, I forgot to test that only one cell is selected. Try:
    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Count >1 then Exit Sub
    
        Dim Found As Variant, _
            ValAddress  As String
    
    'branch to Function FindValidationCells to get the address of the block of cells
    'with data validation starting at cell Cll
    
            ValAddress = FindValidationCells
        
        If Not Intersect(Target, Range(ValAddress)) Is Nothing Then
            Application.EnableEvents = False
            Set Found = Range("types").Find(Target.Value)
            
            If Not Found Is Nothing Then
                Found.Copy
                Target.PasteSpecial _
                    Paste:=xlPasteFormats
                Application.CutCopyMode = False
                Set Found = Nothing
            Else
                With Target.Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With    'target cell
            End If  'found
        End If  'in validated cells
        Application.EnableEvents = True
    End Sub

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Format Cell On Select

    @protonLeah
    I tried to send you private message but I encountered an error ("protonLeah has chosen not to receive private messages or may not be allowed to receive private messages. Therefore you may not send your message to him/her.")

    I don't get what red rep
    I remember I gave you rep points as I liked your code and the solution presented
    That's all
    I'm admired of your way of thinking in fact

  13. #13
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    MS 365
    Posts
    596

    Re: Format Cell On Select

    protonLeah,
    You just did it again!
    Thanks

+ 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. Replies: 4
    Last Post: 06-17-2013, 05:21 AM
  2. [SOLVED] Select a cell -> find cell value in workbook then format
    By bobbybill in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-09-2012, 10:09 AM
  3. Select a specific column till it identifies blank cell and to format
    By k1234y in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-28-2012, 12:35 AM
  4. Select all cell with the same format
    By JamieMorien in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-18-2008, 09:54 AM
  5. select adjustment tab in cell format dialog
    By x taol in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-22-2006, 12:35 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