Hi
I have a range of cells which populate the words Gold, Silver, Bronze or Red on the back of a LOOKUP formula which all works fine, I now need a VB Code to be able to change the cell colour depending on which word has been returned.
I've attempted to adapt a previously used code however the cells are remaining white and I'm also getting a Runtime Error 13 which appears to be stopping other code within the sheet working as it should.
This is the code I've tried using, can anyone see any obvious errors?
Many thanksPrivate Sub Worksheet_Calculate() Dim icol As Long Dim c As Range For Each c In Range("af4:af18") Application.ScreenUpdating = False Select Case UCase(c.Value) Case "GOLD": icol = 6 Case "SILVER": icol = 15 Case "BRONZE": icol = 46 Case "RED": icol = 3 Case Else icol = 0 End Select With c .Interior.ColorIndex = icol .Font.ColorIndex = icol End With Next c Application.ScreenUpdating = True End Sub
Put your code in the worksheet Change module.
Private Sub Worksheet_Change(ByVal Target As Range) Dim icol As Long Dim c As Range For Each c In Range("af4:af18") Select Case UCase(c.Value) Case "GOLD": icol = 6 Case "SILVER": icol = 15 Case "BRONZE": icol = 46 Case "RED": icol = 3 Case Else icol = 0 End Select With c .Interior.ColorIndex = icol .Font.ColorIndex = icol End With Next c End Sub
Can't you just use Conditional Formatting in Range("af4:af18")?
Last edited by Marcol; 09-25-2010 at 05:50 AM.
If you need any more information, please feel free to ask.
However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
Also
If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.
Thanks for your reply
I'm getting the following error when I try using the above code
Compile error: Ambigous name detected: Worksheet_Changne
In relation to the conditional formatting question, I should have said I onlt have access to Office 97-2003 at work which only has the option for 3 rules as you'll know
Cheers
You've probably got two WorkSheet)Change event ptocedures, only one is allowed.
Also, which version of Excel are you using? Update your profile with this information
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
You must have some code in the change event already.
Right click the tab of the sheet in question then select > View Code
Put the code in the resultant VBa window.
If there is already something in the change event, then add your code to it.
TIP
Add the versions of Excel you are using to your profile, there are significant differences in them, and answers returned will be tailored to suit your version or earlier.
A location would also help in predicting any possible regional variations. language, or time zone problems.
If you need any more information, please feel free to ask.
However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
Also
If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.
Might be a daft question but at what stage would I add the code? If these were the two codes where would the second code blend with the first and do I need to remove any code for them both to work
Thanks for all your help
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 = 46 Case "AMBER": icol = 44 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 Private Sub Worksheet_Change(ByVal Target As Range) Dim icol As Long Dim c As Range For Each c In Range("af4:af18") Select Case UCase(c.Value) Case "GOLD": icol = 6 Case "SILVER": icol = 15 Case "BRONZE": icol = 46 Case "RED": icol = 3 Case Else icol = 0 End Select With c .Interior.ColorIndex = icol .Font.ColorIndex = icol End With Next c End Sub
To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.
If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.
Doing this will ensure you get the result you need!
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
Hi
I've attached a dummy workbook as requested
I'm almost there with this I think but I'm now getting a runtime error when I enter Gold, Silver etc in column E, the Scores in column AF are changing colour as I want although I also want to be able to see the text in column AF
I've added text into cells E4 to E8 which all works fine, this in turn calculates a percentage score in column AD which then populates a status of either Gold, Silver, Bronze or Red in column AF which is the range of cells that I want to apply the code to change colour depending on the score
Hope that all makes sense
Many thanks in advance for any help
Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Count > 1 Then Exit Sub If Not Intersect(Target, Range("E4:N18")) Is Nothing Then On Error Resume Next With Target.Interior .ColorIndex = xlNone .ColorIndex = Val(Split("gold6silver15bronze46red3cvp1", lcase(Target.Text))(1)) Target.Offset(, 27).Interior.ColorIndex = .ColorIndex End With End If End Sub
Hi snb
Thanks for the code, it works fine for the first part of what I'm trying to achieve however I also need the cells in column AF to change colour depending on the result of the LOOKUP formula
How would I build on your code to do that please?
Cheers
If Not Intersect(Target, Range("E4:N18", "Af4:af18")) Is Nothing Then
---
Ben Van Johnson
Hi
I've added the above code where I assume to be correct however the cells in AF4:AF18 remain unchanged.
Can you see what is stopping the cells in AF4:AF18 changing?
Many thanks
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", "Af4:af18")) Is Nothing Then Application.ScreenUpdating = False Select Case UCase(Target.Value) Case "SILVER": icol = 15 Case "BRONZE": icol = 45 Case "AMBER": icol = 44 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
Hello Twaddy,
This version of the macro works for me. Remove any Worksheet_Change() event code you have in the workbook. Follow the directions below to install the macro.
NOTE: When a formula calculates, it does not trigger the Worksheet_Change event.
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", "AF4:AF18")) Is Nothing Then Select Case UCase(Target.Value) Case "SILVER": icol = 15 Case "BRONZE": icol = 45 Case "AMBER": icol = 44 Case "RED": icol = 3 Case "GOLD": icol = 6 Case "CVP": icol = 1 Case Else: icol = 0 End Select Target.Interior.ColorIndex = icol End If End Sub
How to Save a Worksheet Event Macro
- Copy the macro using CTRL+C keys.
- Open your Workbook and Right Click on the Worksheet's Name Tab for the Worksheet the macro will run on.
- Left Click on View Code in the pop up menu.
- Paste the macro code using CTRL+V
- Make any custom changes to the macro if needed at this time.
- Save the macro in your Workbook using CTRL+S
Last edited by Leith Ross; 09-26-2010 at 07:36 PM.
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
[QUOTE=Leith Ross;2389268]Hello Twaddy,
NOTE: When a formula calculates, it does not trigger the Worksheet_Change event.
QUOTE]
Hi Leith
Thanks for your reply
So to clarify, it isn't possible to have a change event change the cell colour in e4:n18 based on what is typed into the cell AND that changes af4:af18 automatically depending on the outcome of the LOOKUP formula?
Many thanks
Twaddy
Hello Twaddy,
The Worksheet_Change event is fired when either the user types in the cell and hits return or a user makes a selection from a Validation drop down list.
However, when a cell's content is changed by a Formula, a cell link, a DDE conversation, a Forms drop down, or an ActiveX drop down then the event is NOT fired.
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks