+ Reply to Thread
Results 1 to 13 of 13

VB Code to change cell colour by entering a word

  1. #1
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    VB Code to change cell colour by entering a word

    Hi there,

    VBA Noob was kind enough to help me out with a VB code some time back which worked perfectly at the time for it's intended purpose which I've now tried to adapt but I'm having difficulty with it and wondered if somebody would mind taking a look to see what I may be doing wrong.

    The original code used just single letters to change the cell colour however I now need the cell colour to change depending on what word is typed into the cell

    This is the code I'm trying to use but it ain't working, it works if I just use the capital letter of each word but not the whole word as I need.

    HTML Code: 
    If it's really simple then I apologise however I have no working knowledge of how to write code, as may be apparent.

    Any help would be much appreciated

    Cheers

    Twaddy

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VB Code to change cell colour by entering a word

    Hi,

    It works OK for me. Are you sure you're entering the word somewhere in the range E4:N18 and that you have macros enabled?

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: VB Code to change cell colour by entering a word

    What happens? What doesn't work? Which cell(s) are you modifying?

    I've just tested each colour in column E and it seems fine.

    Regards

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: VB Code to change cell colour by entering a word

    Best of both worlds:

    Please Login or Register  to view this content.


    Regards

  5. #5
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: VB Code to change cell colour by entering a word

    Hello all

    Thanks for your replies

    Frustratingly the VB works at home on Office 2010 however in work on 97-2003 version it doesn't work.

    Macro's are enabled and I've added a test macro at work and that works fine

    If I enter any of the specified words anywhere in the range E4:N18, such as Gold, the cell contains the text but remians colourless.

    I'm stumped!

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: VB Code to change cell colour by entering a word

    Are you enabling macros in 2010?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: VB Code to change cell colour by entering a word

    I had a similar problem and the following worked for me.
    Open file in Excel 2010 and save it as "Excel 97-2003" workbook (*.xls).
    Then open this .xls file in older version of excel.

    hth
    Ajay

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: VB Code to change cell colour by entering a word

    Works for me in 2003 and 2007.

    I did add the code to a 2007 workbook where macros were disabled (although I hadn't realised) and I got the effect you describe ... that is, input the text but don't get the colour.

    I only realised when I tried to enable events, thinking that might be the problem, only to be told that macros were disabled.

    Regards

    Regards

  9. #9
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: VB Code to change cell colour by entering a word

    Yeah Macro's are enabled in 2010 at home and works fine

    When I saved it at home I saved as 97-2003 file before emailing to work so that should be fine

    Macros are definitely enabled in work on 97-2003 too so I'm flumoxed!

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: VB Code to change cell colour by entering a word

    try
    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.



  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: VB Code to change cell colour by entering a word

    I'd be inclined to set up a new Excel 2003 workbook and just copy and paste the code into it.

    In other words, start from scratch. If that works, it's something to do with the transition between the two formats.

    Regards

  12. #12
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: VB Code to change cell colour by entering a word

    Hi

    With the help of a colleague in work we've got it sorted

    This is the code we used which has now worked, the only difference I think being that we've taken out the and at the start and end of the code as per my original code, not sure why they were there, what they do or even whether they were the problem but it's now working

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icol As Long
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("E4:N18")) Is Nothing Then

    Application.ScreenUpdating = False

    Select Case UCase(Target.Value)
    Case "SILVER": icol = 15
    Case "BRONZE": icol = 53
    Case "AMBER": icol = 45
    Case "RED": icol = 3
    Case "GOLD": icol = 6
    Case "CVP": icol = 1
    Case Else
    icol = 0
    End Select
    With Target
    .Interior.ColorIndex = icol
    End With
    End If

    Application.ScreenUpdating = True

    End Sub

    Thanks very much to everyone for your help

    Twaddy

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: VB Code to change cell colour by entering a word

    For some reason, you had the italics start and end tags ... as you can see in your last update.

    I'm surprised it compiled with those in ... I had to delete them when I copied your code.

    I had assumed that they were just put there for presentation in your original post.

    Anyway, result. Thanks for letting us know.

    Regards

+ 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