Code to run Based on Cell value

KNKN9

Board Regular
Joined
Mar 27, 2017
Messages
92
Hi,

I have the code
Code:
Sub Iteration_Status()Dim response As Variant
'input box that can accept both text and numbers
response = Application.InputBox("Enter Iteration Status Number", "Numbers Only", , , , , , 1 + 2)
If response = "False" Then
'code distinguishes between "False" on clicking the Cancel button as compared to "False" entered in the input box as a string value which returns 0
MsgBox "Cancel has been pressed"
ElseIf response = "" Then
MsgBox "A zero-length string ("") has been entered"
Else
'reurns "0" (zero) both if either "0" is entered in input box or "False" is entered in input box
MsgBox response, vbOKCancel
End If
ActiveCell.Value = "IT" & response
End Sub


Sub Rework_Status()
Dim response As Variant
'input box that can accept both text and numbers
response = Application.InputBox("Enter Rework Status Number", "Numbers Only", , , , , , 1 + 2)
If response = "False" Then
'code distinguishes between "False" on clicking the Cancel button as compared to "False" entered in the input box as a string value which returns 0
MsgBox "Cancel has been pressed"
ElseIf response = "" Then
MsgBox "A zero-length string ("") has been entered"
Else
'reurns "0" (zero) both if either "0" is entered in input box or "False" is entered in input box
MsgBox response, vbOKCancel
End If
ActiveCell.Value = "REW" & response
End Sub
Sub Rebrief_Status()
Dim response As Variant
'input box that can accept both text and numbers
response = Application.InputBox("Enter Rebrief Status Number", "Numbers Only", , , , , , 1 + 2)
If response = "False" Then
'code distinguishes between "False" on clicking the Cancel button as compared to "False" entered in the input box as a string value which returns 0
MsgBox "Cancel has been pressed"
ElseIf response = "" Then
MsgBox "A zero-length string ("") has been entered"
Else
'reurns "0" (zero) both if either "0" is entered in input box or "False" is entered in input box
MsgBox response, vbOKCancel
End If
ActiveCell.Value = "REB" & response
End Sub
inserted into module and in the sheet itself I have the code
Code:
Private Sub Worksheet_Change(ByVal Target As Range)If Not Intersect(Target, Range("F7")) Is Nothing Then
    Select Case Range("F7")
        Case "Iteration Status": Iteration_Status
        Case "Rework Status":   Rework_Status
        Case "Rebrief Status": Rebrief_Status
        End Select
End If
End Sub
.

However, my code only works for case1 when the cell has "iteration status" and not for the other values. But I can't see why I it not running for the other 2 cases . Thanks in advanced for your help.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
See if this works:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("F7")) Is Nothing Then
    Select Case LCase(Trim(Range("F7").Value))
        Case "iteration status": Iteration_Status
        Case "rework status":   Rework_Status
        Case "rebrief status": Rebrief_Status
    End Select
End If

End Sub
 
Upvote 0
Perfect This works :)


See if this works:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("F7")) Is Nothing Then
    Select Case LCase(Trim(Range("F7").Value))
        Case "iteration status": Iteration_Status
        Case "rework status":   Rework_Status
        Case "rebrief status": Rebrief_Status
    End Select
End If

End Sub
 
Upvote 0
Sorry the code did seem to work at the end. I now have this problem.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)Dim rng As Range
Set rng = Range("G6:AB100")
If Not Application.Intersect(rng, Range(Target.Address)) Is Nothing Then
Select Case LCase(Trim(rng).Value)
Case "iteration status": Iteration_Status
Case "rework status ": Rework_Status
Case "rebrief status ": Rebrief_Status
End Select
End If


End Sub

and i receive error type mismatch for Select Case LCase(Trim(rng).Value).
Can you please help
 
Upvote 0
yes correct.. I have a dropdown list in more than one column. So my aim is whenever I select the droplist value in any column within the sheet I want the corresponding code to run. Hope that makes sense
 
Upvote 0
Ok lets try like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim rng As Range

Set rng = Intersect(Range("G6:AB100"), Target)
If Not rng Is Nothing Then
    If rng.Cells.Count > 1 Then Exit Sub
    Select Case LCase(Trim(Target.Value))
        Case "iteration status": Iteration_Status
        Case "rework status": Rework_Status
        Case "rebrief status": Rebrief_Status
    End Select
End If

End Sub
 
Last edited:
Upvote 0
Perfect... Thank you very much. I was really struggling to get this to work for multiple columns. Your help is much appreciated !!!
 
Upvote 0
Bear in mind if you change more than one cell at once this doesnt run. I presume you wouldnt be doing that with dropdowns. If you may then you need to loop through the changed cells.
 
Upvote 0

Forum statistics

Threads
1,223,994
Messages
6,175,849
Members
452,675
Latest member
duongtruc1610

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top