ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   ActiveSheet test (https://www.excelbanter.com/excel-programming/409374-activesheet-test.html)

Gustaf

ActiveSheet test
 
This ought to be simple, but I can't find any working examples. I want to test that I'm on the right sheet, and if not exit the procedure. I've found the ActiveSheet property, but can't figure out how to use it. I'd like something like:

If ActiveSheet.Name < "Sheet1" Then Exit Sub

I also wonder if there's ever a risk that code written in the Sheet1 module is ever run on another sheet? Perhaps what I want to do is never necessary?

Gustaf

Gary''s Student

ActiveSheet test
 
Your idea is correct. For example:

Sub gustaf()
If ActiveSheet.Name = "Sheet1" Then
MsgBox ("we are on Sheet1")
Else
MsgBox ("we are on some other sheet")
End If
End Sub

There are other ways of thinking about this issue. If you need the
ActiveSheet to be Sheet1, you can just Activate it.

You can also write your code to not depend on being on a specific sheet.
For example, instead of:

Range("A1").Value=123
use:
Sheets("Sheet1").Range("A1").Value=123
--
Gary''s Student - gsnu200779


"Gustaf" wrote:

This ought to be simple, but I can't find any working examples. I want to test that I'm on the right sheet, and if not exit the procedure. I've found the ActiveSheet property, but can't figure out how to use it. I'd like something like:

If ActiveSheet.Name < "Sheet1" Then Exit Sub

I also wonder if there's ever a risk that code written in the Sheet1 module is ever run on another sheet? Perhaps what I want to do is never necessary?

Gustaf



All times are GMT +1. The time now is 11:00 AM.

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