Solved

VBA: Problem with exiting Select Case statement

Posted on 2008-06-17
12
2,683 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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…

919 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

15 Experts available now in Live!

Get 1:1 Help Now