Solved

VBA: Problem with exiting Select Case statement

Posted on 2008-06-17
12
2,927 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

840 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