Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need to search workbook for NEW worksheet

Here is the situation.

I have a work book that by pushing buttons a macro uses data in th
workbook and create a worksheet with that has that data graphed on it


I want the macro to first look for the sheet, to make sure that it i
not already created before it runs. I am currently using this code:

Dim wSht As Worksheet
Dim shtName As String
shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht

to seach through the workbook for different sheets. So when I run th
code (and the ws does not exist yet) it works fine.

But when I run the macro a second time (and now a sheet already exist
with that name) it just comes to the point where the macro names th
chart, and stoppes there. and does not error.

When I debug the code I can see the "wSht.Name" skip over the new ws.
Is there any chance that "Worksheets" does not contain the new w
because I have not saved the program yet?

please help

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Need to search workbook for NEW worksheet

Sub test()
Dim wks As Worksheet, strName As String

strName = Format(Now, "mmmm_yyyy")

On Error Resume Next
Set wks = Worksheets(strName)
If Err.Number Then
Set wks = Worksheets.Add
wks.Name = strName
Err.Clear
Else
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
End If
On Error GoTo 0
End Sub


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"RogerDaShrubber " wrote in
message ...
Here is the situation.

I have a work book that by pushing buttons a macro uses data in the
workbook and create a worksheet with that has that data graphed on it.


I want the macro to first look for the sheet, to make sure that it is
not already created before it runs. I am currently using this code:

Dim wSht As Worksheet
Dim shtName As String
shtName = Format(Now, "mmmm_yyyy")
For Each wSht In Worksheets
If wSht.Name = shtName Then
MsgBox "Sheet already exists...Make necessary " & _
"corrections and try again."
Exit Sub
End If
Next wSht

to seach through the workbook for different sheets. So when I run the
code (and the ws does not exist yet) it works fine.

But when I run the macro a second time (and now a sheet already exists
with that name) it just comes to the point where the macro names the
chart, and stoppes there. and does not error.

When I debug the code I can see the "wSht.Name" skip over the new ws.
Is there any chance that "Worksheets" does not contain the new ws
because I have not saved the program yet?

please help.


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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need to search workbook for NEW worksheet

Thank you that does detect a ws with the same name, but is there a wa
to do this with out creating the new ws "MMMM_YYYY"?
thanks again

--
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
Search one worksheet to pull data into another worksheet HyperMite Excel Worksheet Functions 6 March 4th 09 01:53 PM
I need an Equation in a cell to search a workbook not worksheet MWhaley Excel Worksheet Functions 5 November 2nd 08 12:27 AM
Search a Workbook jknapp1005 Excel Discussion (Misc queries) 3 January 18th 07 03:27 PM
Search Data from one Workbook and copy it into another Workbook Matz Excel Discussion (Misc queries) 0 August 4th 06 10:45 AM
Search workbook fda secretary Excel Discussion (Misc queries) 2 August 24th 05 07:48 PM


All times are GMT +1. The time now is 12:47 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"