Macro to run only on select text

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
I am trying to run a macro but only want it to execute if the text matches.

here is what I am currently using:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)


    If Target.Address(False, False) = "B2" Then
        If Len(Target.Value) >= 2 And Len(Target.Value) <= 2 Then
            MsgBox "Please proceed to fill out the order for " & Target.Value & "." & vbNewLine & "Have a nice day."
            Rows("4:58").Hidden = False
        Else
            MsgBox "Type in FG  for Forest Grove, or Tu for Tualatin or Gr for Gresham. Nothing Else", vbExclamation
            Rows("4:58").Hidden = True
        End If
    End If
End Sub
What I would like is to have is if somebody types in 'FG' then a message box appears if 'Tu' or 'Gr' are typed in the same thing would happen, then the appropriate cells would hide/unhide.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Do you mean something like this?
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
    If Target.Address = "$B$2" Then
        If (Target = "FG") Or (Target = "Tu") Or (Target = "Gr") Then
            MsgBox "Please proceed to fill out the order for " & Target & "." & vbNewLine & "Have a nice day."
            Rows("4:58").Hidden = False
        Else
            MsgBox "Type in FG  for Forest Grove, or Tu for Tualatin or Gr for Gresham. Nothing Else", vbExclamation
            Rows("4:58").Hidden = True
        End If
    End If
    
End Sub
 
Upvote 0
Pretty close, but I was trying to see if it was possible to have a different message appear depending on what was typed in. then the rows would unhide regardless of the text.
 
Upvote 0
Pretty close, but I was trying to see if it was possible to have a different message appear depending on what was typed in.
Sure its possible. Something like this (chanegs your message boxes to suit).
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Address = "$B$2" Then
        Select Case Target.Text
            Rows("4:58").Hidden = False
            Case "FG"
                MsgBox "Say this if FG"
            Case "Tu"
                MsgBox "Say this if Tu"
            Case "Gr"
                MsgBox "Say this if Gr"
            Case Else
                MsgBox "Type in FG  for Forest Grove, or Tu for Tualatin or Gr for Gresham. Nothing Else", vbExclamation
                Rows("4:58").Hidden = True
        End Select
    End If
    
End Sub
 
Upvote 0
I seem to get an error whenever I type into B2:

Compile Error:
Statements and labels invalid between select Case and first Case
 
Upvote 0
OK, let's just move that hidden rows line up a few, and it should work fine:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
    If Target.Address = "$B$2" Then
        Rows("4:58").Hidden = False
        Select Case Target.Text
            Case "FG"
                MsgBox "Say this if FG"
            Case "Tu"
                MsgBox "Say this if Tu"
            Case "Gr"
                MsgBox "Say this if Gr"
            Case Else
                MsgBox "Type in FG  for Forest Grove, or Tu for Tualatin or Gr for Gresham. Nothing Else", vbExclamation
                Rows("4:58").Hidden = True
        End Select
    End If
    
End Sub
 
Upvote 0
It does work, however when any incorrect text is entered, the cells all reappear and the message pops up and then they hide again. Any way to solve this?
 
Upvote 0
OK, the message box is interrupting the procedure halfway through. So let's use variable and capture whether or not we should hide the rows and capture the message box statement, and wait until the very end to apply them, i.e.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
    Dim myHide As Boolean
    Dim myMsgBox As String
 
    If Target.Address = "$B$2" Then
        myHide = False
        Select Case Target.Text
            Case "FG"
                myMsgBox = "Say this if FG"
            Case "Tu"
                myMsgBox = "Say this if Tu"
            Case "Gr"
                myMsgBox = "Say this if Gr"
            Case Else
                myMsgBox = "Type in FG  for Forest Grove, or Tu for Tualatin or Gr for Gresham. Nothing Else"
                myHide = True
        End Select
        Rows("4:58").Hidden = myHide
        MsgBox myMsgBox
    End If
    
End Sub
 
Upvote 0
Note the last two lines (besides the "End" statements):
Code:
        Rows("4:58").Hidden = myHide
        MsgBox myMsgBox
These are the two lines that physically hide/unhide our rows, and return our message box to the screen. Before, they were interspersed into our code, so they could occur at different points/times. I just used variables to "capture" what we wanted to do, and waited until the very end to apply it, after it got through all the steps, so we don't get caught "mid-step" like before.
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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