Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
496to500
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
496to500
496to500
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

VBA: Array an Function übergeben

VBA: Array an Function übergeben
11.10.2004 10:10:07
Matthias
Ich schreibe eine Funktion textsplit (analog zu Split in 2000).

Function textsplit(text, delimiter)
Dim textarr()
textsplit = textarr
End Function

Das funktioniert soweit auch.
Jetzt schimpft allerdings VBA bei der Übergabe über ungültigen Datentyp.
So zum Beispiel hier:
Dim StatArray
StatArray = textsplit(text, vbTab)
If UBound(StatArray) = 6 Then
^^^^^^
Mir ist unklar was ich ändern müsste.
Matthias

2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: VBA: Array an Function übergeben
ungar
Hallo Matthias,
du musst sowohl das "StatArray" als auch die Funktion als Datentyp "Variant" deklarieren. Damit klappt es

Sub TestTextSplit()
Dim StatArray As Variant
StatArray = textsplit(text, vbTab)
MsgBox UBound(StatArray)
End Sub


Function textsplit(text, delimiter) As Variant
Dim textarr(1 To 6)
textsplit = textarr
End Function

AW: VBA: Array an Function übergeben
Dan
Hallo Matthias, hier ein Beispiel. Man muss den Variant Type benutzen, weil es kann alles "tragen" :-). Dann benutzt man die VarType Funktion um feststellen zu koennen, was fuer eine Sub-Typ sich in der Variant Variable befindet. Gruss Dan, CZ.


Option Explicit
Private Function TextSplit(ByVal i_strText As StringByVal i_strDelimiter As StringAs Variant
    
    TextSplit = VBA.Split(i_strText, i_strDelimiter)
    
End Function
Private Sub Test()
    Dim arr As Variant
    
    
    arr = TextSplit("a,b,c,d,e,f", ",")
    
    ' Split : Returns a zero-based, one-dimensional array containing a specified number of substrings.
    If (VBA.VarType(arr) = vbString + vbArray) Then
        MsgBox "UBound(arr) = " & UBound(arr)
    Else
        MsgBox "Kein Array."
    End If
    
    ' ----------------------------------------------
    
    arr = TextSplit("", "")
    
    If (VBA.VarType(arr) = vbString + vbArray) Then
        MsgBox "UBound(arr) = " & UBound(arr)
    Else
        MsgBox "Kein Array."
    End If
    
    ' ----------------------------------------------
    
    arr = 150
    
    If (VBA.VarType(arr) = vbString + vbArray) Then
        MsgBox "UBound(arr) = " & UBound(arr)
    Else
        MsgBox "Kein Array."
    End If
    
'    Remarks
'
'    The VarType function never returns the value for vbArray by itself.
'    It is always added to some other value to indicate an array of a particular type.
'    The constant vbVariant is only returned in conjunction with vbArray to indicate that
'    the argument to the VarType function is an array of type Variant.
'    For example, the value returned for an array of integers is calculated as vbInteger + vbArray,
'    or 8194. If an object has a default property, VarType (object) returns the type of the
'    Object 's default property.
End Sub


Anzeige

38 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige