How do I make a macro only affect a single worksheet?

gordonc068

New Member
Joined
Feb 8, 2016
Messages
12
I have some code that I took from a different Forum post which I've put below. I'm wanting to make it only apply to a specific worksheet names "New Hire Form". Can anyone adjust this do only happen if "New Hire Form" is the selected sheet? Also, do I put it in the "thisworkbook", Sheet, or Module section of the VBA application?

It is currently working, it checks a range called "Mandatory" and if they are all filled, then you can save or close, if not then it wont let you (forcing them to not send incomplete pages). There is a Skip it section in case you need to take a break in the middle of it, you are aware it's not done but you still need to save/close. Any help would be much appreciated.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = ForceDataEntry()
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Cancel = ForceDataEntry()
End Sub
Private Function ForceDataEntry() As Boolean


If Range("Skipit") = "YES" Then
MsgBox ("As you wish")
Exit Function
End If


Dim rng As Range
Dim c As Variant
Dim rngCount As Integer
Dim CellCount As Integer



Set rng = Range("Mandatory")
rngCount = rng.Count


CellCount = 0
For Each c In rng
If Len(c) > 0 Then
CellCount = CellCount + 1
End If
Next c
ForceDataEntry = False
If CellCount <> rngCount Then
ForceDataEntry = True
MsgBox ("1. Check all highlighted feilds are complete or;" & vbNewLine & "2. See Cell E61")
End If
End Function
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
before any worksheet member, for example, Range, add... Worksheets("Sheet1").

obviously use the correct sheet name and you can specify the sheet to be used wherever in the macro. Without it, it uses the ActiveSheet which can change depending what you do
 
Last edited:
Upvote 0
Add this line to the beginning:

Code:
if ucase(activesheet.name) <> "[COLOR=#333333]NEW HIRE FORM" then exit function[/COLOR]
 
Upvote 0
One other thing you can do is put With Worksheets("New Hire Form") in the beginning of the function ForceDataEntry() along with its respective End With right before the end

i.e.

Private Function ForceDataEntry() As Boolean

With Worksheets("New Hire Form")





End With

End Function

This will result in all the references to Range() etc. being understood to refer only to the New Hire Form sheet.
 
Upvote 0
My apologies. I made a mistake in regard to what I said in my post, I believe you also need to add a period before each worksheet member:

i.e.

.Range("Mandatory")

I'm relatively new to Excel myself so maybe some of the other more experienced peopel who posted can verify if this is correct. Cerfani's solution will work as well.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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