Results 1 to 13 of 13

Excel 2007 : How I implement a code?

Threaded View

  1. #1
    Registered User
    Join Date
    02-13-2011
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2007
    Posts
    38

    How I implement a code?

    Hi,
    I have found this code on the internet but I don't know how to make it to funtion.
    Can anyone tell me what do I have to add in the beggining and in the end to make it work.
    Thank you!

    Function SplitText(str As String, iPart As Byte, iMaxChars As Integer) As String
        
    '  ______________________________________________________________________________
    ' |                                                                              |
    ' |  Wim Gielis                                                                  |
    ' |  [email protected]                                                   |
    ' |  06/09/2007, revised 06/10/2007                                              |
    ' |  Custom function to split text in parts. Words are not broken, and you       |
    ' |         can specify the maximal number of characters in each part            |
    ' |  Also on http://www.wimgielis.be                                             |
    ' |______________________________________________________________________________|
        
        
        Dim arrWords As Variant
        Dim iWordCounter As Integer
        Dim j As Integer
        Dim iPartCounter As Integer
        Dim sConcatTemp As String
        
        If iPart < 1 Then
            SplitText = "Part number should at least be 1"
            Exit Function
        End If
        
        SplitText = ""
        
        If str <> "" Then
        
            str = Trim(str)
            str = Replace(Replace(str, Chr(32), " "), Chr(160), " ")
            str = Replace(str, "  ", " ")
            
            arrWords = Split(str)
            ReDim Preserve arrWords(UBound(arrWords) + 1)
            arrWords(UBound(arrWords)) = "a" 'dummy to avoid an error message later on
            
            iPartCounter = 1
            j = 0
            
            Do While iPartCounter <= iPart
                iWordCounter = 0
                sConcatTemp = ""
                
                Do While Len(sConcatTemp) - 1 <= iMaxChars And j + iWordCounter < UBound(arrWords)
                    sConcatTemp = sConcatTemp & " " & arrWords(j + iWordCounter)
                    iWordCounter = iWordCounter + 1
                Loop
                
                If Len(sConcatTemp) - 1 > iMaxChars Then iWordCounter = iWordCounter - 1
                
                If iPartCounter = iPart Then
                    If Len(sConcatTemp) - 1 > iMaxChars Then
                        SplitText = Trim(Left(sConcatTemp, Len(sConcatTemp) - Len(arrWords(j + iWordCounter))))
                    Else
                        SplitText = Trim(sConcatTemp)
                    End If
                End If
                
                iPartCounter = iPartCounter + 1
                
                If j + iWordCounter = UBound(arrWords) Then Exit Function
                
                j = j + iWordCounter
            Loop
        End If
    End Function
    Last edited by marianmix; 02-27-2011 at 02:17 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1