+ Reply to Thread
Results 1 to 15 of 15

change cell color only if that cells font color is black and it's value is < 2000

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-25-2012
    Location
    U.K
    MS-Off Ver
    Excel 2016
    Posts
    101

    change cell color only if that cells font color is black and it's value is < 2000

    Hi'
    I am trying to use vba to change the color of a cell only if that cells font color is black and it's value is less than £2000.

    So far I have assigned the below code to a button and using a MsgBox instead of changing the cell color as a test but is does not work.

    Any ideas.

    Sub Button1_Click()
    If Range("Table1[Balance]").Font.ColorIndex = 1 And Range("Table1[Balance]").Value > 2000 Then
    MsgBox ("TEST")
    End If
    End Sub

  2. #2
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: change cell color only if that cells font color is black and it's value is < 2000

    Quote Originally Posted by randall78 View Post
    Hi'
    I am trying to use vba to change the color of a cell only if that cells font color is black and it's value is less than £2000.
    Sub Button1_Click()
    If Range("Table1[Balance]").Font.ColorIndex = 1 And Range("Table1[Balance]").Value > 2000 Then
    MsgBox ("TEST")
    End If
    End Sub
    Less or more?
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  3. #3
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: change cell color only if that cells font color is black and it's value is < 2000

    Sorry for double post.
    How can I delete this one?
    Last edited by KOKOSEK; 02-05-2019 at 03:20 PM.

  4. #4
    Forum Contributor
    Join Date
    06-25-2012
    Location
    U.K
    MS-Off Ver
    Excel 2016
    Posts
    101

    Re: change cell color only if that cells font color is black and it's value is < 2000

    change for values less then 2000 but only if font color is black.

    I should also point out that the calls in the Balance column of the table contain a formula to return the value. Don't know if that make a difference.
    Last edited by randall78; 02-05-2019 at 03:25 PM.

  5. #5
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: change cell color only if that cells font color is black and it's value is < 2000

    If your "Table1[Balance]" cell is in a named range in a list object then the syntax would be

    If ListObject.Range("Table1[Balance]").Font.ColorIndex = 1 And ListObject.Range("Table1[Balance]").Value < 2000 Then
    MsgBox ("TEST")
    Last edited by JLGWhiz; 02-05-2019 at 03:33 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  6. #6
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: change cell color only if that cells font color is black and it's value is < 2000

    If Range("Table1[Balance]").Font.ColorIndex = 1 And Range("Table1[Balance]").Value > 2000

    If that cells font color is black and it's value is less than £2000.

    Compare what you saying and what you've got in code.

  7. #7
    Forum Contributor
    Join Date
    06-25-2012
    Location
    U.K
    MS-Off Ver
    Excel 2016
    Posts
    101

    Re: change cell color only if that cells font color is black and it's value is < 2000

    The code i have didn't give me any error, it just didn't work. By adding ListObject before Range gives me an Object Required error.

  8. #8
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: change cell color only if that cells font color is black and it's value is < 2000

    Hi,

    Not sure I understand your syntax.

    Table1[Balance] refers to an entire column of values within the table, not just a single cell.

    Are you looping through that column? If so, can we see the rest of your code?

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  9. #9
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: change cell color only if that cells font color is black and it's value is < 2000

    Put in "Table1[Balance]" value 5 000 (in black font), run it and tell what you got.

  10. #10
    Forum Contributor
    Join Date
    06-25-2012
    Location
    U.K
    MS-Off Ver
    Excel 2016
    Posts
    101

    Re: change cell color only if that cells font color is black and it's value is < 2000

    Here is what i have. The code is not giving me an error, but the MsgBox is shown even if none of the values are less then 2000 and if i change the vbBlack to Vb red then the MsgBox does not show so that bit is working its the < 2000 bit that's not.

    Regards
    Attached Files Attached Files

  11. #11
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: change cell color only if that cells font color is black and it's value is < 2000

    try (just hint):

    Sub Button1_Click()
    Dim i As Integer
    For i = 5 To 33
        If (Range("E" & i).Font.Color = vbBlack And Range("E" & i).Value < 2000) Then
            MsgBox ("In cell E" & i & " value " & Range("E" & i).Value & " is black and less than 2000")
        End If
    Next i
    End Sub

  12. #12
    Forum Contributor
    Join Date
    06-25-2012
    Location
    U.K
    MS-Off Ver
    Excel 2016
    Posts
    101

    Re: change cell color only if that cells font color is black and it's value is < 2000

    Thanks for the help by the way.

    Now the < bit is working but the not the font color bit.

    Also, my original plan is to have the cell color change to red instead if having a MsgBox.

  13. #13
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,765

    Re: change cell color only if that cells font color is black and it's value is < 2000

        If (Range("E" & i).Font.Color = vbBlack And Range("E" & i).Value < 2000) Then
            Range("E" & i).Font.Color = vbRed
        End If

  14. #14
    Forum Contributor
    Join Date
    06-25-2012
    Location
    U.K
    MS-Off Ver
    Excel 2016
    Posts
    101

    Re: change cell color only if that cells font color is black and it's value is < 2000

    Thanks but i have just realized why its not working. I have used Conditional formatting to change duplicate values to white. That's why vbBlack bit doesn't work. Know of any way round this.

  15. #15
    Forum Contributor
    Join Date
    06-25-2012
    Location
    U.K
    MS-Off Ver
    Excel 2016
    Posts
    101

    Re: change cell color only if that cells font color is black and it's value is < 2000

    If you look at my other post (Adding up an accumulative total based on the week number in a table.) The file explains in more detail exactly what i'm trying to achieve. Any help would be great.

+ 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 to change strike through to Red without changing the font color (Black)
    By sjs4952 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-22-2018, 01:00 PM
  2. how to automatically activate a macro when the font color in a cell changes to black
    By ainon ithnain in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-17-2014, 03:52 AM
  3. Mouse rolls over to cell will change the tab color, font color and tp bold type
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-20-2013, 10:36 AM
  4. Replies: 2
    Last Post: 02-04-2013, 02:00 PM
  5. Change font color based on font color of another cell
    By Ibyers in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2012, 09:36 AM
  6. Change the font color to black in the entire workbook
    By fatalcore in forum Excel General
    Replies: 2
    Last Post: 03-31-2011, 02:30 AM
  7. Fill Color and Font Color stay Black and White
    By Hors Categorie in forum Excel General
    Replies: 1
    Last Post: 05-10-2005, 07:20 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