I am getting an error on 'If Sh.Name = "Daily" Then' in this event: Private Sub…
- User
I am getting an error on 'If Sh.Name = "Daily" Then' in this event: Private Sub Worksheet_Activate() Application.Calculate
' Display the message only for the "Daily" sheet If Sh.Name = "Daily" Then 'If ActiveSheet.Name = "Daily" Then If ShouldShowMessage() Then Dim answer As VbMsgBoxResult answer = MsgBox("HAVE YOU PRINTED ALL STAFF SCHEDULE SHEETS FOR TODAY?" & vbNewLine & vbNewLine & "Do not update this sheet until you have done so", vbQuestion + vbYesNo, "Confirmation") If answer = vbNo Then MsgBox "The message will reappear the next time you activate this sheet." Else SaveLastPrintedDate End If End If End If 'Application.Wait Now + TimeValue("00:00:01") Call TaskBackUpEnd Sub
- Assistant
The error you are getting is because the variable "Sh" is not defined in the code. In order to fix this error, you need to declare and set the "Sh" variable before using it in the If statement. Here's an updated version of your code with the necessary changes:
Private Sub Worksheet_Activate() Dim Sh As Worksheet ' Define the variable Sh as a worksheet Set Sh = ActiveSheet ' Set Sh to the currently activated sheet
Application.Calculate ' Display the message only for the "Daily" sheet If Sh.Name = "Daily" Then If ShouldShowMessage() Then Dim answer As VbMsgBoxResult answer = MsgBox("HAVE YOU PRINTED ALL STAFF SCHEDULE SHEETS FOR TODAY?" & vbNewLine & vbNewLine & "Do not update this sheet until you have done so", vbQuestion + vbYesNo, "Confirmation") If answer = vbNo Then MsgBox "The message will reappear the next time you activate this sheet." Else SaveLastPrintedDate End If End If End If 'Application.Wait Now + TimeValue("00:00:01") Call TaskBackUpEnd Sub
By declaring the "Sh" variable as a worksheet and setting it to the active sheet, you can then use it in the If statement to check the name of the sheet.
- User
Can you please change this 'Set Sh = ActiveSheet' to This workbook sheet 'Daily'
- Assistant
Certainly! If you want to specifically refer to the "Daily" sheet in the workbook, you can use the following code to set the "Sh" variable:
Dim Sh As Worksheet Set Sh = ThisWorkbook.Sheets("Daily")With this change, the "Sh" variable will be set to the "Daily" sheet in the workbook. Here's the updated code:
Private Sub Worksheet_Activate() Dim Sh As Worksheet Set Sh = ThisWorkbook.Sheets("Daily") Application.Calculate ' Display the message only for the "Daily" sheet If Sh.Name = "Daily" Then If ShouldShowMessage() Then Dim answer As VbMsgBoxResult answer = MsgBox("HAVE YOU PRINTED ALL STAFF SCHEDULE SHEETS FOR TODAY?" & vbNewLine & vbNewLine & "Do not update this sheet until you have done so", vbQuestion + vbYesNo, "Confirmation") If answer = vbNo Then MsgBox "The message will reappear the next time you activate this sheet." Else SaveLastPrintedDate End If End If End If 'Application.Wait Now + TimeValue("00:00:01") Call TaskBackUp End Sub