简体   繁体   中英

VBA select case ignoring single digit (LEN) cells

I have a select case which is meant to look and find set exact cases. What is baffling me is, its executing on cases that have 2 digits but not on single digit cells. Plainly put, Case "B" and "C" are executing in the else statement instead of their respective cases, any pointers will be appreciated...

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
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

That's getting the first 2 characters of Cells(i, 11) which is never going to evaluate to "E" so I'm not convinced that execution is occurring anywhere inside that nested Select Case statement

Try modifying your select to:

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

and the second select to

Trim(contTypeRange)

It looks like you might have extra spaces. If that doesn't work, then check the contTypeRange value for length to see if there are some strange characters in it...

Are you sure that "B" and "C" are executed in the else? Can you write in the else

debug.print contTypeRange debug.print len(contTypeRange)

just to see what is it returning?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM