• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3879
  • Last Modified:

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

0
jgerbitz
Asked:
jgerbitz
  • 5
  • 4
  • 2
  • +1
1 Solution
 
Patrick MatthewsCommented:
Hello jgerbitz,

Please post the code in its entirety.

Regards,

Patrick
0
 
Dufo G. BelskiRetired bureaucrat/desktop supportCommented:
Isn't it "End Select", not "Exit Select"?
0
 
jgerbitzAuthor Commented:
@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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
Patrick MatthewsCommented:
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
 
jgerbitzAuthor Commented:
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
 
Patrick MatthewsCommented:
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
 
GrahamSkanCommented:
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
 
jgerbitzAuthor Commented:
@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
 
GrahamSkanCommented:
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
 
Patrick MatthewsCommented:
Jens,

So, is this solved now?

Regards,

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

:)

Regards,

Patrick
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now