+ Reply to Thread
Results 1 to 13 of 13

Excel 2007 : How I implement a code?

Hybrid 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.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How I implement a code?

    What are you trying to do?

    Would "Text to Columns" not be what you are after?

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

    Re: How I implement a code?

    The think is I put the code in the macro and is look like this.

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
    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
    Application.Goto Reference:="Macro1"
    End Function
    Result: Compile error: Expected End Sub

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

    Re: How I implement a code?

    All codes that I found on the internet and I want to implement give me the same error.
    Can be a excel error?

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How I implement a code?

    Explain what you are trying to do.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

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

    Re: How I implement a code?

    I found the code on the google. The guy who made it said if I use it I could split the paragraph from A1 in other specific cells.
    I have the code, I know what it does. The problem is, how I use it?

    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

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How I implement a code?

    Ask the author.

    We are asking you what you want to do so that we can offer a solution, not try to interpret what you think is the solution

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

    Re: How I implement a code?

    Ok, let me put it in another way.
    I have a code.(doesn't matter what it does).
    Where in excel I put codes?

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How I implement a code?

    You put the code in a standard module

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

    Re: How I implement a code?

    Thank you for the earlier answer.
    I attached a picture. This is a standard module?
    Attached Images Attached Images

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,851

    Re: How I implement a code?

    Quote Originally Posted by marianmix View Post
    Thank you for the earlier answer.
    I attached a picture. This is a standard module?
    Yes, that is a standard module. Just paste the code in, alone. You do not need to add any other code, like you did here:

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
    Then in your spreadsheet you can use the function like this:

    =SplitText(A1,5,10)

    or whatever arguments are appropriate.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: How I implement a code?

    To run the actual code

    Sub DoIt()
    'str can be a phrase or contained in a cell
    'iPart should be a number > 0
    ' iMaxChars is an integer representing the max characters required
    
    SplitText(str As String, iPart As Byte, iMaxChars As Integer) As String
    End Sub
    You would still be better off attaching an example & explaining what you want to do!

  13. #13
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: How I implement a code?

    hi Marianmix,

    In addition to the very sensible requests for you to explain what you want to do...


    It looks like you may be biting off more than you can chew. Here is a link for VBA beginners: http://www.mvps.org/dmcritchie/excel/getstarted.htm
    which is included in this larger list of helpful links (http://www.excelforum.com/excel-gene...additions.html). I recommend you read through all the ones under "VBA for beginners" & perhaps the Tutorials as well.

    Happy learning & Goodluck!

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

+ Reply to Thread

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