Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Find existing worksheet

Hi,

Hoping someone can help me.

I am creating a Macro in Excel 2000 and have run across a
problem. I have a file with numerous worksheets but
would like to search for one particular worksheet 'RECORD
TYPE THREE' in the file and if not found then add a new
worksheet with that name. I can add a new one, but do
not know how to search for an existing worksheet by that
name. Any help would be greatly appreciated.

Thanks,
Rob
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Find existing worksheet

Hi Rob

One way

Sub test()
Dim N As Long
On Error Resume Next
With ThisWorkbook.Worksheets
N = Len(.Item("'RECORD TYPE THREE").Name)
If N = 0 Then
.Add(after:=.Item(.Count)).Name = "'RECORD TYPE THREE"
End If
End With
End Sub


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2000-2003)
www.rondebruin.nl



"Rob" wrote in message ...
Hi,

Hoping someone can help me.

I am creating a Macro in Excel 2000 and have run across a
problem. I have a file with numerous worksheets but
would like to search for one particular worksheet 'RECORD
TYPE THREE' in the file and if not found then add a new
worksheet with that name. I can add a new one, but do
not know how to search for an existing worksheet by that
name. Any help would be greatly appreciated.

Thanks,
Rob



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Find existing worksheet

Here's an alternate method, though not necessarily better:

Sub AddSheetIfDoesntExist()

'Store the name of the worksheet for multiple use
Const szNEW_SHEET As String = "RECORD TYPE THREE"

'Use thsi variable to check for the worksheet's existence
Dim wks As Worksheet

'Suppress VBA/Macro Error handling
On Error Resume Next
'Try to store
Set wks = ActiveWorkbook.Worksheets(szNEW_SHEET)
'From this line onward, when an error occurs jump to the "Error:" line
On Error GoTo Error

'If the variable has nothing in it then the worksheet doesn't exist
If wks Is Nothing Then
'Add the worksheet
ActiveWorkbook.Worksheets.Add
'Rename the new worksheet (which when added becomes the active
sheet)
ActiveSheet.Name = szNEW_SHEET
End If

'Stop the macro here. The code below it only runs when an error exists
Exit Sub
Error:
'Display the error number and description in a message box
MsgBox Err.Number & vbLf & vbLf & Err.Description, vbCritical
End Sub


--
__________________
Robert Rosenberg
R-COR Consulting Services
Microsoft MVP - Excel


"Rob" wrote in message
...
Hi,

Hoping someone can help me.

I am creating a Macro in Excel 2000 and have run across a
problem. I have a file with numerous worksheets but
would like to search for one particular worksheet 'RECORD
TYPE THREE' in the file and if not found then add a new
worksheet with that name. I can add a new one, but do
not know how to search for an existing worksheet by that
name. Any help would be greatly appreciated.

Thanks,
Rob



  #4   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Find existing worksheet

The following FOR statement loops through each worksheet
in the active workbook, add your code below to check the
worksheet name and if the FOR loop has finished, add a new
worksheet named (X).

For Each Worksheet In Worksheets

This is the only way i know of checking if a particular
worksheet name exists.

-----Original Message-----
Hi,

Hoping someone can help me.

I am creating a Macro in Excel 2000 and have run across a
problem. I have a file with numerous worksheets but
would like to search for one particular worksheet 'RECORD
TYPE THREE' in the file and if not found then add a new
worksheet with that name. I can add a new one, but do
not know how to search for an existing worksheet by that
name. Any help would be greatly appreciated.

Thanks,
Rob
.

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
Find macro for already existing graph Mike Excel Discussion (Misc queries) 2 May 16th 07 04:27 PM
Despite data existing in Excel 2002 spreadsheet Find doesn't find AnnieB Excel Discussion (Misc queries) 1 June 16th 06 02:15 AM
How do I conditionally build worksheet from existing worksheet? Bob G Excel Discussion (Misc queries) 1 July 3rd 05 06:40 PM
How do I find out how many different cell formats an existing wor. wheelsii Excel Discussion (Misc queries) 1 March 24th 05 04:58 PM
find existing worsheet monika Excel Programming 10 February 5th 04 02:03 PM


All times are GMT +1. The time now is 09:55 AM.

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"