VBA cleaning/compressing multiple if then statements

Skiff

Board Regular
Joined
May 30, 2016
Messages
58
Office Version
  1. 2019
Platform
  1. Windows
Hi. I've come up with VBA code that works fine, but is long, and probably can be compressed into something shorter or more tidy.
Is there a way to substitute these multiple statements with something like arrays or anything else?

I'm quite new to VBA (only programming I'm doing) - tried few tutorials about arrays, but for now it is beyond my understanding.

Thanks in advance.

My code is:
Code:
      If proponowano = "TAK" Then
         If kpu = "a" Then stawka_u = 70
         If kpu = "b" Then stawka_u = 75
         If kpu = "c" Then stawka_u = 90
         If kpu = "m" Then stawka_u = 140
         If kpu = "d" Then stawka_u = 130
         If kpu = "van" Then stawka_u = 130
         If kpu = "p" Then stawka_u = 260
         If kpu = "e" Then stawka_u = 255
         If kpu = "suv" Then stawka_u = 500
         If kpu = "f" Then stawka_u = 500
         If kpu = "g" Then stawka_u = 649
         If kpu = "h" Then stawka_u = 800
         If kpu = "dsuv" Then stawka_u = 0
         If kpu = "inna" Then stawka_u = 0
         
         If kpw = "a" Then stawka_w = 70
         If kpw = "b" Then stawka_w = 75
         If kpw = "c" Then stawka_w = 90
         If kpw = "m" Then stawka_w = 140
         If kpw = "d" Then stawka_w = 130
         If kpw = "van" Then stawka_w = 130
         If kpw = "p" Then stawka_w = 260
         If kpw = "e" Then stawka_w = 255
         If kpw = "suv" Then stawka_w = 500
         If kpw = "f" Then stawka_w = 500
         If kpw = "g" Then stawka_w = 649
         If kpw = "h" Then stawka_w = 800
         If kpw = "dsuv" Then stawka_w = 0
         If kpw = "inna" Then stawka_w = 0
      End If
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi,
one way maybe:

Code:
Option Base 1
Sub test()
    Dim arr As Variant, m As Variant
    Dim check As String
    
    kpu = "suv"
    
    check = kpu
    
    arr = Array("a", "b", "c", "m", "d", "van", "p", "e", "suv", "f", "g", "h", "dsuv", "inna")
    m = Application.Match(check, arr, False)
    
    If Not IsError(m) Then
        stawka_u = Choose(CInt(m), 70, 75, 90, 140, 130, 130, 260, 255, 500, 500, 649, 800, 0, 0)
        MsgBox stawka_u
    Else
' no match
    End If
    
End Sub


Dave
 
Last edited:
Upvote 0
Solution
Thanks. I'll check it out, and look on the web what exacly we are doing here.
 
Upvote 0

Forum statistics

Threads
1,226,730
Messages
6,192,708
Members
453,748
Latest member
akhtarf3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top