+ Reply to Thread
Results 1 to 10 of 10

If ActiveCell

Hybrid View

vaw30080 If ActiveCell 02-03-2015, 11:21 AM
skywriter Re: If ActiveCell 02-03-2015, 11:43 AM
stnkynts Re: If ActiveCell 02-03-2015, 11:58 AM
JOHN H. DAVIS Re: If ActiveCell 02-03-2015, 12:06 PM
sktneer Re: If ActiveCell 02-03-2015, 12:14 PM
skywriter Re: If ActiveCell 02-03-2015, 12:20 PM
JOHN H. DAVIS Re: If ActiveCell 02-03-2015, 12:27 PM
vaw30080 Re: If ActiveCell 02-03-2015, 12:54 PM
stnkynts Re: If ActiveCell 02-03-2015, 01:04 PM
vaw30080 Re: If ActiveCell 02-03-2015, 01:24 PM
  1. #1
    Registered User
    Join Date
    02-03-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    3

    If ActiveCell

    I'm trying to create a macro which will change the fill color of a cell based on text displayed in a cell. If the text displayed is Red, I'd like the cell fill color to be red, if text is yellow change the cell fill color to yellow, if green change the color to green. Below is what I've written. The code currently is changing the cell fill color to red no matter the value. Can someone assist?

    Sub SetActive()

    If ActiveCell = "Red" Then
    ActiveCell.Interior.Color = 255 ' Color cell interior red
    ElseIf ActiveCell = "Yellow" Then
    ActiveCell.Interior.Color = 49407 ' Color cell interior orange
    ElseIf ActiveCell = "Green" Then
    ActiveCell.Interior.Color = 65280 ' Color cell interior green
    End If

    End Sub

  2. #2
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: If ActiveCell

    It's case sensitive. Type green it won't work, but type Green it works. In these cases it worked for me.

    Try this:
    Sub SetActive()
    
    If LCase(ActiveCell.Value) = "red" Then
    ActiveCell.Interior.Color = 255 ' Color cell interior red
    ElseIf LCase(ActiveCell.Value) = "yellow" Then
    ActiveCell.Interior.Color = 49407 ' Color cell interior orange
    ElseIf LCase(ActiveCell.Value) = "green" Then
    ActiveCell.Interior.Color = 65280 ' Color cell interior green
    End If
    
    End Sub
    Last edited by skywriter; 02-03-2015 at 11:48 AM.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: If ActiveCell

    Are you using Conditional Formatting to change the color in the cell?

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If ActiveCell

    Maybe:

    Sub vaw30080()
    Select Case ActiveCell.Value
        Case Is = "Red"
            ActiveCell.Interior.ColorIndex = 3
        Case Is = "Yellow"
            ActiveCell.Interior.ColorIndex = 6
        Case Is = "Green"
            ActiveCell.Interior.ColorIndex = 4
    End Select
    End Sub

  5. #5
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,655

    Re: If ActiveCell

    Or you may try something like this... (similar to one suggested by JOHN)
    Sub SetActive()
    Select Case ActiveCell.Value
        Case Is = "Red"
            ActiveCell.Interior.Color = vbRed
        Case Is = "Yellow"
            ActiveCell.Interior.Color = vbYellow
        Case Is = "Green"
            ActiveCell.Interior.Color = vbGreen
    End Select
    End Sub
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  6. #6
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: If ActiveCell

    vaw30080 beware of case sensitivity. If your code contains = "Red" and you type red or RED in a cell your code will not work.

    Using Select Case LCase(ActiveCell.Value) and then typing your code as "red" or "green" will change this.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If ActiveCell

    Modified taking into account skywriter's comments.

    Sub vaw30080()
    Select Case ActiveCell.Value
        Case Is = "Red", "red"
            ActiveCell.Interior.ColorIndex = 3
        Case Is = "Yellow", "yellow"
            ActiveCell.Interior.ColorIndex = 6
        Case Is = "Green", "green"
            ActiveCell.Interior.ColorIndex = 4
    End Select
    End Sub
    Also as mention by stnkynts if you are using conditional formatting for the cells, please let us know the formulas so that adjustments can be made.

  8. #8
    Registered User
    Join Date
    02-03-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    3

    Re: If ActiveCell

    Okay. Thanks for the many replies. The 'Case Is' and the 'LCase' is working, however, I have to manually run the code to enact changes when the value in the cell is changed. How do I get vba to automatically run when a value is input or the existing value is changed in the active cell?

  9. #9
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: If ActiveCell

    Place it within the appropriate sheet module

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Selection.Count > 1 Then Exit Sub
    
    Select Case UCase(Target)
        Case Is = "RED"
            Target.Interior.ColorIndex = 3
        Case Is = "YELLOW"
            Target.Interior.ColorIndex = 6
        Case Is = "GREEN"
            Target.Interior.ColorIndex = 4
    End Select
    
    End Sub

  10. #10
    Registered User
    Join Date
    02-03-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    3

    Re: If ActiveCell

    Stnkynts, That's it. Works like a charm. 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. Copying an activecell range and pasting to another activecell range
    By Londonbound in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-12-2014, 07:41 AM
  2. [SOLVED] copy rows with activecell value and insert rows after activecell
    By a.hudrea in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-21-2014, 09:05 AM
  3. Selecting Activecell and cell next to the Activecell
    By Raghukumarn in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-31-2013, 06:32 AM
  4. Output contents of Activecell when Activecell may be string or numeric.
    By jfriddle in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-09-2012, 12:27 PM
  5. How to set the Activecell of "Sheet2" to Activecell of "Sheet1"
    By shawnh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-06-2010, 11:12 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