VBA: Split text in cells to multiple rows

sr1111

New Member
Joined
Sep 2, 2022
Messages
46
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
Hi,

I am looking for a VBA to split the text into cells into rows.

Input:
test.xlsx
A
1
2Company, id*; ¤¤¤Communications Media, http://www.Communications.com/Media.
3Company, id*1; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.+=+enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.+=+enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u..///Company, Name 33T; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.///Company, Name 33T; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
Sheet1


Output1:
test.xlsx
A
1
2Company, id*; ¤¤¤Communications Media, http://www.Communications.com/Media.
3Company, id*1; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.+=+enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.+=+enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u..
4Company, Name 33T; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
5Company, Name 33T; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
Sheet2


output2:
test.xlsx
A
1
2Company, id*; ¤¤¤Communications Media, http://www.Communications.com/Media.
3Company, id*1; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
4Company, id*1; enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
5Company, id*1; enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u..
6Company, Name 33T; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
7Company, Name 33T; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
Sheet3


output3:
test.xlsx
AB
1
2Company, id*¤¤¤Communications Media, http://www.Communications.com/Media.
3Company, id*1¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
4Company, id*1enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
5Company, id*1enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u..
6Company, Name 33T¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
7Company, Name 33T¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.
Sheet4
 
Pardon me.
I can't figure out what your rules of splitting are.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Code to get output3
VBA Code:
Sub Split_Text()
Dim A, M, N
Dim Lr As Long, T As Long, Ta As Long

Lr = Range("A" & Rows.Count).End(xlUp).Row
A = Sheets("Sheet1").Range("A2:A" & Lr)
With Sheets("Sheet2")
.Range("A1") = "Result"
For T = 1 To UBound(A, 1)
A(T, 1) = Replace(A(T, 1), "+=+", "///" & Left(A(T, 1), InStr(1, A(T, 1), "; ") + 2))
M = ""
M = Split(A(T, 1), "///")
    For Ta = 0 To UBound(M)
    N = ""
    N = Split(M(Ta), "; ")
    .Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Resize(1, UBound(N) + 1) = N ' Application.Transpose(M)
    Next Ta
Next T
End With
End Sub
 
Upvote 0
Thank you

kvsrinivasamurthy


And In sheet1/column B, I have a unique ID in the respective row as well. If a unique ID is available in a row cell, how to copy that into the output? I want to see from which ID the results are appearing.
 
Upvote 0
And In sheet1/column B, I have a unique ID in the respective row as well. If a unique ID is available in a row cell, how to copy that into the output? I want to see from which ID the results are appearing.
Pardon me.
I can't figure out what your rules of splitting are.
 
Upvote 0
If unique ID is available is it to be copied all the split rows . Pl paste a image or mini sheet showing how is data and expected result.
 
Upvote 0
If unique ID is available is it to be copied all the split rows . Pl paste a image or mini sheet showing how is data and expected result.

Here iit is:

Input:


test111.xlsx
AB
1CompanyIDUnique ID
2 Company, id*; ¤¤¤Communications Media, http://www.Communications.com/Media///Company, id*;/// Company, id**;1@#$%2company
3Company, id*1; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.+=+enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.+=+enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u..///Company, Name 33T; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.///Company, Name 33T; Company, Name 33T;¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.///Company, id*;///Company, id*1@# $%3a 1@# $%3a 1@# $%3a
4id, comi;///com1, id;///com22, id;1@#$%4b 1@#$%4b
5id, comi;///===id, comi;///1@#22$%6c
Sheet1


Output:
test111.xlsx
ABC
1ResultotherUnique ID
2Company, id¤¤¤Communications Media, http://www.Communications.com/Media1@#$%2company
3Company, id*1@#$%2company
4Company, id**1@#$%2company
5Company, id*1¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.1@# $%3a 1@# $%3a 1@# $%3a
6Company, id*1enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.1@# $%3a 1@# $%3a 1@# $%3a
7Company, id*1enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u..1@# $%3a 1@# $%3a 1@# $%3a
8Company, Name 33T¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u1@# $%3a 1@# $%3a 1@# $%3a
9Company, Name 33TCompany, Name 33T;¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.; ¤¤¤enter fr gic an, ght cer tit, reo th and ce ersit, and, p0 99, 9A. c e:9k@o.u.1@# $%3a 1@# $%3a 1@# $%3a
10Company, Name 33T1@# $%3a 1@# $%3a 1@# $%3a
11Company, Name 33T1@# $%3a 1@# $%3a 1@# $%3a
12Company, id*1@# $%3a 1@# $%3a 1@# $%3a
13Company, id*1@# $%3a 1@# $%3a 1@# $%3a
14id, comi1@#$%4b 1@#$%4b
15com1, id1@#$%4b 1@#$%4b
16com22, id1@#$%4b 1@#$%4b
17id, comi1@#22$%6c
18===id, comi1@#22$%6c
Sheet2
 
Upvote 0
There is some problem with A3 value. There are only # numbers of "Company, Name 33T". Expected result shows $ numbers and "///" are missing.
Code:
VBA Code:
Sub Split_Text()
Dim A, M
Dim Lr As Long, T As Long, Ta As Long, Ro As Long, Cnt As Long

Lr = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
A = Sheets("Sheet1").Range("A2:B" & Lr)
With Sheets("Sheet2")
.Range("A1").CurrentRegion.Clear
.Range("A1") = "Result"
For T = 1 To UBound(A, 1)
A(T, 1) = Replace(A(T, 1), "+=+", "///" & Left(A(T, 1), InStr(1, A(T, 1), "; ") + 2))
M = ""
M = Split(A(T, 1), "///")
    For Ta = 0 To UBound(M)
    If M(Ta) <> "" Then
    Cnt = InStr(1, M(Ta), ";")                         
        If Cnt > 0 Then                                
        Ro = .Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
        .Range("A" & Ro & ":C" & Ro).NumberFormat = "@"
        .Range("A" & Ro) = Trim(Left(M(Ta), Cnt - 1))  
        .Range("B" & Ro) = Trim(Mid(M(Ta), Cnt + 1))
        .Range("C" & Ro) = A(T, 2)
        End If
    End If
    Next Ta
Next T
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,222
Messages
6,123,717
Members
449,116
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top