案例陈述不起作用 - VBA

时间:2017-07-07 08:53:19

标签: excel vba excel-vba excel-2013 case-statement

在我的项目中,我有一个具有独特佣金计算的客户列表(例如,Client_1的佣金率为0.1%,而Client_2的佣金率为0.25%)。

在我的VBA代码中,我做了一些特殊情况,因client_id而异。我接受这个代码的问题是,即使我输入一个特殊的ID,它也会跳过这个案例(例如,如果klienta_nr = 100,它会跳过Case klienta_nr = 100并返回Case Else)。我该如何解决这个问题?

Private Sub CommandButton1_Click()

Dim klienta_nr As Long
Dim ISIN As String
Dim Cena As Double
Dim Skaits As Double
Dim Komisija As Double
Dim vk As String

Set kSheet = ThisWorkbook.Sheets("komisijas")


klienta_nr = Range("B2").Value
ISIN = Range("E2").Value
Cena = Range("H2").Value
Skaits = Range("I2").Value
vk = Range("B2").Value

Select Case klienta_nr

Case klienta_nr = 100

            If klienta_nr = 100 And (Left(ISIN, 2) = "DE" Or Left(ISIN, 2) = "FR" Or Left(ISIN, 2) = "NL" Or Left(ISIN, 2) = "IT" Or Left(ISIN, 2) = "IE") Then
                Komisija = (Cena * Skaits) * 0.01
                Range("K2").Value = Komisija
                End If
            If klienta_nr = 100 And Komisija <= 30 Then
                Range("K2").Value = 30
                End If

            'Case where klient is special, but ISIN doesn't apply
            If klienta_nr = 100 And (Left(ISIN, 2) <> "DE" Or Left(ISIN, 2) <> "FR" Or Left(ISIN, 2) <> "NL" Or Left(ISIN, 2) <> "IT" Or Left(ISIN, 2) <> "IE") Then
                Komisija = (Cena * Skaits) * 0.003
                If Komisija >= 40 Then
                    Range("K2").Value = 40
                    End If
            End If


 Case klienta_nr = 105

            If klienta_nr = 105 And (Left(ISIN, 2) = "DE" Or Left(ISIN, 2) = "FR" Or Left(ISIN, 2) = "NL" Or Left(ISIN, 2) = "IT" Or Left(ISIN, 2) = "IE") Then
                Komisija = (Cena * Skaits) * 0.01
                Range("K2").Value = Komisija
                End If
            'Set 30 EUR Min
            If klienta_nr = 105 And Komisija <= 30 Then
                Range("K2").Value = 30
                End If

    'End If


 Case klienta_nr = 110

            If klienta_nr = 110 And (Left(ISIN, 2) = "NO" Or Left(ISIN, 2) = "SE" Or Left(ISIN, 2) = "DK" Or Left(ISIN, 2) = "FI" Or Left(ISIN, 2) = "IS" Or Left(ISIN, 2) = "LT" Or Left(ISIN, 2) = "EE" Or Left(ISIN, 2) = "DE" Or Left(ISIN, 2) = "FR" Or Left(ISIN, 2) = "NL" Or Left(ISIN, 2) = "IT" Or Left(ISIN, 2) = "IE" Or Left(ISIN, 2) = "AT" Or Left(ISIN, 2) = "BE" Or Left(ISIN, 2) = "ES" Or Left(ISIN, 2) = "PT") Then
                Komisija = (Cena * Skaits) * 0.002
                Range("K2").Value = Komisija
                End If

            If klienta_nr = 110 And (Left(ISIN, 2) = "US") Then
                Komisija = (Cena * Skaits) * 0.002
                End If

            If klienta_nr = 110 And (Left(ISIN, 2) = "UK") Then
                Komisija = (Cena * Skaits) * 0.002
                Range("K2").Value = Komisija
                End If

            If klienta_nr = 110 And (Left(ISIN, 2) = "CH") Then
                Komisija = (Cena * Skaits) * 0.002
                Range("K2").Value = Komisija
                End If
            'Set 20 [valūte] MIN
            If klienta_nr = 110 And Komisija <= 20 Then
                Range("K2").Value = 20
                End If


