Hallo zusammen,
ich habe eine kleine Funktion geschrieben, um Dezimalzahlen in Binärzahlen umzuwandeln.
Eigentlich funktioniert auch alles ganz gut.... Solange ich keine 0 eingebe.
Dann wird die Funktion gar nicht aufgerufen.
Woran kann das liegen?
Für Eure Mühe im voraus vielen Dank!
Function DezimalBin(DezZahl As LongLong, BinDigits As Integer) As String
Dim TempRest As LongLong
Dim BinString As String
Dim MaxForThisPos As LongLong
TempRest = DezZahl
If BinDigits <= 0 Then
Answer = MsgBox("Value must be greate than zero!", vbOK + vbCritical, "VBA-Tutorial")
Exit Function
End If
If BinDigits > 32 Then
Answer = MsgBox("Only values up to 32 are supported!", vbOK + vbCritical, "VBA-Tutorial")
Exit Function
End If
For i = (BinDigits - 1) To 0 Step -1
MaxForThisPos = (2 ^ i)
If TempRest >= MaxForThisPos Then
BinString = BinString + "1"
TempRest = TempRest - MaxForThisPos
Else
BinString = BinString + "0"
End If
Next
If TempRest > 0 Then
Answer = MsgBox("The value given cannot be shown with the given number of digits! Please provide a fitting value", vbOK + vbCritical, "VBA-Tutorial")
Exit Function
End If
DezimalBin = BinString
End Function