Run a macro based on text in a cell?

camhall22

New Member
Joined
Feb 2, 2012
Messages
19
Hello

Im new to this and was hoping for some help/advice. What I would like to do it run a macro when specific text is entered into a specific cell. This is for a template I am making.

For example when a user entered the words "None" or "Zero" into cell D4 - the macro is run automatically. If any other value is entered, do nothing

Any help would be much appreciated
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Paste this into the Sheet module of your workbook
Adjust the "Do Stuff" line to suit your actual needs

Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("D4")
If target.Value = "Zero" Or target.Value = "None" Then
    'Do Stuff
End If
End Sub
 
Upvote 0
Hi,

Ive been doing this but no joy: A specific text is selected from a drop down list to trigger the macro
Code:
Sub worksheet_change(ByVal target As Range)
Set target = Range("c8")
If target.Value = "Long PA Professional with SPQ" Or target.Value = "Long PA Professional" Then
Sub Merge()
'
' Merge Macro
'


'
    Range("D16").Select
    ActiveCell.FormulaR1C1 = "Occupied By Long PA"
    Range("D16:I16").Select
    With Selection.Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With Selection.Font
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    ActiveWindow.ScrollColumn = 3
    ActiveWindow.ScrollColumn = 1
    Range("C16").Select
End Sub
 
Last edited by a moderator:
Upvote 0
I am very very new to this and I'm struggling even after doing a lot of reading. I am trying to do what seems to be pretty simple. I want to run a simple macro that unhides certain rows when certain text is in a cell. I recorded a macro that unhides the cells but I can't get it to run when that text is in the cell. Basically what I want is if cell B3=Tower then unhide rows 51-55. Can someone help me? Thank you in advance.
 
Last edited by a moderator:
Upvote 0
The basic code would be:
Code:
If Range("B3").value = "Tower" then range("51:55").Entirerow.hidden = False

How do you want it triggered?
 
Upvote 0
Cell B3 is a drop down so if I select Tower from the drop down that's how I would like it triggered. Is that possible?

The basic code would be:
Code:
If Range("B3").value = "Tower" then range("51:55").Entirerow.hidden = False

How do you want it triggered?
 
Upvote 0
Right-click the worksheet tab, choose View Code and paste this in:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3")) Is Nothing Then
    If Range("B3").Value = "Tower" Then Range("51:55").EntireRow.Hidden = False
End If
End Sub
 
Upvote 0
Thanks so much! I hate to be a pain but is there a way to then re-hide those cells if I change what I pick in the drop down? That is, I pick Tower so the rows unhide but then I change it to something else so they hide again?

Right-click the worksheet tab, choose View Code and paste this in:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3")) Is Nothing Then
    If Range("B3").Value = "Tower" Then Range("51:55").EntireRow.Hidden = False
End If
End Sub
 
Upvote 0
Sure, just change it to:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3")) Is Nothing Then
    Range("51:55").EntireRow.Hidden = (Range("B3").Value = "Tower")
End If
End Sub
 
Upvote 0
When I use that command it unhides the rows no matter what I choose from the dropdown menu. I'm hoping I can set it up so that when I chose "Tower" it unhides those rows and even after I have chosen "Tower" and then change it to something else, it would hide those rows.

Sure, just change it to:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B3")) Is Nothing Then
    Range("51:55").EntireRow.Hidden = (Range("B3").Value = "Tower")
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,448
Members
448,966
Latest member
DannyC96

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