VBA选择案例忽略单个数字(LEN)单元格

时间:2016-01-21 16:16:06

标签: excel vba excel-vba

我有一个选择案例,用于查找和查找设置的确切案例。令我困惑的是,它在具有2位但不在单位数字单元格上的情况下执行。简单地说,Case" B"和" C"正在执行else语句而不是各自的情况,任何指针将不胜感激...

Public Function cartMaxCalc(i As Long)

Dim contQty
Set contQty = Cells(i, 6)
Dim contTypeRange
Set contTypeRange = Cells(i, 5)
Dim cartMx As Long
'determine category by first digit of cell
    Select Case UCase(Left(Cells(i, 11).Value, 2))
        Case "E"
            Select Case contTypeRange

                Case "B", "J3", "B0" ' "B" is not recognized
                    cartMx = 4 * contQty
                Case "C", "C0", "J2", "B2" ' "C" is not recognized
                    cartMx = 8 * contQty
                Case "C2", "J1", "j4"
                    cartMx = 16 * contQty
                Case "D1"
                    cartMx = 24 * contQty
                Case "XX", "ZZ"
                    cartMx = 0
                Case Else '"B" and "C" execute here
                    cartMx = contQty
                End Select
 Case "G", "P"
            Select Case contTypeRange
                Case "B", "J3", "B0", "D1"
                    'do nothing
                Case "C", "C0", "J2"
                    cartMx = 6 * contQty
                Case "C2", "J1"
                    cartMx = 12 * contQty
                Case "XX", "ZZ"
                    cartMx = 0
                Case Else
                    cartMx = contQty
                End Select

        Case "A", "A3", "B", "B", "C", "C3", "D", "D3", "D4"
            Select Case contTypeRange
                Case "C", "C0", "J2", "B2"
                    cartMx = 2 * contQty
                Case "C2", "J1"
                    cartMx = 4 * contQty
                Case "D1"
                    cartMx = 6 * contQty
                Case Else
                    cartMx = contQty
                End Select



        Case "T", "F", "R", "L"
            Select Case contTypeRange
                Case "B", "J3", "B0"
                    cartMx = 2 * contQty
                Case "C", "C0", "J2", "B2"
                    cartMx = 4 * contQty
                Case "C2", "J1"
                    cartMx = 8 * contQty
                Case "D1"
                    cartMx = 12 * contQty
                Case "XX", "ZZ"
                    cartMx = 0
                Case Else
                    cartMx = contQty
                End Select
        Case Else
            cartMx = 1 * contQty
    End Select



        cartMaxCalc = cartMx

 End Function

3 个答案:

答案 0 :(得分:2)

Select Case UCase(Left(Cells(i, 11).Value, 2)) '// <~~ Get 2 characters from left
    Case "E" '// <~~ Only 1 character long - will never be equal
        Select Case contTypeRange '// <~~ this would never be reached

这是Cells(i, 11)的第一个 2 字符,它永远不会评估为"E"所以我不相信在嵌套{{}内的任何地方都执行了执行1}}陈述

答案 1 :(得分:1)

尝试将您的选择修改为:

Trim(UCase(Left(Cells(i, 11).Value, 2))) 

,第二个选择

Trim(contTypeRange)

看起来你可能有额外的空间。如果这不起作用,请检查contTypeRange值的长度,看看其中是否有一些奇怪的字符......

答案 2 :(得分:1)

你确定&#34; B&#34;和&#34; C&#34;在别的地方执行?你能写下别的吗

debug.print contTypeRange debug.print len(contTypeRange)

只是为了看看它又回来了什么?