Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 17
Default Infinite Loop in Error Handler

As part of a larger program, I ask users to enter a name for the
worksheet. I want to have in built in error checking to make sure
that if the user enters a blank, just numbers, special characters, or
anything else that would cause a name in naming a worksheet that it
will ask the user for a new name until they enter one that is valid.
This is the section of code that causes problems:

Public Function CheckSheet(newSheetName As String) As String
Dim ws As Worksheet

'Makes sure that the name is a valid name in Excel (not numeric,
special characters, or blank)
On Error GoTo ErrHandler:
Set Sheets(ws).Name = newSheetName

CheckSheet = newSheetName

ErrHandler:
MsgBox "Please enter a valid name.", vbInformation
newSheetName = InputBox("What would you like to name the new
week?", "Week Name")
Resume
On Error GoTo 0
End Function

I am not that familiar with using On Error and have looked at pretty
much all the how to's on the internet, but I can't figure out my
problem. I have also tried using it a number of other ways including
a while loop that tries to make use of the IsError function, but
everything I try gives me either an error message or an infinite
loop.

I think I am getting a type mismatch error when I try to set the sheet
name to the user defined (newSheetName) variable, but I'm not sure
why.

Any help is greatly appreciated!

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default Infinite Loop in Error Handler

Give this a whirl...

Public Sub CheckSheet(ByVal newSheetName As String)
Dim ws As Worksheet

Set ws = Worksheets.Add
On Error Resume Next
ws.Name = newSheetName

Do While ws.Name < newSheetName
newSheetName = InputBox("Invalid sheet name. Please try again.")
ws.Name = newSheetName
Loop
On Error GoTo 0

End Sub

--
HTH...

Jim Thomlinson


"chris" wrote:

As part of a larger program, I ask users to enter a name for the
worksheet. I want to have in built in error checking to make sure
that if the user enters a blank, just numbers, special characters, or
anything else that would cause a name in naming a worksheet that it
will ask the user for a new name until they enter one that is valid.
This is the section of code that causes problems:

Public Function CheckSheet(newSheetName As String) As String
Dim ws As Worksheet

'Makes sure that the name is a valid name in Excel (not numeric,
special characters, or blank)
On Error GoTo ErrHandler:
Set Sheets(ws).Name = newSheetName

CheckSheet = newSheetName

ErrHandler:
MsgBox "Please enter a valid name.", vbInformation
newSheetName = InputBox("What would you like to name the new
week?", "Week Name")
Resume
On Error GoTo 0
End Function

I am not that familiar with using On Error and have looked at pretty
much all the how to's on the internet, but I can't figure out my
problem. I have also tried using it a number of other ways including
a while loop that tries to make use of the IsError function, but
everything I try gives me either an error message or an infinite
loop.

I think I am getting a type mismatch error when I try to set the sheet
name to the user defined (newSheetName) variable, but I'm not sure
why.

Any help is greatly appreciated!


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 17
Default Infinite Loop in Error Handler

Thanks for the prompt reply. I think your code would have worked, but
I had to try and change it as I want to copy an existing sheet.
As is, I'm still having issues, but hopefully you will recognize the
error quickly.
I thought I would also put another related function I'm having trouble
with. The second function checks to make sure that the worksheet
name is not an existing sheet name which would also give an error. I
broke it out into a separate function because at one point I also
have to check if a sheet the user specifies exists and I was trying to
minimize code redundancy. I can do it with a while loop by scrolling
through
all the sheets, but when I have a lot of sheets, that slows it down a
lot. As a result, I was trying this shortcut, so far, without
success.

'===========================================
Public Function CheckSheet(ByVal newSheetName As String) As String
Dim ws As Worksheet

newSheetName = CheckDuplicate(newSheetName, False)

Set ws = Sheets("Template").Copy
Worksheets(ws).Move befo=Worksheets("Instructions")
On Error Resume Next
ws.Name = newSheetName

Do While ws.Name < newSheetName
newSheetName = InputBox("Invalid sheet name. Please try
again.", "Sheet Name")
ws.Name = newSheetName
Loop
On Error GoTo 0

Sheets(newSheetName).Visible = True
CheckSheet = newSheetName

End Function

'===========================================
Public Function CheckDuplicate(newSheetName As String, Dup As Boolean)
As String
Dim Duplicate As String
Dim ws As Worksheet

Duplicate = "False"

'Checks to make sure the name entered is not already a sheet name
Do While Duplicate = "False"
Duplicate = "True"
On Error Resume Next
Set ws = Sheets(newSheetName)
On Error GoTo 0
If Not Dup Then
If Not ws Is Nothing Then
MsgBox "Sheet already exists!", vbInformation
newSheetName = InputBox("What would you like to name
the new week?", "Week Name")
Duplicate = "False"
End If
Else
If ws Is Nothing Then
MsgBox "Sheet doesn't exist!", vbInformation
newSheetName = InputBox("Please reenter the sheet
name?", "Report Generator")
Duplicate = "False"
End If
End If
Loop

CheckDuplicate = newSheetName
End Function



On Sep 17, 3:34 pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
This-.com wrote:
Give this a whirl...

Public Sub CheckSheet(ByVal newSheetName As String)
Dim ws As Worksheet

Set ws = Worksheets.Add
On Error Resume Next
ws.Name = newSheetName

Do While ws.Name < newSheetName
newSheetName = InputBox("Invalid sheet name. Please try again.")
ws.Name = newSheetName
Loop
On Error GoTo 0

End Sub

--
HTH...

Jim Thomlinson



"chris" wrote:
As part of a larger program, I ask users to enter a name for the
worksheet. I want to have in built in error checking to make sure
that if the user enters a blank, just numbers, special characters, or
anything else that would cause a name in naming a worksheet that it
will ask the user for a new name until they enter one that is valid.
This is the section of code that causes problems:


Public Function CheckSheet(newSheetName As String) As String
Dim ws As Worksheet


'Makes sure that the name is a valid name in Excel (not numeric,
special characters, or blank)
On Error GoTo ErrHandler:
Set Sheets(ws).Name = newSheetName


CheckSheet = newSheetName


ErrHandler:
MsgBox "Please enter a valid name.", vbInformation
newSheetName = InputBox("What would you like to name the new
week?", "Week Name")
Resume
On Error GoTo 0
End Function


I am not that familiar with using On Error and have looked at pretty
much all the how to's on the internet, but I can't figure out my
problem. I have also tried using it a number of other ways including
a while loop that tries to make use of the IsError function, but
everything I try gives me either an error message or an infinite
loop.


I think I am getting a type mismatch error when I try to set the sheet
name to the user defined (newSheetName) variable, but I'm not sure
why.


Any help is greatly appreciated!- Hide quoted text -


- Show quoted text -



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
Error Handler sharad Excel Discussion (Misc queries) 1 September 17th 07 06:38 PM
URGENT- Can't get out of infinite loop J@Y Excel Discussion (Misc queries) 4 June 14th 07 09:41 PM
Infinite loop using Worksheet_Calculate Parker Excel Discussion (Misc queries) 2 November 3rd 06 12:25 PM
For Next Infinite Loop Naji Excel Discussion (Misc queries) 5 January 13th 06 06:56 PM
Error Handler Not Working Bill Excel Discussion (Misc queries) 0 August 25th 05 07:13 PM


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