Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everybody. i drawn down below code. this is meant to return multiple split string parts in same row but different colmuns one beside other. however , even if i click and drag the function on close columns, it keeps returning the first array value only.

Function SCOMPONI(stringa As String) As Variant
Dim TAGLIA As Variant, Colonne As Integer, MATRICE() As String, i As Integer

TAGLIA = Split(Trim(stringa), " ")
Colonne = UBound(TAGLIA)

    ReDim MATRICE(0 To 0, 0 To Colonne)
    For i = 0 To Colonne
        MATRICE(0, i) = TAGLIA(i)
    Next
    
SCOMPONI = MATRICE
End Function


What I have tried:

i tried to explicit array sizes (0 to n) , and writing double circle brackets beside the array variable name [SCOMPONI() = MATRICE()]
but nothing
thanks
Posted
Updated 23-Jun-21 21:54pm
Comments
Maciej Los 24-Jun-21 2:59am    
Can you post input string and expected output?
Mat 257 24-Jun-21 3:28am    
yes,
input "Basilico Biologico 107/21 10Sx Cella1 Gambo" in cell(1,1)

process: split string out " " delimiter

expected output:

Basilico range(1,2) Biologico range(1,3) 107/21 range(1,4) 10Sx range(1,5) Cella1 range(1,6) Gambo range(1,7)
Mat 257 21-Feb-22 9:06am    
thanks
Maciej Los 21-Feb-22 10:11am    
You're very welcome :)

1 solution

As i mentioned in the comment to the question there's no need to write custom function, because it has been written already: Range.TextToColumns method (Excel) | Microsoft Docs[^]

But if you want to return specific "word" from text, you can write a function like this:

VBA
Function SCOMPONI(stringa As String, Optional itemNo As Integer = 0) As Variant
        Dim matrice As Variant, retVal As Variant
        matrice = Split(Trim(stringa), " ")
        
        If itemNo < 0 Then itemNo = 0
        If itemNo > UBound(matrice) Then itemNo = UBound(matrice)
        
        retVal = matrice(itemNo)

        SCOMPONI = retVal
End Function


Usage:
B1 = SCOMPONI($A$1, 0)
C1 = SCOMPONI($A$1, 1)
D1 = SCOMPONI($A$1, 2)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900