Split function Array out of range

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
Hi guys,

I would like to split text in vba

VBA Code:
Function ViertesWort(varText As Variant)
    Dim varWoerter As Variant
    
    varWoerter = Split(varText, " ")
'    ViertesWort = varWoerter(3)
    
'    If varWoerter(3) = "" Then
'        Debug.Print "Es ist leer"
'    End If
    
    'Also wrong
    If IsNull(varWoerter(3)) Then
        Debug.Print "Leer"
    End If
    
End Function


So the above code splits string into an array.
But as I don't know the size of the array if the "The sentence is" like this then I will not have an array with varWoerter(3)

and it throughs an error.

I tried to catch it with the above code but in both ways I did not be able to work it out.

Mybe someone can tell me?

I know with ubound I find the last string in the array but if I don't know how long the array is in the first place then it does not work.

The reason behind this is I like to get some information out of the text but don't need to have a longer text split all the way.

Maximum to four
so varWoerter(3)


Thanks for your help!
 
How about..

VBA Code:
Function ViertesWort(varText As String, n As Long) As String
    ViertesWort = Split(varText & Space(n), " ")(n - 1)
End Function

Used like..

Book1
ABCDE
1one two three four five sixonetwothreefour
2one two three fouronetwothreefour
3one two threeonetwothree 
4one twoonetwo  
5oneone   
Sheet1
Cell Formulas
RangeFormula
B1:B5B1=ViertesWort($A1,1)
C1:C5C1=ViertesWort($A1,2)
D1:D5D1=ViertesWort($A1,3)
E1:E5E1=ViertesWort($A1,4)
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,223,262
Messages
6,171,080
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top