+ Reply to Thread
Results 1 to 13 of 13

Macro for coloring cells based on contents

Hybrid View

  1. #1
    Registered User
    Join Date
    12-31-2017
    Location
    Moscow
    MS-Off Ver
    2013
    Posts
    8

    Macro for coloring cells based on contents

    I have the following macro

    Formula: copy to clipboard
    Sub ModelColoring()

    Dim cell As Range, constantCell As Range, formulaCells As Range
    Dim cellFormula As String

    With Selection
    On Error Resume Next
    Set constantCell = .SpecialCells(xlCellTypeConstants, xlNumbers)
    Set formulaCells = .SpecialCells(xlCellTypeFormulas, 21)
    On Error GoTo 0
    End With


    If Not constantCell Is Nothing Then
    constantCell.Font.Color = vbBlue
    End If

    If Not formulaCells Is Nothing Then
    For Each cell In formulaCells
    cellFormula = cell.Formula

    If cellFormula Like "*.xls*]*!*" Then
    cell.Font.Color = RGB(0, 176, 80)
    ElseIf cellFormula Like "*!*" _
    And Not cellFormula Like "*~**" _
    And Not cellFormula Like "*+*" _
    And Not cellFormula Like "*-*" _
    And Not cellFormula Like "*/*" _
    And Not cellFormula Like "*^*" _
    And Not cellFormula Like "*%*" _
    And Not cellFormula Like "*>*" _
    And Not cellFormula Like "*<*" _
    And Not cellFormula Like "*=<*" _
    And Not cellFormula Like "*=>*" _
    And Not cellFormula Like "*<>*" _
    And Not cellFormula Like "*&*" Then
    cell.Font.Color = vbRed
    Else
    cell.Font.Color = vbBlack
    End If
    Next cell
    End If

    End Sub

    I have problems with this line

    Formula: copy to clipboard
    And Not cellFormula Like "*~**"



    Basically I want all the cells with constants have one font color, those that perform a calculation to have another and those that derive their value from another sheet a third. However, the above mentioned line does not seem to recognize the multiplication symbol * for some reason and colors the cells that use values from other sheets and multiply them as if they are simply derived from another sheet (i.e. vbRed instead of black).

    I am still quite new to VBA so if anyone could help I would appreciate it.
    Thx in advance
    Attached Files Attached Files
    Last edited by serge_kashlik; 12-31-2017 at 07:53 PM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro for coloring cells based on contents

    Not tested
           If cellFormula Like "*.xls*]*!*" Then
    
                cell.Font.Color = RGB(0, 176, 80)
    
            ElseIf cellFormula Like "*!*" And Not cellFormula Like "*~[*]*" And Not cellFormula Like "*[+/^%><&-]*" _
                    And Not cellFormula Like "*=<*" And Not cellFormula Like "*=>*" And Not cellFormula Like "*<>*" Then
    
                cell.Font.Color = vbRed
    
            Else
    
            cell.Font.Color = vbBlack
    
            End If

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,526

    Re: Macro for coloring cells based on contents

    Attaching a sample workbook enables others to work on your problem:

    To attach a sample workbook. Make sure there is just enough data to make it clear what is needed. Include BEFORE/AFTER sheets if needed to show the process you're trying to complete or automate. Remember to desensitize the data.

    Click on GO ADVANCED and click "manage attachments" to open the upload window.


    To add a file to a post

  4. #4
    Registered User
    Join Date
    12-31-2017
    Location
    Moscow
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for coloring cells based on contents

    Thank for the tip
    All done

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro for coloring cells based on contents

    If that takes care of your original question, select Thread Tools from the menu link above and mark this thread as SOLVED.

  6. #6
    Registered User
    Join Date
    12-31-2017
    Location
    Moscow
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for coloring cells based on contents

    Did not solve it unfortunately

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro for coloring cells based on contents

    Then you will need to upload a workbook with before/after.

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  8. #8
    Registered User
    Join Date
    12-31-2017
    Location
    Moscow
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for coloring cells based on contents

    I edited the original thread and added the file

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro for coloring cells based on contents

    And what do you expect to match with "*~**" ?

    Do you mean escape asterisk like "*[*]*" ?

  10. #10
    Registered User
    Join Date
    12-31-2017
    Location
    Moscow
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for coloring cells based on contents

    I want any cell that contains a formula which uses the multiplication sign to have the font color black. The problem now is that the macro does not recognize the * as a separate symbol and I thought that usually in order to distinguish a wildcard character you need to use the "~".
    I tried several ways including[*], /* and ~* but none of them seem to work

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro for coloring cells based on contents

    Your problem cell are now red.
    What color should they be?

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Macro for coloring cells based on contents

    It should work
    MsgBox "=1*2" Like "*[*]*"
    MsgBox "=12" Like "*[*]*"
    If you haven't got what you want, it would be the way you use it.

    And do not recommend to use same procedure name of Module name.

    See the attached
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    12-31-2017
    Location
    Moscow
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for coloring cells based on contents

    Thx for the help, it works as I need it to

+ 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: 22
    Last Post: 03-16-2017, 09:42 PM
  2. [SOLVED] I need a macro to clear cells based on contents
    By emymeeky in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-28-2013, 08:54 AM
  3. [SOLVED] Combining Contents of two cells whilst preserving text coloring
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-11-2013, 12:20 AM
  4. [SOLVED] Coloring cells based on value in another cell
    By killerthun in forum Excel General
    Replies: 2
    Last Post: 08-16-2012, 08:21 AM
  5. Coloring Cells dependent upon their contents
    By omgeokid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-16-2011, 02:33 PM
  6. Macro to hide columns based on contents of two cells
    By Korae13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-18-2006, 12:40 AM
  7. Coloring Cell Font based on Cell Contents
    By willjohnson33@yahoo.com in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-22-2005, 08:06 PM

Tags for this Thread

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