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

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
 
A

Auric__

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
 
R

Ron Rosenfeld

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

Try this. It assumes
Data in A1, but you can easily loop through a series of cells
Strings to keep are <space> separated
Parentheses to be retained as per your example

================================
Option Explicit
Sub DeDupString()
Dim rg As Range
Dim s As String, v() As Variant
Dim re As Object, mc As Object, m As Object
Dim coll As Collection, i As Long
Set rg = Range("A1")
Set re = CreateObject("vbscript.regexp")
Set coll = New Collection
With re
.Pattern = "(?:[\w()]+)"
.Global = True
End With

s = rg.Text
If re.test(s) = True Then
Set mc = re.Execute(s)
On Error Resume Next
For Each m In mc
coll.Add Item:=m, Key:=CStr(m)
Next m
End If

ReDim v(0 To coll.Count - 1)
For i = 1 To coll.Count
v(i - 1) = coll(i)
Next i

With rg
.Value = Join(v, vbLf)
.WrapText = True
.EntireColumn.AutoFit = True
.EntireRow.AutoFit = True
End With

End Sub
============================
 
G

geniusideas

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
 
G

geniusideas

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

geniusideas

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
 
R

Ron Rosenfeld

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

It should not be any surprise that if you supply neither representative data, nor clear cut specifications, you will receive solutions which only work on the data you supply.

Is the data you have supplied now truly representative? Or are there other variations which you are keeping a secret?

To account for the new data you have supplied, in the solution I provided, you only need to change the regular expression pattern.


.Pattern = "\([^)]+\)+|(\w)(?:[\s\S](?=\1|\b))*"


Whether it will work on your "actual data" depends on how representative your examples are. You show strings which consist of the SAME letter repeated a varying number of times, sometimes with an included space; and sometimes surrounded by parentheses. If that does not describe your real data, then the routine will not work.

Also note that what I have supplied will only work on a single cell. To have this work on multiple cells, in addition to the looping, you also will need to clear out the collection each time. But maybe you only have a single cell to deal with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top