WCM Forum

WCM Forum (http://www.wcm.at/forum/index.php)
-   Programmierung (http://www.wcm.at/forum/forumdisplay.php?f=17)
-   -   Frage Funktion textzahl (http://www.wcm.at/forum/showthread.php?t=165098)

coolbininet 04.05.2005 13:10

Frage Funktion textzahl
 
Hallo Leute!

Möchte eine Funktion schreiben die mir aus einem String zB.: "23+123+83+79" die Summe der einzelnen Zahlen ausgibt.

Hänge aber bereits schon hier:

Public Function textzahl(text As String)
Dim d, pos, i As Long
Do While (Len(text) > 0)
pos = InStr(text, "+")
d = Val(Left(text, pos - 1))
text = Mid(text, pos + 1)
Debug.Print (d)
Loop
End Function

Fehlermeldung von VBA:

Unzulässiger Prozeduraufruf oder ungültiges Argument (Fehler 5)

Was ist da falsch und muss besser oder anders gemacht werden?

Coolbininet

T.dot 04.05.2005 13:28

ich habs nicht gestetet, vermute aber mal folgendes:

pos = InStr(text, "+")
d = Val(Left(text, pos - 1))
text = Mid(text, pos + 1)


Wenn du beim letzten durchlauf bist, findest du kein "+" mehr, dh. pos wird 0, wenn du dann bei left(text,pos-1) als Startwert -1 hast (da ja pos=0), kann er das nicht verarbeiten.

Wenns das nicht ist könntest ja mal ausfindig machen, in welcher Zeile der Fehler ist.

mfg Thomas

coolbininet 04.05.2005 13:58

Lösung!
 
Habe jetzt eine

Public Function textzahl(text As String)
Dim d, da, pos, i, a, ausgabe As Long
Do
pos = InStr(text, "+")
If (pos < 1) Then
da = text
Exit Do
Else
d = Val(Left(text, pos - 1)) + d
text = Mid(text, pos + 1)
Exit Do
End If
Loop Until (Len(text) = 0)
ausgabe = d + da
Debug.Print (ausgabe)
End Function

Ist umständlich, geht's einfacher?

Grüsse Coolbininet

T.dot 04.05.2005 14:17

Du könntest deinen Text in ein Array splitten mit split(Stringzumsplitten,Teil) - also split("23+123+83+79","+") und machst dann ne schleife durchs Array und zählst alle Werte zusammen.

Ansonsten könnt ma deinen Code noch anders optimieren:

Do
pos = InStr(text, "+")
If (pos < 1) Then
d = d + text
text = ""
Else
d = Val(Left(text, pos - 1)) + d
text = Mid(text, pos + 1)
End If
Loop Until (Len(text) = 0)

mfg Thomas

jak 04.05.2005 14:53

Andere Möglichkeit:
Code:

pos = InStr(text, "+")
While (pos <> Null) And (pos <> 0)
  d = Val(Left(text, pos - 1)) + d
  text = Mid(text, pos + 1)
  pos = InStr(text, "+")
Wend

Jak


Alle Zeitangaben in WEZ +2. Es ist jetzt 18:23 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© 2009 FSL Verlag