Solved

VBA: Problem with exiting Select Case statement

Posted on 2008-06-17
12
2,577 Views
Last Modified: 2008-06-17
Hi,

According to MS (http://msdn.microsoft.com/en-us/library/cy37t14y.aspx), I should be able to exit a Select Case statement by using "Exit Select"... but it's not being accepted by the Word 2003 VBA compiler.

Am I missing something, or does this not apply to VBA?

I have a Select Case statement (below) that could go to Case 35, but I have no need for it and would like to exit out of the Select statement right after Case 24.  What's the best way to do this?

Thanks,
Jens
For Each oRow In oTbl.Rows

                            Select Case oRow.Index

                                    Case 2 'Component Name

                                        xlWS.Cells(intXLRow, intComponentNameColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

                                    Case 3 'Component Description

                                        xlWS.Cells(intXLRow, intDefinitionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

                                    Case 4 'Component Version

                                    '...

                                    Case 24 'Coding Strength

                                        xlWS.Cells(intXLRow, intTermCodingStrengthColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

                                    '...

Open in new window

0
Comment
Question by:jgerbitz
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21803605
Hello jgerbitz,

Please post the code in its entirety.

Regards,

Patrick
0
 
LVL 20

Expert Comment

by:Dufo G. Belski
ID: 21803608
Isn't it "End Select", not "Exit Select"?
0
 

Author Comment

by:jgerbitz
ID: 21803665
@Patrick: posted below (I assume you mean just the Select Case statement...).  It looks ugly in the window, hopefully it's more legible once posted.
@asyscokid: End Select is how one normally exits the statement, but you can't use it to 'leave the party' early.

Cheers,
Jens
For Each oRow In oTbl.Rows

    Select Case oRow.Index

            Case 2 'Component Name

                xlWS.Cells(intXLRow, intComponentNameColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 3 'Component Description

                xlWS.Cells(intXLRow, intDefinitionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 4 'Component Version

                xlWS.Cells(intXLRow, intComponentVersionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 6 'Data Element Name

                xlWS.Cells(intXLRow, intDataElementNameColumn).Formula = StrConv(CleansedText(oRow.Cells(2).Range.Text), vbProperCase)

            Case 7 'Data Element Description

                xlWS.Cells(intXLRow, intDefinitionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 8 'Data Element Version

                xlWS.Cells(intXLRow, intDataElementVersionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 9 'Data Element Type

                xlWS.Cells(intXLRow, intDataElementTypeColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 10 'Data Element Length

                xlWS.Cells(intXLRow, intDataElementLengthRuleColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 11 'Information Exchange Format Mask

                xlWS.Cells(intXLRow, intFormatMaskColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 12 'Cardinality

                xlWS.Cells(intXLRow, intCardinalityColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 13 'Obligation

                xlWS.Cells(intXLRow, intObligationColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 14 'Storyboard

                xlWS.Cells(intXLRow, intStoryBoardColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 16 'OID

                xlWS.Cells(intXLRow, intOIDColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 17 'Terminology Version

                xlWS.Cells(intXLRow, intTermVersionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 18 'Extensions

                xlWS.Cells(intXLRow, intTermExtensionsColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 19 'Proprietary Local Codes

                xlWS.Cells(intXLRow, intTermProprietaryCodesColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 20 'Translation or Cross-Mapping

                xlWS.Cells(intXLRow, intTermTranslationColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 21 'Terminology Update Capability

                xlWS.Cells(intXLRow, intTermUpdateCapabilityColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 22 'Data Types

                xlWS.Cells(intXLRow, intTermDataTypesColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 23 'Permissible Values

                xlWS.Cells(intXLRow, intTermPermissibleValuesColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 24 'Coding Strength

                xlWS.Cells(intXLRow, intTermCodingStrengthColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

            Case 26 'Provincial Business Rules

                If CleansedText(oRow.Cells(2).Range.Text) <> "" Then

                    xlWS.Cells(intXLRow, intBusinessRulesColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)

                Else

                    xlWS.Cells(intXLRow, intBusinessRulesColumn).Formula = "None supplied."

                End If

            Case Else

'                                        MsgBox ("Error: Floating free: " & CleansedText(oRow.Cells(1).Range.Text))

    End Select

Next oRow

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21803761
Jens,

So what exactly is it that you need?  The code looks OK to me; perhaps all you need to do is remove
the branch for Case 26, if in fact you do not need anything north of 24...

Patrick
0
 

Author Comment

by:jgerbitz
ID: 21803809
My mistake in the original post Patrick.  I actually don't need anything north of 26.  The code works fine, but it's not super efficient having it cycle through the remaining Cases.  Here it's not such a big deal, but how would someone with "lots" of Cases deal with this situation?

Seems like an exit strategy shouldn't be that hard... I mean, we have them for all the loops.

Jens
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 21803953
How about...


For Each oRow In oTbl.Rows
    If oRow.Index <=26 Then
        Select Case oRow.Index
                Case 2 'Component Name
                    xlWS.Cells(intXLRow, intComponentNameColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 3 'Component Description
                    xlWS.Cells(intXLRow, intDefinitionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 4 'Component Version
                    xlWS.Cells(intXLRow, intComponentVersionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 6 'Data Element Name
                    xlWS.Cells(intXLRow, intDataElementNameColumn).Formula = StrConv(CleansedText(oRow.Cells(2).Range.Text), vbProperCase)
                Case 7 'Data Element Description
                    xlWS.Cells(intXLRow, intDefinitionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 8 'Data Element Version
                    xlWS.Cells(intXLRow, intDataElementVersionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 9 'Data Element Type
                    xlWS.Cells(intXLRow, intDataElementTypeColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 10 'Data Element Length
                    xlWS.Cells(intXLRow, intDataElementLengthRuleColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 11 'Information Exchange Format Mask
                    xlWS.Cells(intXLRow, intFormatMaskColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 12 'Cardinality
                    xlWS.Cells(intXLRow, intCardinalityColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 13 'Obligation
                    xlWS.Cells(intXLRow, intObligationColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 14 'Storyboard
                    xlWS.Cells(intXLRow, intStoryBoardColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 16 'OID
                    xlWS.Cells(intXLRow, intOIDColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 17 'Terminology Version
                    xlWS.Cells(intXLRow, intTermVersionColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 18 'Extensions
                    xlWS.Cells(intXLRow, intTermExtensionsColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 19 'Proprietary Local Codes
                    xlWS.Cells(intXLRow, intTermProprietaryCodesColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 20 'Translation or Cross-Mapping
                    xlWS.Cells(intXLRow, intTermTranslationColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 21 'Terminology Update Capability
                    xlWS.Cells(intXLRow, intTermUpdateCapabilityColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 22 'Data Types
                    xlWS.Cells(intXLRow, intTermDataTypesColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 23 'Permissible Values
                    xlWS.Cells(intXLRow, intTermPermissibleValuesColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 24 'Coding Strength
                    xlWS.Cells(intXLRow, intTermCodingStrengthColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                Case 26 'Provincial Business Rules
                    If CleansedText(oRow.Cells(2).Range.Text) <> "" Then
                        xlWS.Cells(intXLRow, intBusinessRulesColumn).Formula = CleansedText(oRow.Cells(2).Range.Text)
                    Else
                        xlWS.Cells(intXLRow, intBusinessRulesColumn).Formula = "None supplied."
                    End If
                Case Else
'                                        MsgBox ("Error: Floating free: " & CleansedText(oRow.Cells(1).Range.Text))
        End Select
    End If
Next oRow
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21804025
The execution 'exits' the Select structure at the end or after the first Case block where the condition is met.

There is therefore no need for an Exit Select.
0
 

Author Comment

by:jgerbitz
ID: 21804114
@Patrick... that'll do the trick!  Thanks; it's obvious now that I look at it.
@GrahamSkan: Thanks for your input, but I'm still not clear on how else I could have dealt with the issue of multiple 'empty' Cases without the If... Then that Patrick supplied.  I'm likely just not getting this...

Jens
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21804231
I didn't look at the detail, but I'm sure that Patrick will have dealt with your immediate  problem.

I was just concerned because you seemed to have a misunderstanding about the Select structure.
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21807954
Jens,

So, is this solved now?

Regards,

Patrick
0
 

Author Comment

by:jgerbitz
ID: 21807962
Right, yes, it is.  Got so excited about the answer I forgot where I was.  :)
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21808566
Jens,

:)

Regards,

Patrick
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Word / Printing 8 51
excel VBA sending mail 9 41
Excel Formatting test 26 55
Resetting Word when a document is closed 5 11
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Outlook Free & Paid Tools
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now