ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Correct syntax for Active Worksheet (https://www.excelbanter.com/excel-programming/333513-correct-syntax-active-worksheet.html)

[email protected]

Correct syntax for Active Worksheet
 
Hello:
I am trying to write VBA code to display a message in the Workbook,
BeforePrint event.
I have three worksheets in the workbook, named SheetA, SheetB, and
SheetC.

I want a different message to show depending upon whether SheetA is the
active sheet, or SheetB or SheetC

The code would look something like this:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet = Sheets("SheetA") Then MsgBox "Hello"
etc etc
End Sub

Obviously, I am not using the correct syntax "ActiveSheet" as it is
giving me an error.

I would appreciate if you could help me with the correct syntax

TIA

Vinay


Toppers

Correct syntax for Active Worksheet
 
Try

f ActiveSheet.Name = "SheetA" Then MsgBox "Hello"

" wrote:

Hello:
I am trying to write VBA code to display a message in the Workbook,
BeforePrint event.
I have three worksheets in the workbook, named SheetA, SheetB, and
SheetC.

I want a different message to show depending upon whether SheetA is the
active sheet, or SheetB or SheetC

The code would look something like this:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet = Sheets("SheetA") Then MsgBox "Hello"
etc etc
End Sub

Obviously, I am not using the correct syntax "ActiveSheet" as it is
giving me an error.

I would appreciate if you could help me with the correct syntax

TIA

Vinay



Sean Connolly[_2_]

Correct syntax for Active Worksheet
 
Hello Vinay,

Try this ...

-----

Private Sub Workbook_BeforePrint(Cancel As Boolean)
Select Case ActiveSheet.Name
Case "Sheet1"
MsgBox ("Insert message for Sheet1 here.")
Case "Sheet2"
MsgBox ("Insert message for Sheet2 here.")
Case "Sheet3"
MsgBox ("Insert message for Sheet3 here.")
Case Else
MsgBox ("Some other Sheet is currently active. Insert message
here.")
End Select
End Sub

-----

HTH, Sean.

" wrote:

Hello:
I am trying to write VBA code to display a message in the Workbook,
BeforePrint event.
I have three worksheets in the workbook, named SheetA, SheetB, and
SheetC.

I want a different message to show depending upon whether SheetA is the
active sheet, or SheetB or SheetC

The code would look something like this:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet = Sheets("SheetA") Then MsgBox "Hello"
etc etc
End Sub

Obviously, I am not using the correct syntax "ActiveSheet" as it is
giving me an error.

I would appreciate if you could help me with the correct syntax

TIA

Vinay



Tom Ogilvy

Correct syntax for Active Worksheet
 
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If lcase(ActiveSheet.Name) = lcase("SheetA") Then MsgBox "Hello"

End Sub

--
Regards,
Tom Ogilvy

wrote in message
oups.com...
Hello:
I am trying to write VBA code to display a message in the Workbook,
BeforePrint event.
I have three worksheets in the workbook, named SheetA, SheetB, and
SheetC.

I want a different message to show depending upon whether SheetA is the
active sheet, or SheetB or SheetC

The code would look something like this:

Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet = Sheets("SheetA") Then MsgBox "Hello"
etc etc
End Sub

Obviously, I am not using the correct syntax "ActiveSheet" as it is
giving me an error.

I would appreciate if you could help me with the correct syntax

TIA

Vinay




[email protected]

Correct syntax for Active Worksheet
 
Thanks Toppers, Tom & Sean
Your suggestions helped
Vinay



All times are GMT +1. The time now is 03:01 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com