PC Review


Reply
Thread Tools Rate Thread

detecting open worksheet in open work book

 
 
windsurferLA
Guest
Posts: n/a
 
      14th Oct 2007
Using Excel'97 running under WinXP-pro w/ SR-2

To prevent an error when activating a specific worksheet in an open
workbook, I need to know if a specific worksheet is already the active
worksheet.

Forest example, a command such as:

If (Worksheets("scratch").Range("E3").Value = "") Then .... End If

can be used to tell if the a specific cell contains a value.



Is there a better way to determine if a specific worksheet in a workbook
is the active worksheet than by using the following code?

If((worksheets.active)= worksheets("scratch")) Then .... End If
 
Reply With Quote
 
 
 
 
Bill Renaud
Guest
Posts: n/a
 
      14th Oct 2007
Write a UDF (User-Defined-Function) like the following:

'----------------------------------------------------------------------
Public Function IsActiveWorksheet(strWorksheetName As String) As Boolean
Dim wsActive As Worksheet
Dim wsTest As Worksheet

On Error GoTo ExitFunction

Set wsActive = ActiveSheet
Set wsTest = Worksheets(strWorksheetName)

IsActiveWorksheet = (wsTest.Name = wsActive.Name)

ExitFunction:
End Function


If the active sheet happens to be a chart, the error handler will cause the
function to return False anyway.
--
Regards,
Bill Renaud



 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      14th Oct 2007
You could try something like

If ActiveSheet.Name = Worksheets("Scratch").Name Then
' Scratch is active
Else
' Scratch is not active. make it so.
Worksheets("Scratch").Activate
End If

If you always need Scratch to be active when the workbook opens, just use

Private Sub Workbook_Open()
Me.Worksheets("Scratch").Activate
End Sub

This code doesn't care is Scratch is already active, it just ensures that it
is active.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"windsurferLA" <(E-Mail Removed)> wrote in message
news:47119d29$0$9546$(E-Mail Removed)...
> Using Excel'97 running under WinXP-pro w/ SR-2
>
> To prevent an error when activating a specific worksheet in an open
> workbook, I need to know if a specific worksheet is already the active
> worksheet.
>
> Forest example, a command such as:
>
> If (Worksheets("scratch").Range("E3").Value = "") Then .... End If
>
> can be used to tell if the a specific cell contains a value.
>
>
>
> Is there a better way to determine if a specific worksheet in a workbook
> is the active worksheet than by using the following code?
>
> If((worksheets.active)= worksheets("scratch")) Then .... End If


 
Reply With Quote
 
windsurferLA
Guest
Posts: n/a
 
      14th Oct 2007
Thanks for prompt reply and very useful suggestion.

Your code for ensuring sheet is active that doesn't care if sheet is
lready active is just want I wanted. It also introduced me to the short
cut "me" keyword which until now I had not used, even though I written
hundreds of macros.



Chip Pearson wrote:
> You could try something like
>
> If ActiveSheet.Name = Worksheets("Scratch").Name Then
> ' Scratch is active
> Else
> ' Scratch is not active. make it so.
> Worksheets("Scratch").Activate
> End If
>
> If you always need Scratch to be active when the workbook opens, just use
>
> Private Sub Workbook_Open()
> Me.Worksheets("Scratch").Activate
> End Sub
>
> This code doesn't care is Scratch is already active, it just ensures
> that it is active.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy worksheet from one book to open workbook Nev Microsoft Excel Discussion 1 27th Feb 2010 01:23 PM
work book open _ event Hassan Microsoft Excel Worksheet Functions 1 3rd Jul 2008 12:49 PM
App_change event to open all work book mvenkatesan45@gmail.com Microsoft Excel Misc 1 2nd Aug 2007 03:52 PM
open work book nowfal Microsoft Excel Programming 2 16th Aug 2005 10:30 PM
Making a button to open a work book =?Utf-8?B?RGFycmVsbA==?= Microsoft Excel Worksheet Functions 2 3rd Feb 2004 01:26 PM


Features
 

Advertising
 

Newsgroups
 


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