Using Case

G

GReg

I have a macro that has 14 cases. I am running it with
For i = 1 to 14
Select Case i

This works, but if I add or delete a case I need to
remember to change the For i line. Is there a way to do
something like

For Every Case
Select Case ?

Thanks/
 
H

Helmut Weber

Hi Greg,
I wonder whether there is a need
for a loop at all. If so, "case else"
may help you. Maybe like this:
Dim i As Integer
For i = 1 To 14
Select Case i
Case 1: MsgBox i ' Nonsense of course
Case 2: MsgBox i ' Nonsense of course
Case 13: MsgBox i ' Nonsense of course
Case Else: MsgBox "else" ' or just nothing
End Select
Next
But rather odd indeed.
 
G

Greg Maxey

Helmut,

That is what I have now. See my post "Tech Eval" posted yesterday. I am
trying to find a way to loop through all cases regardless of the number so I
don't have to remember to change the For i = 1 to ?? line.

Thanks
 
P

Peter Hewett

Hi Greg

I can't see what the Case statement does, it looks totally redundant. Why not just
execute the code you want?

' Do what was Case 1
' Do what was Case 2
' Do what was Case 13

Cheers - Peter


As Helmut says, there's no need for a for...next loop at all, just let the
Case Else at the end pick up and stragglers...

For your information, you'll make the select statement faster if you reorder
the case statements so that the most likely one is at the top. Cases are
evaluated one after the other until the case is met (rather like a long list
of If statements). Putting the most likely used one at the top prevents rare
ones from even being checked for.

Yes, it means your nicely in order cases are now out of order, but it'll be
faster!

Pete.

HTH + Cheers - Peter
 
H

Helmut Weber

Hi Greg,
you don't even need a "select case" command.
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
Sub Test444()
Resetsearch
Dim sMsg1 As String ' String Message 1
Dim sRslt As String ' String Result of Inputbox
sMsg1 = "Type in any additional leading character"
With ActiveDocument.Range.Find
.MatchWildcards = True
.Text = "< {1,}*"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
.Text = "(^13)( {1,}*)"
.Replacement.Text = "\1"
.Execute Replace:=wdReplaceAll
.Text = "(^l)( {1,}*)"
.Replacement.Text = "\1"
.Execute Replace:=wdReplaceAll
.Text = "( {1,})(^13)"
.Replacement.Text = "\2"
.Execute Replace:=wdReplaceAll
.Text = "( {1,})(^l)"
.Replacement.Text = "\2"
.Execute Replace:=wdReplaceAll
sRslt = InputBox(sMsg1)
.MatchWildcards = False
.Text = sRslt
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
Resetsearch
End Sub
---
If you feel like diving into this really deeply,
and find the hard coded repetitions not too convincing,
then you need a loop indeed!
Let's say there were hundreds of replacing operations.
Then I would put the search string, the replacement string
and a representative for the boolean "matchwildcards"
in an extra file, like this:
( {1,})(^13)
\1
0 ' ???
I would count the lines in the file,
dimension an array accordingly,
read the lines of file into the array,
and work my way through something (!) like this,
untested, of course.
For i = 1 to upperbound(Myarray)
.Text = Myarray(1)
.Replacement.Text = Myarray(2)
.MatchWildcards = Cbool(Myarray(3))
.Execute Replace:=wdReplaceAll
next
 
H

Helmut Weber

hmm...
in more detailed code, as bugs are too serious,
though I wrote "like this" and "untested":
Sub Makro1()
Dim i As Integer
Dim aStr(27) As String
For i = 1 To 27
aStr(i) = CStr(i)
Next
For i = 1 To 27 - 2 Step 3 ' to get entry i, entry i+1, entry i+2
MsgBox aStr(i) ' .text
MsgBox aStr(i + 1) ' .replacement.text
MsgBox aStr(i + 2) ' .boolean matchwildcards
Next
End Sub
 
G

Greg Maxey

Peter, Pete, Helmut,

Thanks all. I looks like the three of you have shown me that there is more
than one way and better ways to perform repetive similiar tasks. I am
battening down the hatches today in preparation for Hurricane Frances. If I
return to a house still standing I will try to get my head around each of
your suggestions.
 
G

Greg Maxey

Peter,

Do you mean replace each Case with:

Do
.Text = "< {1,}*" 'Was Case 1
.Replacement.Text = "" 'Was Case 1
.Execute Replace:=wdReplaceAll
Exit Do
Loop

You said:

Do what was Case 1
Do what was Case 2 ...

Do'nt DOs require LOOPs? I know that I only want to DO each of the 14 bits
one time.
 
G

Greg Maxey

Peter,

Thanks for the hopes. Yours and the hopes and prayers of many others
overcame the awesome power of mother nature and spared many from a battering
much worse than most received. I returned home today after evacuating to
Tampa FL, and found relative minor damage. Roof shingles missing, trees
blown over, a little water blown through cracks but nothing major. It has
been a harrowing four days as the reaches of Frances ranged over 500 miles
and she is still grinding towards another coastal area in the pan handle of
Florida.

I have visited your fair city of Darwin twice. If memory serves me
correctly a Willy Nilly named Tracy gave her a battering on Chrismas Day in
76 I think. These are not pleasant things. I think with your tip and the
others I have worked out the subject macro.

Thanks again for all the help and assistance you offer here.
 
P

Peter Hewett

Hi Greg

I'm very pleased to hear that you came through it all Ok. It amazing where water gets
blown. Thanks to instantaneous global TV I've seen second hand what you've been going
through. So much for "no such thing a Global Warming" huh!

I'm actually a Kiwi, living in (windy) Wellington! We had a storm a couple of weeks back
- but it was nothing compared to what hits your area.

Good luck + Cheers - Peter


Peter,

Thanks for the hopes. Yours and the hopes and prayers of many others
overcame the awesome power of mother nature and spared many from a battering
much worse than most received. I returned home today after evacuating to
Tampa FL, and found relative minor damage. Roof shingles missing, trees
blown over, a little water blown through cracks but nothing major. It has
been a harrowing four days as the reaches of Frances ranged over 500 miles
and she is still grinding towards another coastal area in the pan handle of
Florida.

I have visited your fair city of Darwin twice. If memory serves me
correctly a Willy Nilly named Tracy gave her a battering on Chrismas Day in
76 I think. These are not pleasant things. I think with your tip and the
others I have worked out the subject macro.

Thanks again for all the help and assistance you offer here.

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top