案例陈述不起作用 - VBA
Case Statement Not Working - VBA
在我的项目中,我有一个具有独特佣金计算的客户列表(例如,Client_1 的佣金率为 0.1%,而 Client_2 的佣金率为 0.25%)。
在我的 VBA 代码中,我做了特殊情况,因 client_id 而异。我使用这段代码遇到的问题是,即使我输入了一个特殊的 ID,它也会跳过大小写(例如,如果 klienta_nr = 100,它会跳过大小写 klienta_nr = 100 和returns 其他情况)。我该如何解决这个问题?
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
Select Case 只需要
中的条件
Select Case klienta_nr
并且不在下面的 Case 部分:
改变
Case klienta_nr = 105
到
Case 105
一旦你有了 Select Case klienta_nr
,然后第一个案例是 Case 100
,你就不会在 If klienta_nr = 100
下面重复,因为你已经在你的 [=14= 中检查过了].
将您的 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
在我的项目中,我有一个具有独特佣金计算的客户列表(例如,Client_1 的佣金率为 0.1%,而 Client_2 的佣金率为 0.25%)。
在我的 VBA 代码中,我做了特殊情况,因 client_id 而异。我使用这段代码遇到的问题是,即使我输入了一个特殊的 ID,它也会跳过大小写(例如,如果 klienta_nr = 100,它会跳过大小写 klienta_nr = 100 和returns 其他情况)。我该如何解决这个问题?
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
Select Case 只需要
中的条件Select Case klienta_nr
并且不在下面的 Case 部分:
改变
Case klienta_nr = 105
到
Case 105
一旦你有了 Select Case klienta_nr
,然后第一个案例是 Case 100
,你就不会在 If klienta_nr = 100
下面重复,因为你已经在你的 [=14= 中检查过了].
将您的 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