How make long string in a cell become text line by line in same cell?

  • Thread starter Thread starter geniusideas
  • Start date Start date
G

geniusideas

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
 
geniusideas said:
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
 
Sorry Guys,

Not so accurate because the result should be as below

Before
AAAAA (BBBB) (CCCC(DD)) | AAAAA (EEEE) (FFFFFF) | GGGGG (EEEE)
(HHHHHH)

After
AAAAA
(BBBB)
(CCCC(DD))
(EEEE)
(FFFFFF)
(GGGGG)
(HHHHHH)

Remove duplication and line by line in one in same cell
 
Sorry Guys,

Not so accurate.

I need the the answer as below

Before
AAAAA (BBBB) (CCCC(DD)) | AAAAA (EEEE) (FFFFFF) | GGGGG (EEEE)
(HHHHHH)

After
AAAAA
(BBBB)
(CCCC(DD))
(EEEE)
(FFFFFF)
(GGGGG)
(HHHHHH)

Thanks anyway..
 
Sorry Guy,

The above example both OK but for my actual data was not accurate. my
actual data have more spaces in between as below:

Before
AA AAAA (BB BBBB) (CC CCCC(DD)) | AA AAAA (EE EEEE) (FFFFFF) | GG GGGG
(EE EEEE) (HHHHHH)

After
AA AAAA
(BB BBBB)
(CC CCCC(DD))
(EE EEEE)
(FFFFFF)
GG GGGG
(HHHHHH)

Before is in one line and after become line by line and remove
duplicate

Sorry Guy. The question is not accurate before
Please help.
Thanks
 
Back
Top