How make long string in a cell become text line by line in same cell?
geniusideas wrote:
Hi, All
I really need desperately in VBA code for my new project! I have long
text string in single cell which separated by bracket as below:
AAAAA (BBBB) (CCCC(DD)) | AAAAA (EEEE) (FFFFFF) | GGGGG (EEEE)
(HHHHHH)
The above text is one line and sometime up to 300 characters. What I
need to do is
to split the above text line by line and remove duplication as below
but still in the same cell:
AAAAA
(BBBB)
(CCCC(DD)
(EEEE)
(FFFFFF)
(GGGGG)
(HHHHHH)
The sequence must be kept and as you can see the duplication already
remove for AAAAA and (EEEE).
Hopefully anyone here can help.Thanks
This assumes that they're formatted *exactly* as above: separated by
spaces, pipes need to be discarded, etc. It's a bit of a mess, and uses TEH
EVILE GOTO, but it works (on the one line of data you supplied). Test on
test data before using it live; this REPLACES the unformatted data with the
results.
Sub foo()
Dim tmp1 As Variant
Dim L0 As Long, L1 As Long, L2 As Long 'loop counters
Dim startval As Long
tmp1 = Split(ActiveCell.Formula)
restart1:
startval = L0
For L0 = startval To UBound(tmp1) - 1
If "|" = tmp1(L0) Then
For L1 = L0 To UBound(tmp1) - 1
tmp1(L1) = tmp1(L1 + 1)
Next
ReDim Preserve tmp1(UBound(tmp1) - 1)
GoTo restart1
Else
startval = L0 + 1
restart2:
For L1 = startval To UBound(tmp1)
If (tmp1(L0)) = tmp1(L1) Then
For L2 = L1 To UBound(tmp1) - 1
tmp1(L2) = tmp1(L2 + 1)
Next
ReDim Preserve tmp1(UBound(tmp1) - 1)
startval = L1
GoTo restart2
End If
Next
End If
Next
ActiveCell.Formula = Join(tmp1, vbCrLf)
End Sub
--
- Are my questions stupid?
- Not as bad as your answers.
|