PDA

View Full Version : [VBA] HELP


Serpe
17-06-2004, 17:02
volevo fare una funzione che passatagli una stringa e un separatore mi restituisse una collection con le sub-stringhe che il separatore delimita

Sub passastr()
Dim j As New Collection
stringa = "a;Bcdefg;ghek;ehsie;"
sep = ";"
Set j = DS(stringa, sep)
End Sub


Function DS(ByVal stringa As String, ByVal sep As String) As Collection
Dim substr As New Collection

k = 1 'posini
y = 0 'posfina

Do
y = InStr(k, stringa, sep, vbTextCompare) 'si becca il valore della posiozne del separatore
If y > 0 Then
substr.Add Mid(stringa, k, y - k)
k = y + 1
End If
Loop While y > 0
DS = substr
End Function

ho un errore di compilazione in :
DS1 = substr
dove mi dice "Argomento non facoltativo" , ma non ho capito quale elemento vuole

qualche suggerimento?

cisky
17-06-2004, 20:19
Se invece di una collection ti basta un'array puoi usare direttamente il metodo SPLIT, oppure creare una routine di questo tipo.


Function DS(ByVal sStringa As String, ByVal sSep As String) As Collection

Dim collRet As Collection
Dim sSplit() As String
Dim lUBound As Long
Dim lLowBound As Long
Dim l As Long

Set collRet = New Collection

sSplit() = Split(sStringa, sSep)
lUBound = UBound(sSplit())
lLowBound = LBound(sSplit())

For l = 1 To lUBound
collRet.Add sSplit(l)
Next l

Set DS = collRet

End Function

cisky
17-06-2004, 20:23
Ops, riguardo al tuo problema "Argomento non facoltativo" è perchè devi usare la parola chiave SET per impostare il valore di ritorno della funzione.

Non:

DS = substr

ma:

SET DS = substr


Ciao!

Serpe
18-06-2004, 08:33
Tnx a lot :)