Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
How make long string in a cell become text line by line in same cell?
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
How make long string in a cell become text line by line in same cell?
On Wed, 26 Oct 2011 08:56:07 -0700 (PDT), 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 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 ============================ |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
How make long string in a cell become text line by line in same cell?
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 |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
How make long string in a cell become text line by line 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.. |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
How make long string in a cell become text line by line in same cell?
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 |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
How make long string in a cell become text line by line in same cell?
On Wed, 26 Oct 2011 16:11:57 -0700 (PDT), geniusideas wrote:
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. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Challenge - Excel Line Feed Character CHR(10) - How to Delete and keep the text formatting without going ro single line in a cell ? | Excel Programming | |||
Challenge - Excel Line Feed Character CHR(10) - How to Delete and keep the text formatting without going ro single line in a cell ? | Excel Worksheet Functions | |||
Excel - How do I fit 2 lines of text in one cell, vs a long line? | Excel Discussion (Misc queries) | |||
how do I make more then one line of pasted text not move to one cell? | New Users to Excel | |||
A 2 line text showing up in the Cell in Excel prints in 1 line | Excel Discussion (Misc queries) |