dfolzenlogen
New Member
- Joined
- Oct 18, 2009
- Messages
- 36
This may be a dumb question but here goes! I purchased Allen Wyatt's ExcelTips and was trying to implement a User Defined Function referenced there for Splitting Text to Multiple Cells. I copied and pasted the code into my VBA Module. When I use the function and enter the parameters, I see the result I want but the result is not returned to my spreadsheet. What am I missing?
I have the following sample text in Column G:
I am using the following formula in Column I:
Below is the code I am using:
I have the following sample text in Column G:
The quick brown fox jumped over the fence and then ran home as fast as he could.
I am using the following formula in Column I:
=SplitMe(G21,1,10)
Below is the code I am using:
Code:
Function SplitMe(sSentence As String, iPos As Integer, Optional iLen = 12)
Dim sSegments() As String
Dim iSegments As Integer
Dim sRest As String
Dim sTemp As String
Dim iSpace As Integer
Dim J As Integer
iSegments = 0
sRest = sSentence
sTemp = Left(sRest, iLen + 1)
Do Until Len(sTemp) <= iLen
iSpace = 0
For J = Len(sTemp) To 1 Step -1
If Mid(sTemp, J, 1) = " " And iSpace = 0 Then iSpace = J
Next J
If iSpace > 0 Then
sTemp = Left(sRest, iSpace - 1)
sRest = Mid(sRest, iSpace + 1)
Else
sRest = Mid(sRest, Len(sTemp) + 1)
End If
iSegments = iSegments + 1
ReDim Preserve sSegments(1 To iSegments)
sSegments(iSegments) = sTemp
sTemp = Left(sRest, iLen + 1)
Loop
iSegments = iSegments + 1
ReDim Preserve sSegments(1 To iSegments)
sSegments(iSegments) = sTemp
If iPos <= iSegments Then
SplitMe = sSegments(iPos)
Else
SplitMe = ""
End If
End Function