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
 
Ahh, now I think I see what you are trying to accomplish. Give this formula a try (keyed to the data you showed in Message #8)...
Excel Formula:
=TEXTSPLIT(TEXTJOIN("/",,SUBSTITUTE(G10:G14&"-"&H10:H14&"-"&I10:I14," / ","/--")),"-","/")
 
Upvote 1
Solution

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Ahh, now I think I see what you are trying to accomplish. Give this formula a try (keyed to the data you showed in Message #8)...
Excel Formula:
=TEXTSPLIT(TEXTJOIN("/",,SUBSTITUTE(G10:G14&"-"&H10:H14&"-"&I10:I14," / ","/--")),"-","/")
I think i'm almost there I need to add one more link to the cells and this is how it's looking.

=TEXTSPLIT(TEXTJOIN("/",,SUBSTITUTE(J2:J1000&"-"&K2:K1000&"-"&L2:L1000&"-"&M2:M1000," / ","/---")),"-","/")

But I'm getting SPILL error I'm trying to output this in column F,G,H and I where there are no formulas.
 
Upvote 0
I think Im getting that because in column J I have numbers like 102-412-21 and so on and it's outputting way different format. Maybe because of the dash in the numbers?

Edit: Yes this was the reason I just replaced the dash with + and it's working now. Thank you!
 
Upvote 0
Just one note how I can modify this to hide 0 in column G if there is no data in J,K,L,M.
 
Upvote 0
Just one note how I can modify this to hide 0 in column G if there is no data in J,K,L,M.
See if this fixes the problem...
Excel Formula:
=TEXTSPLIT(SUBSTITUTE(TEXTJOIN("/",,SUBSTITUTE(J2:J1000&"-"&K2:K1000&"-"&L2:L1000&"-"&M2:M1000," / ","/---")),"/---/","/"),"-","/")
 
Upvote 0
See if this fixes the problem...
Excel Formula:
=TEXTSPLIT(SUBSTITUTE(TEXTJOIN("/",,SUBSTITUTE(J2:J1000&"-"&K2:K1000&"-"&L2:L1000&"-"&M2:M1000," / ","/---")),"/---/","/"),"-","/")
No it's not hiding the zeros.

See in the screenshot below.

1688245944254.png


Maybe with IF condition or IFERROR?
 
Upvote 0
Any chance you can post a file demonstrating this to DropBox (it's free and safe) so I can see exactly what your setup is. I tried to duplicate what I thought you had and the formula I posted worked fine for my guess at your layout, but your reply indicates I guessed wrong at how your data is arranged.
 
Upvote 0
Ahh, now I think I see what you are trying to accomplish. Give this formula a try (keyed to the data you showed in Message #8)...2
Excel Formula:
=TEXTSPLIT(TEXTJOIN("/",,SUBSTITUTE(G10:G14&"-"&H10:H14&"-"&I10:I14," / ","/--")),"-","/")
Hello, Sorry for reopening this, but is there a way to modify the formula to fill out in the empty rows which are added the respective code as well for example test23 to be added in the empty row for each code ABC, ABV and AB23? So to look like this:

test.xlsm
NO
18TestABC
19Test23ABC
20Test23ABV
21Test23AB23
22Test24ABB
23Test24AVV
24Test24AWW
25Test25ACC
26Test25ATT
27Test25AWQ
28Test25AUU
Sheet1
 
Upvote 0
More or less like that with the 3 fields:

test.xlsm
GHI
33TestTest1ABC
34Test23Test2ABC
35Test23Test2ABV
36Test23Test2AB23
37Test24Test3ABB
38Test24Test3AVV
39Test24Test3AWW
40Test25Test4ACC
41Test25Test4ATT
42Test25Test4AWQ
43Test25Test4AUU
Sheet1
 
Upvote 0
I think I was able to do it via macro:

Sub SplitAndCopyDataWithCorrectedHeader()
Dim ws As Worksheet
Dim lastRow As Long, destRow As Long
Dim i As Long, j As Long, k As Long
Dim headerRange As Range, header1Range As Range, header2Range As Range, header3Range As Range
Dim headerValue As String, header1Value As String, header2Value As String, header3Value As String
Dim valuesHeader As Variant, valuesHeader1 As Variant, valuesHeader2 As Variant, valuesHeader3 As Variant

' Set the worksheet where the data is located
Set ws = ThisWorkbook.Sheets("Sheet2")

' Find the last row with data in column G
lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row

' Set the ranges for Header, Header1, Header2, and Header3
Set headerRange = ws.Range("F11:F" & lastRow)
Set header1Range = ws.Range("G11:G" & lastRow)
Set header2Range = ws.Range("H11:H" & lastRow)
Set header3Range = ws.Range("I11:I" & lastRow)

' Initialize the destination row
destRow = lastRow + 1

' Loop through each row in the range
For i = 1 To header3Range.Rows.Count
headerValue = headerRange.Cells(i, 1).Value
header1Value = header1Range.Cells(i, 1).Value
header2Value = header2Range.Cells(i, 1).Value
header3Value = header3Range.Cells(i, 1).Value

' Split values in Header3 based on "/"
valuesHeader3 = Split(header3Value, " / ")

' Copy Header, Header1, Header2, and each split value in Header3
For j = LBound(valuesHeader3) To UBound(valuesHeader3)
' Find the last used column in the destination row
k = ws.Cells(destRow, ws.Columns.Count).End(xlToLeft).Column

' Increment the column index
k = k + 1

' Write values to the destination cells
ws.Cells(destRow, k).Value = headerValue ' Header value from cell F10
ws.Cells(destRow, k + 1).Value = header1Value
ws.Cells(destRow, k + 2).Value = header2Value
ws.Cells(destRow, k + 3).Value = valuesHeader3(j)

' Move to the next destination row
destRow = destRow + 1
Next j
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,129
Messages
6,123,210
Members
449,090
Latest member
bes000

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