Effektivisere vba-kode
HejJeg har følgende vba-kode som jeg anvender til at periodisere nogle omkostninger.
Problemet er at den er for tung og langsom.
Mit spørgsmål er om der er nogen der kan pege på nogle løsninger, der kan forbedre hastigheden markant?
Den er ikke særlig kompliceret men lang pga. en masse if-else
----
Option Base 1
Function PLversion1(TmStart, TmSlut, CF, RGdato, Uddata)
Dim TerminAct As Integer, Antal As Integer, Dage As Integer
Dim PL As Double
Dim x As Integer, z As Integer
If IsError(CF) Then
Exit Function
End If
TerminAct = TmSlut - TmStart
Dim c(13, 1) As Double
c(1, 1) = Application.WorksheetFunction.VLookup(TmStart, Range("DaysRG"), 2, False)
For z = 2 To 13
c(z, 1) = Application.WorksheetFunction.VLookup(c(z - 1, 1), Range("DaysRG2"), 2, False)
Next
For z = 1 To 13
If c(z, 1) < TmSlut Then
Antal = Antal + 1
End If
Next
If Antal = 0 Then
If RGdato = c(1, 1) Then
Dage = TmSlut - TmStart
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 1 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = TmSlut - c(1, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 2 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = TmSlut - c(2, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 3 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = TmSlut - c(3, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 4 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = TmSlut - c(4, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 5 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = c(5, 1) - c(4, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(6, 1) Then
Dage = TmSlut - c(5, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 6 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = c(5, 1) - c(4, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(6, 1) Then
Dage = c(6, 1) - c(5, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(7, 1) Then
Dage = TmSlut - c(6, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 7 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = c(5, 1) - c(4, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(6, 1) Then
Dage = c(6, 1) - c(5, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(7, 1) Then
Dage = c(7, 1) - c(6, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(8, 1) Then
Dage = TmSlut - c(7, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 8 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = c(5, 1) - c(4, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(6, 1) Then
Dage = c(6, 1) - c(5, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(7, 1) Then
Dage = c(7, 1) - c(6, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(8, 1) Then
Dage = c(8, 1) - c(7, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(9, 1) Then
Dage = TmSlut - c(8, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 9 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = c(5, 1) - c(4, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(6, 1) Then
Dage = c(6, 1) - c(5, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(7, 1) Then
Dage = c(7, 1) - c(6, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(8, 1) Then
Dage = c(8, 1) - c(7, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(9, 1) Then
Dage = c(9, 1) - c(8, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(10, 1) Then
Dage = TmSlut - c(9, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 10 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = c(5, 1) - c(4, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(6, 1) Then
Dage = c(6, 1) - c(5, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(7, 1) Then
Dage = c(7, 1) - c(6, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(8, 1) Then
Dage = c(8, 1) - c(7, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(9, 1) Then
Dage = c(9, 1) - c(8, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(10, 1) Then
Dage = c(10, 1) - c(9, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(11, 1) Then
Dage = TmSlut - c(10, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 11 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = c(5, 1) - c(4, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(6, 1) Then
Dage = c(6, 1) - c(5, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(7, 1) Then
Dage = c(7, 1) - c(6, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(8, 1) Then
Dage = c(8, 1) - c(7, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(9, 1) Then
Dage = c(9, 1) - c(8, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(10, 1) Then
Dage = c(10, 1) - c(9, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(11, 1) Then
Dage = c(11, 1) - c(10, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(12, 1) Then
Dage = TmSlut - c(11, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
ElseIf Antal = 12 Then
If RGdato = c(1, 1) Then
Dage = RGdato - TmStart
PL = Dage / TerminAct * CF
ElseIf RGdato = c(2, 1) Then
Dage = c(2, 1) - c(1, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(3, 1) Then
Dage = c(3, 1) - c(2, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(4, 1) Then
Dage = c(4, 1) - c(3, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(5, 1) Then
Dage = c(5, 1) - c(4, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(6, 1) Then
Dage = c(6, 1) - c(5, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(7, 1) Then
Dage = c(7, 1) - c(6, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(8, 1) Then
Dage = c(8, 1) - c(7, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(9, 1) Then
Dage = c(9, 1) - c(8, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(10, 1) Then
Dage = c(10, 1) - c(9, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(11, 1) Then
Dage = c(11, 1) - c(10, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(12, 1) Then
Dage = c(12, 1) - c(11, 1)
PL = Dage / TerminAct * CF
ElseIf RGdato = c(13, 1) Then
Dage = TmSlut - c(12, 1)
PL = Dage / TerminAct * CF
Else
PL = 0
End If
End If
Select Case Uddata
Case Is = "PL"
PLversion1 = PL
Case Is = "DAYS"
PLversion1 = Dage
End Select
End Function