jeudi 27 octobre 2016

EXCEL VBA user defined function - else without if

The attached code is run on VBA, but I do not understand why there is an error says else without if or if without end if. I am pretty sure that I have matched every end if with if statement.

Sub teee() is just for testing the decimalize function. It would be greatly check the code and tell me what is wrong with my code... I am almost close to complete a project if I can troubleshoot this function.

Sub teee()

sss = "-1-21+"

MsgBox (decimalize(sss))


End Sub


Function decimalize(s As Variant) As Long

Dim checkers As Variant
Dim ab As Long
Dim leftnum As Long
Dim rightnum As Long
Dim poneg As Integer


checkers = s
ab = 0
leftnum = 0
rigntnum = 0
poneg = 0

'Positive payup or negative payup

If Left(checkers, 1) = "-" Then
poneg = 1
lencheckers = Len(checkers)
checkers = Mid(checkers, 2, lencheckers - 1)

Else: poneg = 0
End If


startp = InStr(checkers, "-")


If startp = 2 Then leftnum = Left(checkers, 1)
ElseIf startp = 3 Then leftnum = Left(checkers, 2)
ElseIf startp = 4 Then leftnum = Left(checkers, 3)
End If



rightnum = Mid(checkers, startp + 1, 2)


If InStr(checkers, "+") > 0 Then
ab = 0.5
ElseIf InStr(checkers, "1/4") > 0 Then
ab = 0.25
ElseIf InStr(checkers, "1/8") > 0 Then
ab = 0.125
End If

rightnum = rightnum + ab


If poneg = 0 Then
decimalize = rightnum + leftnum * 32
ElseIf poneg = 1 Then
decimalize = (rightnum + leftnum * 32) * -1
End If


End Function

Many Thanks in advance

Aucun commentaire:

Enregistrer un commentaire