Case klienta_nr = 115

            If klienta_nr = 115 And (Left(ISIN, 2) = "NO" Or Left(ISIN, 2) = "SE" Or Left(ISIN, 2) = "DK" Or Left(ISIN, 2) = "FI" Or Left(ISIN, 2) = "IS" Or Left(ISIN, 2) = "LT" Or Left(ISIN, 2) = "EE" Or Left(ISIN, 2) = "DE" Or Left(ISIN, 2) = "FR" Or Left(ISIN, 2) = "NL" Or Left(ISIN, 2) = "IT" Or Left(ISIN, 2) = "IE" Or Left(ISIN, 2) = "AT" Or Left(ISIN, 2) = "BE" Or Left(ISIN, 2) = "ES" Or Left(ISIN, 2) = "PT") Then
                Komisija = (Cena * Skaits) * 0.002
                Range("K2").Value = Komisija
                End If

            If klienta_nr = 115 And (Left(ISIN, 2) = "US") Then
                Komisija = (Cena * Skaits) * 0.002
                End If

            If klienta_nr = 115 And (Left(ISIN, 2) = "UK") Then
                Komisija = (Cena * Skaits) * 0.002
                Range("K2").Value = Komisija
                End If

            If klienta_nr = 115 And (Left(ISIN, 2) = "CH") Then
                Komisija = (Cena * Skaits) * 0.002
                Range("K2").Value = Komisija
                End If
            'Set 20 [valūte] MIN
            If klienta_nr = 115 And Komisija <= 20 Then
                Range("K2").Value = 20
                End If


 Case klienta_nr = 120

            If klienta_nr = 120 And (Left(ISIN, 2) = "US") Then
                Komisija = (Cena * Skaits) * 0.0027
                Range("K2").Value = Komisija
                End If
            'Set 40 USD MIN
            If klienta_nr = 120 And Komisija <= 40 Then
                Range("K2").Value = 40
                End If



'Non-special klient cases
Case Else
    If Not Application.Match(klienta_nr, kSheet.Range("A2:A100")) Then

         If Right(vk, 1) = 1 Or Right(vk, 1) = 8 Then
            Komisija = (Cena * Skaits) * 0.003
            Range("K2").Value = Komisija
            End If

        If Right(vk, 1) = 7 Then
            Komisija = (Cena * Skaits) * 0.01
            Range("K2").Value = Komisija
            End If
        'Komisija MAX is 40, so anything >=40 equals 40
        If Komisija >= 40 Then
            Range("K2").Value = 40
            End If
    End If
   End Select               
  End Sub

2 个答案:

答案 0 :(得分:1)

Select Case只需要

中的标准
Select Case klienta_nr

而不是案例部分如下:

更改

Case klienta_nr = 105

Case 105

答案 1 :(得分:0)

一旦你有Select Case klienta_nr,然后第一个案例是Case 100,你就不会在If klienta_nr = 100下面重复,因为你已经在Case中检查过了}。

将您的Select Case结构更改为:

Select Case klienta_nr

Case 100

    If (Left(ISIN, 2) = "DE" Or Left(ISIN, 2) = "FR" Or Left(ISIN, 2) = "NL" Or Left(ISIN, 2) = "IT" Or Left(ISIN, 2) = "IE") Then
        Komisija = (Cena * Skaits) * 0.01
        Range("K2").Value = Komisija
    End If
    If Komisija <= 30 Then
        Range("K2").Value = 30
    End If

    'Case where klient is special, but ISIN doesn't apply
    If (Left(ISIN, 2) <> "DE" Or Left(ISIN, 2) <> "FR" Or Left(ISIN, 2) <> "NL" Or Left(ISIN, 2) <> "IT" Or Left(ISIN, 2) <> "IE") Then
        Komisija = (Cena * Skaits) * 0.003
        If Komisija >= 40 Then
            Range("K2").Value = 40
        End If
    End If

Case 105

    If (Left(ISIN, 2) = "DE" Or Left(ISIN, 2) = "FR" Or Left(ISIN, 2) = "NL" Or Left(ISIN, 2) = "IT" Or Left(ISIN, 2) = "IE") Then
        Komisija = (Cena * Skaits) * 0.01
        Range("K2").Value = Komisija
    End If
    'Set 30 EUR Min
    If Komisija <= 30 Then
        Range("K2").Value = 30
    End If

' add here your other cases...


End Select