Limit Quick Tasks macro to a specific worksheet

AndyTampa

Board Regular
Joined
Aug 14, 2011
Messages
196
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I've tried searching here and Google and just can't seem to ask the right way to get the right search results.

I put a personal macro in my Quick Access Toolbar to use on a spreadsheet I use every day. I make a new one every month. When there is too much text in a cell, it merges with the 4 cells next to it and corrects the height automatically.

The problem I have is when I accidentally run the macro (via the Quick Access button) on a different sheet or workbook. The macro runs and there is no "undo". I'm trying to find a way for this button to only work when I am on a particular tab named "Packages Worked" on a workbook that always has "Packages Worked" in the title.

If I'm not on the sheet or it isn't even open, I just want a MsgBox that the macro only works on that particular sheet. I tried the below line, but it got a debug error when the workbook was closed and I was in another workbook.

VBA Code:
If ActiveWorkbook.Worksheets("Packages Worked") Is ActiveSheet Then

I don't even have a clue how to check if the workbook name contains specific text. It seems like it would be simple, but searching for the information is getting me all the wrong answers.
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You might want to try something like this instead:
VBA Code:
 If ActiveSheet.Name = "Packages Worked" Then
 
Upvote 0
Give this a try. Place your code where indicated.
VBA Code:
Sub checkWorkbook()
    If ActiveWorkbook.Name Like "*Packages Worked*" And Evaluate("isref('" & "Packages Worked" & "'!A1)") Then
        'your code here
    Else
        MsgBox "The macro works only on sheet 'Packages Worked'."
    End If
End Sub
 
Upvote 0
Give this a try. Place your code where indicated.
VBA Code:
Sub checkWorkbook()
    If ActiveWorkbook.Name Like "*Packages Worked*" And Evaluate("isref('" & "Packages Worked" & "'!A1)") Then
        'your code here
    Else
        MsgBox "The macro works only on sheet 'Packages Worked'."
    End If
End Sub
This works to prevent the macro from working when I'm in the wrong workbook but that's only half the requirement.

If I'm in the right workbook, but on the wrong worksheet of that workbook, the macro still ran. I don't understand the second part of the macro from Evaluate onward. It looks like it's specifying cell A1. I don't know why you would reference a cell. So I changed the second half of the macro to what rlv01 said and it worked for what I needed.

You might want to try something like this instead:
VBA Code:
 If ActiveSheet.Name = "Packages Worked" Then

So now I have:

VBA Code:
Sub checkWorkbook()
    If ActiveWorkbook.Name Like "*Packages Worked*" And ActiveSheet.Name = "Packages Worked" Then
        'your code here
    Else
        MsgBox "The macro works only on sheet 'Packages Worked'."
    End If
End Sub

I don't think I can mark both as the solution though or else I would. I guess I have to mark my own post as the solution to give you both credit.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,325
Messages
6,124,252
Members
449,149
Latest member
mwdbActuary

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