Consulting

Results 1 to 4 of 4

Thread: Remove Array Elements

  1. #1

    Remove Array Elements

    Hi,

    I'm using Split to break a string where Chr(13) occurs, and then to populate an array.

    When I examine the array, some elements show as "space".

    How do I remove the Chr(13) items before the array is constructed, please ?

    My Split code:


      strCoAddress = tb16.Value
      ' Split the string based on carriage returns,
      ' and load the array
      arrCoAddress = Split(strCoAddress, vbLf)
    Many thanks.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I think you need to cleanse the data to get rid of space+CR, CR+space, and CR+CR character pairs, and then do the Split()

    vbCr is the built in constant for ASCII (13)


    Option Explicit
    Sub test()
        Dim i As Long, j As Long
        Dim strCoAddress As String
        Dim aCoAddress  As Variant
    
        strCoAddress = "ACME Widgets  " & vbCr
        strCoAddress = strCoAddress & vbCr
        strCoAddress = strCoAddress & "1234 5th Street    " & vbCr
        strCoAddress = strCoAddress & vbCr
        strCoAddress = strCoAddress & "East Jabip, AA 12345-6789   " & vbCr
    
        aCoAddress = Split(strCoAddress, vbCr)
        For i = LBound(aCoAddress) To UBound(aCoAddress)
            MsgBox aCoAddress(I)
        Next I
    
    '------------------------------------------------------------------
        
        j = InStr(strCoAddress, " " & vbCr)
        Do While j > 0
            strCoAddress = Replace(strCoAddress, " " & vbCr, vbCr)
            j = InStr(strCoAddress, " " & vbCr)
        Loop
        
        j = InStr(strCoAddress, vbCr & " ")
        Do While j > 0
            strCoAddress = Replace(strCoAddress, vbCr & " ", vbCr)
            j = InStr(strCoAddress, vbCr & " ")
        Loop
        
        j = InStr(strCoAddress, vbCr & vbCr)
        Do While j > 0
            strCoAddress = Replace(strCoAddress, vbCr & vbCr, vbCr)
            j = InStr(strCoAddress, vbCr & vbCr)
        Loop
        
        If Right(strCoAddress, 1) = vbCr Then strCoAddress = Left(strCoAddress, Len(strCoAddress) - 1)
        aCoAddress = Split(strCoAddress, vbCr)
        For i = LBound(aCoAddress) To UBound(aCoAddress)
            MsgBox aCoAddress(i)
        Next i
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    arrCoAddress = Split(strCoAddress, vbCrLf

  4. #4
    Paul & snb,

    Many thanks.

    Both answers work for me.

    Regards.

Posting Permissions

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