Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default check whether worksheet's there or not

Hi ya

I want to activate a certain Worksheet 'Results' using a macro. Becaus
I don't know for you whether it exists yet, I want to check that wit
an If-statement and then act accordingly (create the sheet if it's no
there yet). The expression below (.Activate) doesn't work, nor di
using 'Not Sheets(...) Is Nothing' but I look for something of thi
kind as I would like to avoid a loop to check for the sheets
existence


If (Sheets("Results").Activate) Then
...
Else
Sheets.Add
ActiveSheet.Name = "Results"
End If


Any ideas are highly appreciated, cheers!

fabaliciou

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default check whether worksheet's there or not

Hi
try the following

sub foo()
dim wks as worksheet
on error resume next
set wks = worksheets("Results")
on error goto 0
if wks is nothing then
msgbox "Result sheet does not exist"
exit sub
end if
wks.activate
end sub

-----Original Message-----
Hi ya

I want to activate a certain Worksheet 'Results' using a

macro. Because
I don't know for you whether it exists yet, I want to

check that with
an If-statement and then act accordingly (create the

sheet if it's not
there yet). The expression below (.Activate) doesn't

work, nor did
using 'Not Sheets(...) Is Nothing' but I look for

something of this
kind as I would like to avoid a loop to check for the

sheets'
existence


If (Sheets("Results").Activate) Then
...
Else
Sheets.Add
ActiveSheet.Name = "Results"
End If


Any ideas are highly appreciated, cheers!

fabalicious


---
Message posted from http://www.ExcelForum.com/

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 208
Default check whether worksheet's there or not

Hi
You can test with this function, which applies to the Active WorkBook

'Tests to see if a worksheet with the given name exists in the active workbook
Function IsSheetThere(shName As String) As Boolean
Dim DummyWks As String
IsSheetThere = False
On Error Resume Next
DummyWks = ActiveWorkbook.Worksheets(shName).Name
If Err.Number = 0 Then IsSheetThere = True
End Function

Usage:
If not IsSheetThere("Results") then
Sheets.Add
ActiveSheet.Name = "Results"
End If

Other handy functions/subs in the same stable a

Function IsNameInWorkBook(stName As String) As Boolean
'Tests to see if the name stName exists in this workbook
'Adapted from sub in John Green's book

Dim x As String
IsNameInWorkBook = False
On Error Resume Next
x = ActiveWorkbook.Names(stName).Name
If Err.Number = 0 Then IsNameInWorkBook = True
End Function

Public Sub AddSheet(TheSheetName As String)
'Replace an existing sheet or insert if not there

Dim WsNew As Worksheet 'we will insert a fresh worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(TheSheetName).Delete
On Error GoTo 0
Application.DisplayAlerts = True
'If worksheet is not there the error in deletion is ignored
'Add a new sheet
Set WsNew = Worksheets.Add 'Makes wsNew the active sheet
WsNew.Name = TheSheetName
Set WsNew = Nothing
End Sub

Public Function IsFileOpen(filename As String)
'Lifted from Microsoft KB
' This function checks to see if a file is open or not. If the file is
' already open, it returns True. If the file is not open, it returns
' False. Otherwise, a run-time error will occur because there is
' some other problem accessing the file.

Dim filenum As Integer, errnum As Integer

On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
IsFileOpen = False
End Select
End Function


Function IsWorkBookOpen(WorkBookName As String) As Boolean
'See Green p81. Checks workbook is open on the machine
Dim Wkb As Workbook
On Error Resume Next
Set Wkb = Workbooks(WorkBookName)
If Not Wkb Is Nothing Then
IsWorkBookOpen = True
End If
Set Wkb = Nothing
End Function

regards
Paul
fabalicious wrote in message ...
Hi ya

I want to activate a certain Worksheet 'Results' using a macro. Because
I don't know for you whether it exists yet, I want to check that with
an If-statement and then act accordingly (create the sheet if it's not
there yet). The expression below (.Activate) doesn't work, nor did
using 'Not Sheets(...) Is Nothing' but I look for something of this
kind as I would like to avoid a loop to check for the sheets'
existence


If (Sheets("Results").Activate) Then
..
Else
Sheets.Add
ActiveSheet.Name = "Results"
End If


Any ideas are highly appreciated, cheers!

fabalicious


---
Message posted from http://www.ExcelForum.com/

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
can we map calender in to excel worksheet's cell. Anand Pasunoori Excel Discussion (Misc queries) 1 July 15th 09 05:44 PM
Small 1-2 on worksheet's left in Excel Paddy New Users to Excel 2 October 27th 06 05:56 PM
Cell to equal a worksheet's name David P. Excel Discussion (Misc queries) 3 June 11th 05 12:57 AM
Transforming a worksheet's functionality? Arlen Excel Discussion (Misc queries) 1 February 4th 05 10:57 PM
Compare Worksheet's Rich[_19_] Excel Programming 4 October 30th 03 04:26 PM


All times are GMT +1. The time now is 09:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"