Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 516
Default Loop in Excel?

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



  #2   Report Post  
Posted to microsoft.public.excel.programming
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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Loop in Excel?

Simpler is:

Option Compare Text

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Loop in Excel?

or

Dim ThisSheet As Worksheet
Dim sh As Worksheet
Dim UserInputName As String

Do
UserInputName = InputBox("Which sheet?")
On Error Resume Next
Set sh = Worksheets(UserInputName)
If sh Is Nothing Then
MsgBox UserInputName & " does not exist, re-try"
End If
Loop Until Not sh Is Nothing
On Error GoTo 0

Worksheets(UserInputName).Activate

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Charlie" wrote in message
...
Simpler is:

Option Compare Text

.
.
.

Dim ThisSheet As Worksheet

Do
UserInputName = InputBox("Which sheet?")
If UserInputName = "" Then Exit Sub
For Each ThisSheet In ThisWorkbook.Worksheets
If ThisSheet.Name = UserInputName Then
ThisSheet.Activate
Exit Sub
End If
Next
Loop


"K Dales" wrote:

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





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 516
Default Loop in Excel?

Thanks for your help guys. Appreciate it.

Both ways were helpful.

Matt

"Charlie" wrote:

Simpler is:

Option Compare Text

.
.
.

Dim ThisSheet As Worksheet

Do
UserInputName = InputBox("Which sheet?")
If UserInputName = "" Then Exit Sub
For Each ThisSheet In ThisWorkbook.Worksheets
If ThisSheet.Name = UserInputName Then
ThisSheet.Activate
Exit Sub
End If
Next
Loop


"K Dales" wrote:

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



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
Loop in Excel Mike Excel Discussion (Misc queries) 1 January 10th 08 05:35 PM
How to Loop a macro in Excel flecky New Users to Excel 4 June 12th 07 02:10 PM
Excel VBA loop until eof wombatz Excel Programming 5 January 13th 04 11:26 PM
Loop within Excel Formula? Dan[_29_] Excel Programming 3 December 23rd 03 10:26 PM
HELP!!!! Can't stop a loop (NOT an infinite loop) TBA[_2_] Excel Programming 3 December 14th 03 03:33 PM


All times are GMT +1. The time now is 03:10 AM.

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

About Us

"It's about Microsoft Excel"