PDA

View Full Version : [SOLVED] Remove Array Elements



stuartgb100
04-13-2016, 11:02 AM
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.

Paul_Hossler
04-13-2016, 11:23 AM
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

snb
04-13-2016, 11:25 AM
arrCoAddress = Split(strCoAddress, vbCrLf

stuartgb100
04-13-2016, 07:20 PM
Paul & snb,

Many thanks.

Both answers work for me.

Regards.