How to run a macro based on values in another sheet’s column?

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
Hello. Is there a way to run a macro based on values in a column in another sheet?

I have a certain workbook with many macros and sheets. Then I have a main macro that runs all of these back to back. This is where I want to add this code to. Want I want to do is run macro1 if Column A in Sheet “Times” contains the word “Unknown” and if not run macro2. Thank you to anyone willing to help.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You did not specify how you wanted to initiate the macro so I assumed you would assign it to a Forms Control button. In that case this code needs to be copied to code module1 or any other public code module.
VBA Code:
Sub t()
With Sheets("Times")
    If Application.CountIf(.Range("A:A"), "Unknown") > 0 Then
        Macro1
    Else
        Macro2
    End If
End With
End Sub
 
Upvote 0
Solution
You did not specify how you wanted to initiate the macro so I assumed you would assign it to a Forms Control button. In that case this code needs to be copied to code module1 or any other public code module.
VBA Code:
Sub t()
With Sheets("Times")
    If Application.CountIf(.Range("A:A"), "Unknown") > 0 Then
        Macro1
    Else
        Macro2
    End If
End With
End Sub

This works. Thank you so much. Just added "Call" next to the names as this will be in my longer main code
 
Upvote 0
It should have worked without the 'Call' but it is your option.
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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