Select Case Statement不遵循整数变量值

时间:2014-05-24 13:00:50

标签: excel vba excel-vba excel-2010

这是我的代码的摘录:

Dim x as Integer
x = 0
Select Case x


Case x = 0 'LINE 4
find_Element = fin_Array(x)

    If Trim(find_Element) <> "" Then
        For k = 3 To last_WS
            With Sheets(k).Range("1:200")
                sh_Name = Worksheets(k).Name
                Set pointer_to_Element = .Find(find_Element, _
                    .Cells(.Cells.Count), xlValues, xlWhole, xlByRows, _
                    xlNext, False)
                pointer_Int_Row = CInt(pointer_to_Element.Row)
                'pointer_Int_Col = CInt(pointer_to_Element.Column)

                Worksheets(k).Cells(pointer_Int_Row, lock_Col).Copy
                Worksheets("EXTRACTIONS").Range("B8").Offset(, q).PasteSpecial xlPasteValues
                Worksheets("EXTRACTIONS").Range("B7").Offset(, q).Value = sh_Name
                q = q + 1
            End With
        Next k
    End If
q = 1

Case x = 1
find_Element = fin_Array(x)

    If Trim(find_Element) <> "" Then
        For k = 3 To last_WS
            With Sheets(k).Range("1:200")
                sh_Name = Worksheets(k).Name
                Set pointer_to_Element = .Find(find_Element, _
                    .Cells(.Cells.Count), xlValues, xlWhole, xlByRows, _
                    xlNext, False)
                pointer_Int_Row = CInt(pointer_to_Element.Row)
                pointer_Int_Col = CInt(pointer_to_Element.Column)

                Worksheets(k).Cells(pointer_Int_Row, pointer_Int_Col).Offset(, 2) _
                .Copy
                Worksheets("EXTRACTIONS").Range("B8").Offset(, q).PasteSpecial xlPasteValues
                Worksheets("EXTRACTIONS").Range("B7").Offset(, q).Value = sh_Name
                q = q + 1
            End With
        Next k
    End If
q = 1

我的问题是我在我的代码中明确设置x=0 VBA当它转到第4行时它会跳转并执行Case x = 1 !!!!

当我在代码中明确设置x=0时,这怎么可能?

2 个答案:

答案 0 :(得分:3)

根据http://msdn.microsoft.com/en-us/library/cy37t14y.aspx,它的意思是:

Dim x As Integer
x = 0
Select Case x
    Case 0
        'do some stuff
    Case 1
        'do other stuff
End Select

扩展PeeKay所说的话:

如果x = 0等于x并且评估为0True等同于1,同样x = 1False将进行0测试评估为1(或Dim x As Integer x = 0 Select Case x Case 1 'do some stuff Case 0 'do other stuff End Select ),因为x不等于{{1}},因此您的代码最终看起来像:

{{1}}

答案 1 :(得分:2)

解释安迪的评论: 当您明确设置x = 0时,Case X= 0将始终返回true。

只需使用:

Select Case X
    Case 0
    ' do stuff
    Case 1
    ' do other stuff
    Case Else
    '...
End Select