Clever-Excel-Forum

Normale Version: VBA - TextBox mit Dezimalzahlen addieren
Du siehst gerade eine vereinfachte Darstellung unserer Inhalte. Normale Ansicht mit richtiger Formatierung.
Hallo Zusammen,

leider konnte mir die Suche im Internet nicht weiterhelfen.

Mein Problem ist es, dass ich es nicht schaffe die TextBoxen in meiner Userform, die auch Dezimalzahlen enthalten können, zu addieren.

Hier ist mein Code:
Code:
Private Sub ProzCalc()
   On Error Resume Next
   
   Dim i As Byte
   Dim a, b As Integer
   Dim WDays, DDays, FG As Double
       
   a = 2
   For i = 1 To 12
       a = a + 1
       If Controls("TextBox" & a).Value = True Then
           Controls("TextBox" & a).Value = Format(Controls("TextBox" & a), "0.00")
           WDays = WDays + Controls("TextBox" & a).Value
       End If
       If Controls("CheckBox" & i).Value = True Then
           DDays = DDays + Controls("TextBox" & a).Value
       End If
   Next i
   
   FG = ((DDays / WDays) * 100)
   
   Label57.Caption = FG
   
End Sub

Wenn ich jetzt in TextBox 3 die Zahl "4" stehen habe und in TextBox4 und TextBox5 die Zahlen "0.25", dann hat WDays diesen Wert 4,025,025

Ich hoffe jemand kann mir helfen, damit ich die Werte der TextBoxen so addieren kann, dass sie richtig ausgegeben werden. 

Danke Vorarb
Konnte es zum Glück doch selber lösen. :)

Ich musste einfach nur CDbl(WDays) einfügen und schon hat es geklappt.

Hier der Code falls jemand mal vor dem selbem Problem steht. ;)

Code:
Private Sub ProzCalc()
   On Error Resume Next
   
   Dim i As Byte
   Dim a, b As Integer
   Dim WDays, DDays, FG As Double
       
   a = 2
   For i = 1 To 12
       a = a + 1
       If Controls("TextBox" & a).Value = True Then
           Controls("TextBox" & a).Value = Format(Controls("TextBox" & a), "0.00")
           WDays = CDbl(WDays) + CDbl(Controls("TextBox" & a).Value)
       End If
       If Controls("CheckBox" & i).Value = True Then
           DDays = CDbl(DDays) + CDbl(Controls("TextBox" & a).Value)
       End If
   Next i
   
   FG = ((DDays / WDays) * 100)
   
   Label57.Caption = FG
   
End Sub

1Matthias

Moin!
Die Textboxen geben nur Text zurück. In deiner Zeile verkettest du die also nur. Wandle den Inhalt in das benötigte Format um.
Bspw. so:

Code:
dim DDays as double
DDays = DDays + cdbl(Controls("TextBox" & a))

Ggf. vorher noch prüfen, ob der Inhalt auch eine Zahl ist (isnumeric) sonst läufs du ggf. in einen Fehler.
VG
Auch Hallo,

(10.08.2018, 08:45)Mr.Invisible schrieb: [ -> ]Hier der Code

Code:
Private Sub ProzCalc()
   Dim a, b As Integer
   Dim WDays, DDays, FG As Double
       

bei einer solchen Dekleration hat nur die Variable b den Typ Integer, a ist Variant. Dasselbe in der nächsten Zeile, hier hat nur FG den Typ Double, WDays und DDays sind Variant. Mache es so

Code:
Dim a As Integer, b As Integer
    Dim WDays As Double, DDays As Double, FG As Double