+ Reply to Thread
Results 1 to 23 of 23

Change cell color w/o using conditional formula

Hybrid View

jimjaix Change cell color w/o using... 03-10-2009, 01:40 PM
Kenneth Hobson Re: Change cell color w/o... 03-10-2009, 01:51 PM
jimjaix Re: Change cell color w/o... 03-10-2009, 02:14 PM
JBeaucaire Re: Change cell color w/o... 03-10-2009, 02:18 PM
Cheeky Charlie Re: Change cell color w/o... 03-10-2009, 02:24 PM
jimjaix Re: Change cell color w/o... 03-10-2009, 02:39 PM
JBeaucaire Re: Change cell color w/o... 03-10-2009, 02:24 PM
Cheeky Charlie Re: Change cell color w/o... 03-10-2009, 02:33 PM
jimjaix Re: Change cell color w/o... 03-10-2009, 02:35 PM
Cheeky Charlie Re: Change cell color w/o... 03-10-2009, 02:39 PM
Cheeky Charlie Re: Change cell color w/o... 03-10-2009, 02:42 PM
Cheeky Charlie Re: Change cell color w/o... 03-10-2009, 02:45 PM
jimjaix Re: Change cell color w/o... 03-10-2009, 02:56 PM
Cheeky Charlie Re: Change cell color w/o... 03-10-2009, 03:03 PM
JBeaucaire Re: Change cell color w/o... 03-10-2009, 03:07 PM
jimjaix Re: Change cell color w/o... 03-10-2009, 03:10 PM
Cheeky Charlie Re: Change cell color w/o... 03-10-2009, 03:13 PM
JBeaucaire Re: Change cell color w/o... 03-10-2009, 03:15 PM
jimjaix Re: Change cell color w/o... 03-10-2009, 03:15 PM
Cheeky Charlie Re: Change cell color w/o... 03-11-2009, 05:20 AM
jimjaix Re: Change cell color w/o... 03-11-2009, 10:07 AM
Cheeky Charlie Re: Change cell color w/o... 03-11-2009, 10:31 AM
jimjaix Re: Change cell color w/o... 03-11-2009, 01:36 PM
  1. #1
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Change cell color w/o using conditional formula

    Hi, I want to create a function or vba code for a column IF any text in that column is = to Selfpay turn them all to Red text. I want to do this without using conditional formula. Thanks

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Change cell color w/o using conditional formula

    What does "them all" mean? All cells with that data value or the whole column?

  3. #3
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Re: Change cell color w/o using conditional formula

    I would like to have a range like K5:K70.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Change cell color w/o using conditional formula

    Adjust the range to the column and cell range you want "watched", then put this macro into the sheet module:
    Sub worksheet_calculate()
    Dim cell As Range, rng As Range
    Set rng = Range("K5:K70")
    
    For Each cell In rng
        If cell.Value = "Selfpay" Then
            With cell.Interior
                .ColorIndex = 3
                .Pattern = xlSolid
            End With
        Else
            cell.Interior.ColorIndex = xlNone
        End If
    Next cell
    End Sub
    Last edited by JBeaucaire; 03-10-2009 at 02:20 PM. Reason: Fixed color to change to RED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  5. #5
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    Sub FindSelfpays()
    
    Dim rRange As Range
    Dim rCell As Range
    Dim sFirstAdd As String
    
    Set rRange = Range("A5:M70") 'Change this to suit your needs
    
    On Error Resume Next
        Set rCell = rRange.Find("Selfpay")
        If Err.Number <> 0 Then Exit Sub
    On Error GoTo 0
    
    sFirstAdd = rCell.Address
    Do
        Range(Cells(5, rCell.Column), Cells(70, rCell.Column)).Font.ColorIndex = 3
        Set rCell = rRange.Find("Selfpay", rCell)
    Loop Until rCell.Address = sFirstAdd
    
    Set rCell = Nothing
    Set rRange = Nothing
    
    End Sub
    HTH

  6. #6
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Re: Change cell color w/o using conditional formula

    This code changed all the cloum in K to Red, not just the selfpay?



    Quote Originally Posted by Cheeky Charlie View Post
    Sub FindSelfpays()
    
    Dim rRange As Range
    Dim rCell As Range
    Dim sFirstAdd As String
    
    Set rRange = Range("A5:M70") 'Change this to suit your needs
    
    On Error Resume Next
        Set rCell = rRange.Find("Selfpay")
        If Err.Number <> 0 Then Exit Sub
    On Error GoTo 0
    
    sFirstAdd = rCell.Address
    Do
        Range(Cells(5, rCell.Column), Cells(70, rCell.Column)).Font.ColorIndex = 3
        Set rCell = rRange.Find("Selfpay", rCell)
    Loop Until rCell.Address = sFirstAdd
    
    Set rCell = Nothing
    Set rRange = Nothing
    
    End Sub
    HTH

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Change cell color w/o using conditional formula

    Or, if you only want it to happen when you first enter SELFPAY into the cells, and not check the cells all the time (meaning there are no formulas changing those values), then use this instead:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("K5:K70")) Is Nothing Then
                If Target.Value = "Selfpay" Then
                    With Target.Interior
                        .ColorIndex = 3
                        .Pattern = xlSolid
                    End With
                Else
                    Target.Interior.ColorIndex = xlNone
                End If
        End If
    End Sub
    This occurs in real time but only when you type in those cells.

    The previous macro I provided occurs in real time and will update those cell color even if formulas are possible changing their value.

  8. #8
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    If you want "live conditional formatting" (you should probably use conditional formatting - but let's ignore that for now) then you can use a worksheet_change event macro but remember this will remove the undo functionality for that sheet . If you do want this, I would recommend a macro which actively finds each instance of "selfpay" then formats the range, instead of a macro which searches every single cell of the range by default - macros which operate on _change should be very "light".

    CC

  9. #9
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Re: Change cell color w/o using conditional formula

    Hi the code works great, it changed the color when I run the macro. It there a way that this code will automatically run in the background? let's say I enter "selfpay" in column K it will automatically change the color without me running the macro? Thanks

  10. #10
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rRange As Range
    Dim rCell As Range
    Dim sFirstAdd As String
    
    Set rRange = Range("A5:M70") 'Change this to suit your needs
    If Intersect(Target, rrange) Is Nothing Then exit sub
    
    On Error Resume Next
        Set rCell = rRange.Find("Selfpay")
        If Err.Number <> 0 Then Exit Sub
    On Error GoTo 0
    
    sFirstAdd = rCell.Address
    Do
        Range(Cells(5, rCell.Column), Cells(70, rCell.Column)).Font.ColorIndex = 3 'also adjust the 5 and 70 here to change the top and bottom rows
        Set rCell = rRange.Find("Selfpay", rCell)
    Loop Until rCell.Address = sFirstAdd
    
    Set rCell = Nothing
    Set rRange = Nothing
    
    End Sub
    HTH
    Last edited by Cheeky Charlie; 03-10-2009 at 02:40 PM. Reason: hot to change font colour application area

  11. #11
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    IF any text in that column is = to Selfpay turn them all to Red text.
    Perhaps you could be more clear

    this certainly didn't help:
    I would like to have a range like K5:K70
    as it implies the whole column within a given table

  12. #12
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rRange As Range
    Dim rCell As Range
    Dim sFirstAdd As String
    
    Set rRange = Range("A5:M70") 'Change this to suit your needs
    If Intersect(Target, rRange) Is Nothing Then Exit Sub
    
    On Error Resume Next
        Set rCell = Intersect(Target, rRange).Find("Selfpay")
        If rcell is nothing Then Exit Sub
    On Error GoTo 0
    
    sFirstAdd = rCell.Address
    Do
        rCell.Font.ColorIndex = 3
        Set rCell = Intersect(Target, rRange).Find("Selfpay", rCell)
    Loop Until rCell.Address = sFirstAdd
    
    Set rCell = Nothing
    Set rRange = Nothing
    
    End Sub
    Done

    /thread
    Last edited by Cheeky Charlie; 03-10-2009 at 02:46 PM. Reason: wrong error check!

  13. #13
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Re: Change cell color w/o using conditional formula

    I am sorry if I did not explain clear enough, I want a vba code that will change the color of the text or the field to red if "Selfpay" is located under column K (let's just assume the range is K5:K70).

    JBeaucaire's Macro works great, but is there anyway I can make it to work without running the marco? (Column K is a list box, assume if I select Selfpay it will automatically change the color?)


    Cheeky Charlie, I tried your code but it doesn't do anything, maybe I am doing something wrong where should I put the code under? or module? I am a novice excel user...
    Update 1 (I put the code under the sheet and it works PERFECTLY) Thanks alot Cheeky
    UPdate 2 (If it was Selfpay and I select another option, it remains red) let's say if it was selfpay (red) and I change it to something else it should turn black.
    Last edited by jimjaix; 03-10-2009 at 02:59 PM.

  14. #14
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    My original, like JB's was based on a one-time run

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rRange As Range
    Dim rCell As Range
    Dim sFirstAdd As String
    
    Set rRange = Range("A5:M70") 'Change this to suit your needs
    If Intersect(Target, rRange) Is Nothing Then Exit Sub
    
    On Error Resume Next
        Set rCell = Intersect(Target, rRange).Find("Selfpay")
        If rcell is nothing Then
            intersect(target,rrange).font.colorindex = 1
            Exit Sub
        end if
    On Error GoTo 0
    
    sFirstAdd = rCell.Address
    Do
        rCell.Font.ColorIndex = 3
        Set rCell = Intersect(Target, rRange).Find("Selfpay", rCell)
    Loop Until rCell.Address = sFirstAdd
    
    Set rCell = Nothing
    Set rRange = Nothing
    
    End Sub
    This should now reset

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Change cell color w/o using conditional formula

    Mine is not a manual macro. You should not be running it manually. Right-click on the sheet-tab name where this is supposed to run and VIEW CODE...paste the macro in there.

    Mine were designed to operate on their own if you put them in the SHEET module.

  16. #16
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Re: Change cell color w/o using conditional formula

    Yup it does works now, thanks a lot Cheeky

    Quote Originally Posted by Cheeky Charlie View Post
    My original, like JB's was based on a one-time run

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rRange As Range
    Dim rCell As Range
    Dim sFirstAdd As String
    
    Set rRange = Range("A5:M70") 'Change this to suit your needs
    If Intersect(Target, rRange) Is Nothing Then Exit Sub
    
    On Error Resume Next
        Set rCell = Intersect(Target, rRange).Find("Selfpay")
        If rcell is nothing Then
            intersect(target,rrange).font.colorindex = 1
            Exit Sub
        end if
    On Error GoTo 0
    
    sFirstAdd = rCell.Address
    Do
        rCell.Font.ColorIndex = 3
        Set rCell = Intersect(Target, rRange).Find("Selfpay", rCell)
    Loop Until rCell.Address = sFirstAdd
    
    Set rCell = Nothing
    Set rRange = Nothing
    
    End Sub
    This should now reset
    Sorry JBeaucaire, I was putting the code under module that was why it didn't work, I tried putting on the Sheet code selection and it works like a charm. Thank you so much
    Quote Originally Posted by JBeaucaire View Post
    Mine is not a manual macro. You should not be running it manually. Right-click on the sheet-tab name where this is supposed to run and VIEW CODE...paste the macro in there.

    Mine were designed to operate on their own if you put them in the SHEET module.

  17. #17
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    oops - one was on calculate, t'other on change - why the difference?

  18. #18
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Change cell color w/o using conditional formula

    The Calculate version is in case there are formulas in column K making changes to the pay selection. If some other cell changes a value and THAT changes the column K value, it won't change the color unless it's a Worksheet_Calculate event.

    The WorkSheet_Change version only works when manual changes are being made IN column K. Significant difference.

  19. #19
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Re: Change cell color w/o using conditional formula

    JBeaucaire & Cheeky Charlie I have something that's similar to this, instead of looking for the word “Selfpay” I want to do a date comparison. I have a list of date in a column I; I want the data to be in red text if it’s greater than today(). How do I implement something like this?

  20. #20
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    Use conditional formatting...

  21. #21
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Re: Change cell color w/o using conditional formula

    Any ways to do it without using conditinoal formula?

  22. #22
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Re: Change cell color w/o using conditional formula

    Why don't you want to use conditional formatting?

  23. #23
    Registered User
    Join Date
    12-07-2007
    Posts
    20

    Re: Change cell color w/o using conditional formula

    just don't want to lol... I want to learn more about coding and stuff

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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