Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default need code to run when user changes value in cell

Adjust the "A" and "B" etc. to suit.

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("B2"))
'Set vRngInput = Intersect(Target, Range("B2:B10")) for larger range of cells
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = "A": Num = 10 'green
Case Is = "B": Num = 1 'black
Case Is = "C": Num = 5 'blue
Case Is = "D": Num = 7 'magenta
Case Is = "E": Num = 46 'orange
Case Is = "F": Num = 3 'red
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub

This is sheet event code. Right-click on the sheet tab and "View Code"

Copy/paste the above into that module.


Gord Dibben MS Excel MVP

On Tue, 16 Jan 2007 13:28:01 -0800, Grd wrote:

Hi everyone,

I need to change the color of a cell based on the value the user types in
the cell.
In this cell B2. If they type ON then it should be blue, BC red, AL yellow,
NS green and PEI pink. I can't use conditional formatting from the menu
because its only got three conditions I could be using many more.

Also I don't know how code can run automatically when a person changes the
value of a cell in a sheet.

Could anyone help

Thanks

Suzanne


  #2   Report Post  
Posted to microsoft.public.excel.programming
Go Go is offline
external usenet poster
 
Posts: 6
Default need code to run when user changes value in cell

Thanks Gord,

Copied and pasted and works 'out of the box'. Just changed the "A" and "B"
bits to what I need.

Great work

S

"Gord Dibben" wrote:

Adjust the "A" and "B" etc. to suit.

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("B2"))
'Set vRngInput = Intersect(Target, Range("B2:B10")) for larger range of cells
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = "A": Num = 10 'green
Case Is = "B": Num = 1 'black
Case Is = "C": Num = 5 'blue
Case Is = "D": Num = 7 'magenta
Case Is = "E": Num = 46 'orange
Case Is = "F": Num = 3 'red
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub

This is sheet event code. Right-click on the sheet tab and "View Code"

Copy/paste the above into that module.


Gord Dibben MS Excel MVP

On Tue, 16 Jan 2007 13:28:01 -0800, Grd wrote:

Hi everyone,

I need to change the color of a cell based on the value the user types in
the cell.
In this cell B2. If they type ON then it should be blue, BC red, AL yellow,
NS green and PEI pink. I can't use conditional formatting from the menu
because its only got three conditions I could be using many more.

Also I don't know how code can run automatically when a person changes the
value of a cell in a sheet.

Could anyone help

Thanks

Suzanne



  #3   Report Post  
Posted to microsoft.public.excel.programming
Bob Bob is offline
external usenet poster
 
Posts: 972
Default need code to run when user changes value in cell

Gord,
I have a similar situation, except that I have a column of cells (the range
can vary) where a user can input a code (0-20), and based on the code
inputted, I need to change both the font color and cell color.
Given that I am a VBA novice, can you kindly tell me how to modify your code
to accommodate my particular situation?
Thanks,
Bob


"Gord Dibben" wrote:

Adjust the "A" and "B" etc. to suit.

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("B2"))
'Set vRngInput = Intersect(Target, Range("B2:B10")) for larger range of cells
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = "A": Num = 10 'green
Case Is = "B": Num = 1 'black
Case Is = "C": Num = 5 'blue
Case Is = "D": Num = 7 'magenta
Case Is = "E": Num = 46 'orange
Case Is = "F": Num = 3 'red
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub

This is sheet event code. Right-click on the sheet tab and "View Code"

Copy/paste the above into that module.


Gord Dibben MS Excel MVP

On Tue, 16 Jan 2007 13:28:01 -0800, Grd wrote:

Hi everyone,

I need to change the color of a cell based on the value the user types in
the cell.
In this cell B2. If they type ON then it should be blue, BC red, AL yellow,
NS green and PEI pink. I can't use conditional formatting from the menu
because its only got three conditions I could be using many more.

Also I don't know how code can run automatically when a person changes the
value of a cell in a sheet.

Could anyone help

Thanks

Suzanne



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default need code to run when user changes value in cell

Bob,

The colorindex numbers below are made up - you will need to modify them, as well as add another 15
cases...

HTH,
Bernie
MS Excel MVP

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num1 As Integer
Dim Num2 As Integer
Dim rng As Range
Dim vRngInput As Variant

'Optional - if you want to limit to one cell at a time
' If Target.Cells.Count 1 Then Exit Sub

Set vRngInput = Intersect(Target, Range("B2:B1000"))
If vRngInput Is Nothing Then Exit Sub

On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = 1:
Num1 = 10
Num2 = 12
Case Is = 2:
Num1 = 3
Num2 = 12
Case Is = 3:
Num1 = 4
Num2 = 3
Case Is = 4:
Num1 = 6
Num2 = 7
Case Is = 5:
Num1 = 8
Num2 = 9
End Select
'Apply the color
rng.Interior.ColorIndex = Num1
rng.Font.ColorIndex = Num2

Next rng
endit:
Application.EnableEvents = True
End Sub


"Bob" wrote in message
...
Gord,
I have a similar situation, except that I have a column of cells (the range
can vary) where a user can input a code (0-20), and based on the code
inputted, I need to change both the font color and cell color.
Given that I am a VBA novice, can you kindly tell me how to modify your code
to accommodate my particular situation?
Thanks,
Bob


"Gord Dibben" wrote:

Adjust the "A" and "B" etc. to suit.

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Num As Long
Dim rng As Range
Dim vRngInput As Variant
Set vRngInput = Intersect(Target, Range("B2"))
'Set vRngInput = Intersect(Target, Range("B2:B10")) for larger range of cells
If vRngInput Is Nothing Then Exit Sub
On Error GoTo endit
Application.EnableEvents = False
For Each rng In vRngInput
'Determine the color
Select Case rng.Value
Case Is = "A": Num = 10 'green
Case Is = "B": Num = 1 'black
Case Is = "C": Num = 5 'blue
Case Is = "D": Num = 7 'magenta
Case Is = "E": Num = 46 'orange
Case Is = "F": Num = 3 'red
End Select
'Apply the color
rng.Interior.ColorIndex = Num
Next rng
endit:
Application.EnableEvents = True
End Sub

This is sheet event code. Right-click on the sheet tab and "View Code"

Copy/paste the above into that module.


Gord Dibben MS Excel MVP

On Tue, 16 Jan 2007 13:28:01 -0800, Grd wrote:

Hi everyone,

I need to change the color of a cell based on the value the user types in
the cell.
In this cell B2. If they type ON then it should be blue, BC red, AL yellow,
NS green and PEI pink. I can't use conditional formatting from the menu
because its only got three conditions I could be using many more.

Also I don't know how code can run automatically when a person changes the
value of a cell in a sheet.

Could anyone help

Thanks

Suzanne





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Code to have User input data for cell Bean Counter[_2_] Excel Discussion (Misc queries) 0 May 27th 10 09:56 PM
making NT user/login user appear in a cell [email protected] Excel Worksheet Functions 1 March 3rd 09 03:41 AM
need code to run when user changes value in cell Don Guillett Excel Programming 0 January 16th 07 09:46 PM
How do I format a cell, so the user must use a user calendar? nathan Excel Discussion (Misc queries) 0 January 16th 06 05:26 PM


All times are GMT +1. The time now is 06:44 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"