Thread: Loop in Excel?
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
K Dales[_2_] K Dales[_2_] is offline
external usenet poster
 
Posts: 1,163
Default Loop in Excel?

I would advise against the Select Case to deal with this. The problem is
that if you (or anyone else) ever changes a sheet name, adds or deletes a
sheet, whatever, you would need to revise the code. Better would be a For
Each... loop:

Dim ThisSheet as Worksheet, SheetFound as Boolean

SheetFound = False
While Not(SheetFound)
UserInputName = InputBox ("Which sheet?")
For Each ThisSheet in ThisWorkbook.Worksheets
SheetFound = SheetFound Or (ThisSheet.Name = UserInputName)
Next ThisSheet
If Not(SheetFound) Then MsgBox "Not found!",vbExclamation
WEnd



"Matt" wrote:

I'm trying to write a macro that will loop a process.

i.e. a text box pops up and asks "What sheet do you want to pull data
from?" and if the name is incorrect, it'll show that name is incorrect
and loops back to the beginning askin what sheet they want to pull the
data from again. If the sheet entered matches, it will go directly to that
sheet in the workbook.

I was thinking maybe it's better to use a SELECT Case and have it evaluate
whether or not the text entered equals any of the sheet names in the workbook.

Any ideas?

Thanks in advance,
Matt