How can I run a Macro If any cell in col G:G contains a specific text Value

norts55

Board Regular
Joined
Jul 27, 2012
Messages
183
Hello,
This seems like an easy one but I have been searching and searching for similar code but just can't seem to find what I am looking for and get it to work.

Can someone please tell me how to Search my in active sheet, column G for the text value "My Text" and if "My Text" is found in any cell of column G, it runs the macro "My_Macro". If it does not find "My Text", it does nothing.

Any help would be appreciated as I have been struggling with this. I have code below that I think is close but I am missing something.


VBA Code:
If (Range("G:G") = "My Text") Then
        Application.Run "'" & ActiveWorkbook.Name & "'" & "!My_Macro"
        End If
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
VBA Code:
Option Explicit

Sub SrchColC()
    Dim rng As Range
    Dim shtSrc As Worksheet
    Dim c As Range
    Dim i As Integer
    
    
    With Application
        .ScreenUpdating = False
        .DisplayStatusBar = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    
 'don't scan the entire column...
    Set rng = Application.Intersect(shtSrc.Range("G:G"), shtSrc.UsedRange)

    For Each c In rng.Cells
        If c.Value = "My Text" Then
            
             'Nameofyourmacrohere
          
            destRow = destRow + 1

        End If
    Next
    
     With Application
        .ScreenUpdating = True
        .DisplayStatusBar = True
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
    End With
    
End Sub
 
Upvote 0
Thank you very much for the help on the, I seem to be getting an error.

1676588238297.png


1676588222569.png
 
Upvote 0
Try this :

VBA Code:
Option Explicit

Sub Copy_n_Paste()
On Error Resume Next

    Dim srchtrm As String
    Dim rng As Range, destRow As Long
    Dim shtSrc As Worksheet
    Dim c As Range
    Dim i As Integer
    
    
    With Application
        .ScreenUpdating = False
        .DisplayStatusBar = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With
    
    Set shtSrc = Sheets("Sheet1")    'source sheet

    'don't scan the entire column...
    Set rng = Application.Intersect(shtSrc.Range("G:G"), shtSrc.UsedRange)

    For Each c In rng.Cells
        If c.Value = "My Text" Then
        
         'Your Macro Name Here
            
            destRow = destRow + 1

        End If
    Next
    
    With Application
        .ScreenUpdating = True
        .DisplayStatusBar = True
        .Calculation = xlCalculationAutomatic
        .EnableEvents = True
    End With
        
    Application.CutCopyMode = False
    Sheets("Sheet1").Range("A1").Select

End Sub
 
Upvote 0
Try
VBA Code:
Sub RunMacro()
Dim Frng As Range
Set Frng = Sheets("Sheet1").Range("G:G").Find("My text")
If Not Frng Is Nothing Then
Run "MyMacro"
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,216,404
Messages
6,130,376
Members
449,578
Latest member
TT123

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