Link to home
Start Free TrialLog in
Avatar of jgerbitz
jgerbitzFlag for Canada

asked on

VBA: Problem with exiting Select Case statement

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

Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Hello jgerbitz,

Please post the code in its entirety.

Regards,

Patrick
Avatar of A Syscokid
A Syscokid

Isn't it "End Select", not "Exit Select"?
Avatar of jgerbitz

ASKER

@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

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
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
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
@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
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.
Jens,

So, is this solved now?

Regards,

Patrick
Right, yes, it is.  Got so excited about the answer I forgot where I was.  :)
Jens,

:)

Regards,

Patrick