VBA Macro to split data

Shwapx

New Member
Joined
Sep 28, 2022
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I'm trying to create a macro which splits data out of textjoin formula and output it as list.

I have created a macro which do that, but it's not moving the cells and the data is not correct. So I need to adjust the macro to add empty cell in column G to keep up with the data.

Here is my macro at the moment:

Sub SeparateDataForEachCell()
Dim lastRow As Long
Dim dataRange As Range
Dim cell As Range
Dim dataString As String
Dim dataArray() As String
Dim outputRange As Range
Dim outputCell As Range
Dim i As Integer

lastRow = Cells(Rows.Count, "I").End(xlUp).Row
Set dataRange = Range("I1:I" & lastRow)

For Each cell In dataRange
dataString = cell.Value

dataArray = Split(dataString, " / ")

Set outputCell = cell.Offset(0, -1)

For i = LBound(dataArray) To UBound(dataArray)
outputCell.Value = dataArray(i)
Set outputCell = outputCell.Offset(1)
Next i
Next cell
End Sub

Here is an example table:

Book1
GHI
1TestABCABC
2Test23ABCABC / ABV / AB23
3Test24ABBABB / AVV / AWW
4Test25ACCACC / ATT / AWQ / AUU
5ATT
6AWQ
7AUU
8Up you can see the output which I'm getting now
9Below is the ouput which I'm looking for
10TestABC
11Test23ABC
12ABV
13AB23
14Test24ABB
15AVV
16AWW
17Test25ACC
18ATT
19AWQ
20AUU
Sheet1
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Assuming the "Test" text is in cells G1:G4 and the slash delimited text is in cells I1:I4, then this formula will generate the desired output (no VBA required)...
VBA Code:
=TEXTSPLIT(TEXTJOIN("/",,SUBSTITUTE(G1:G4&"-"&I1:I4," / ","/-")),"-","/")
 
Upvote 1
Assuming the "Test" text is in cells G1:G4 and the slash delimited text is in cells I1:I4, then this formula will generate the desired output (no VBA required)...
VBA Code:
=TEXTSPLIT(TEXTJOIN("/",,SUBSTITUTE(G1:G4&"-"&I1:I4," / ","/-")),"-","/")
So I can use that formula and use my results in G and I columns as help to split it as I want. Thanks I will try and report.
 
Upvote 0
I just tried, but it's giving me #Spill error, is it because my results are coming from formula?
 
Upvote 0
Where are you putting this formula at? A #Spill error means you have data or formulas in the output range. The output from my formula is two columns... the first with the properly spaced "Test.." text and the second with the split apart text.
 
Upvote 0
I think a macro would be better approach If I able to include in the macro to add cell for each item splitted in
Where are you putting this formula at? A #Spill error means you have data or formulas in the output range. The output from my formula is two columns... the first with the properly spaced "Test.." text and the second with the split apart text.
I've made it to work, but still it's not working in my real case. Since I have another column in the middle and I actually have to move 2 columns when we do the split otherwise one of the row is staying high and one is going down so data is not correct. That's why I was thinking if I can add cells only in the columns.
 
Upvote 0
I was a little confused about your actual existing data originally... and I still am. What does your EXISTING data actually look like?
 
Upvote 0
I was a little confused about your actual existing data originally... and I still am. What does your EXISTING data actually look like?
I hope this helps a bit.

Book1
GHIJ
10Header1Header2Header3
11TestTest1ABC
12Test23Test2ABC / ABV / AB23
13Test24Test3ABB / AVV / AWW
14Test25Test4ACC / ATT / AWQ / AUU
15
16If I don't have data in header 1 I won't have data inside other headers as well so.
17
18All of the data under these headers is coming from formulas I can move them outisde my result and work with your formula on their place, but I'm getting this result since it's moving only one row and not the two (I have adjusted it to look into column H and I) so column G is falling behind.
19TestTest1ABC
20Test23Test2ABC
21Test24ABV
22Test25AB23
23Test3ABB
24AVV
25AWW
26Test4ACC
27ATT
28AWQ
29AUU
Sheet1
 
Upvote 0
I'm still not clear on the output you need. Columns H and I look like what you asked for... why is Column G there at all? If it is from formulas, can't you just put the formulas somewhere else? I think the problem I am having is in understanding fully what you have and exactly what you want from it.
 
Upvote 0
I'm still not clear on the output you need. Columns H and I look like what you asked for... why is Column G there at all? If it is from formulas, can't you just put the formulas somewhere else? I think the problem I am having is in understanding fully what you have and exactly what you want from it.
Column G should move with Column H since they are part of the result and they are linked. Since we move with the formula down column H G is falling behind.
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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