Split in row and auto insert row as per requirement

Vishaal

Well-known Member
Joined
Mar 16, 2019
Messages
543
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
  2. Web
Hi, we have the following sheet where we want to split in row wise instead of column wise, can we do this from macro or any formula, its a large database

query sheet
S.No.CodeNamePriceTotalQty
18185,7965,8175General,hest254,tinkvol1140,320,280105,240,2101,1,1
26594jhset1401051
37652,8795,8473,8265rosey,randy-1,dom(vol1)(ed-1),Series test320,280,140,295240,210,105,2211,1,1,1


required sheet
S.No.CodeNamePriceTotalQty
18185General1401051
27965hest2543202401
38175tinkvol12802101
46594jhset1401051
57652rosey3202401
68795randy-12802101
78473dom(vol-1)(ed-1)1401051
88265Series test2952211



If there is no formula, kindly help with VBA please
 
Last edited by a moderator:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi. Try this code, please.
The code will put the result in sheet ReqSheet.
Change sheet names to suit.

VBA Code:
Sub SplitIntoRows()
 Dim nxtRow As Long, vAllData As Variant, i As Long, m As Long
 Dim colB As Variant, colC As Variant, colD As Variant, colE As Variant, colF As Variant
  Application.ScreenUpdating = False
  vAllData = Sheets("QuerySheet").Range("B2:F" & Sheets("QuerySheet").Cells(Rows.Count, 1).End(3).Row).Value2
  nxtRow = 2
  With Sheets("ReqSheet")
   .[A:F].Clear
   Sheets("QuerySheet").[A1:F1].Copy .[A1]
    For i = 1 To Sheets("QuerySheet").Cells(Rows.Count, 1).End(3).Row - 1
     colB = Split(vAllData(i, 1), ",")
     colC = Split(vAllData(i, 2), ",")
     colD = Split(vAllData(i, 3), ",")
     colE = Split(vAllData(i, 4), ",")
     colF = Split(vAllData(i, 5), ",")
     For m = LBound(colB) To UBound(colB)
      .Cells(nxtRow, 2) = colB(m)
      .Cells(nxtRow, 3) = colC(m)
      .Cells(nxtRow, 4) = colD(m)
      .Cells(nxtRow, 5) = colE(m)
      .Cells(nxtRow, 6) = colF(m)
      nxtRow = nxtRow + 1
     Next m
    Next i
    .[A2] = 1: .Range("A2:A" & .Cells(Rows.Count, 2).End(3).Row).DataSeries , xlLinear
  End With
End Sub
 
Upvote 1
Solution
Here is an alternative solution employing Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", type text}, {"Name", type text}, {"Price", type text}, {"Total", type text}, {"10,", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "CODE.1", each Text.Split([Code],",")),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "NAME.1", each Text.Split([Name],",")),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "PRICE.1", each Text.Split([Price],",")),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "TOTAL.1", each Text.Split([Total],",")),
    #"Added Custom4" = Table.AddColumn(#"Added Custom3", "TEN", each Text.Split([#"10,"],",")),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom4",{"Code", "Name", "Price", "Total", "10,"}),
    #"Added Custom5" = Table.AddColumn(#"Removed Columns", "CustomX", each Table.FromColumns({[CODE.1],[NAME.1],[PRICE.1],[TOTAL.1],[TEN]})),
    #"Expanded CustomX" = Table.ExpandTableColumn(#"Added Custom5", "CustomX", {"Column1", "Column2", "Column3", "Column4", "Column5"}, {"Column1", "Column2", "Column3", "Column4", "Column5"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded CustomX",{"Column1", "Column2", "Column3", "Column4", "Column5"})
    
in
    #"Removed Other Columns"
Book4
ABCDEF
1S.NoCodeNamePriceTotal10,
218185,7965,8175General, hest254,tinkvol1140,320,280105,240,2101,1,1
326594jhest1401051
437652,8795,8473,8260rosey, randy-1,dom(vol1)(ed-1), Series test320 ,280,140,295240,210,105,2211,1,1,1
5
6
7Column1Column2Column3Column4Column5
88185General1401051
97965 hest2543202401
108175tinkvol12802101
116594jhest1401051
127652rosey3202401
138795 randy-12802101
148473dom(vol1)(ed-1)1401051
158260 Series test2952211
Sheet1
 
Upvote 1
its a large database
I don't know how large you are talking but over 20k records the Power Query was much faster than the VBA.
Since you seem to want the VBA version, just modify Osvaldo's solution to use an array for the output and that will speed it up significanlty.

VBA Code:
Sub SplitIntoRows_Osvaldo_mod()
    
    Dim nxtRow As Long, vAllData As Variant, i As Long, m As Long
    Dim colB As Variant, colC As Variant, colD As Variant, colE As Variant, colF As Variant
    Dim outarr()

    Application.ScreenUpdating = False
    
    vAllData = Sheets("QuerySheet").Range("B2:F" & Sheets("QuerySheet").Cells(Rows.Count, 1).End(3).Row).Value2
    nxtRow = 1
    ReDim outarr(1 To 1000000, 1 To UBound(vAllData, 2) + 1)

    Sheets("ReqSheet").Columns("A:F").Clear
    Sheets("QuerySheet").Range("A1:F1").Copy Sheets("ReqSheet").Range("A1")
    For i = 1 To Sheets("QuerySheet").Cells(Rows.Count, 1).End(3).Row - 1
        colB = Split(vAllData(i, 1), ",")
        colC = Split(vAllData(i, 2), ",")
        colD = Split(vAllData(i, 3), ",")
        colE = Split(vAllData(i, 4), ",")
        colF = Split(vAllData(i, 5), ",")
        For m = LBound(colB) To UBound(colB)
            outarr(nxtRow, 1) = nxtRow
            outarr(nxtRow, 2) = colB(m)
            outarr(nxtRow, 3) = colC(m)
            outarr(nxtRow, 4) = colD(m)
            outarr(nxtRow, 5) = colE(m)
            outarr(nxtRow, 6) = colF(m)
            nxtRow = nxtRow + 1
        Next m
    Next i
    
    Sheets("ReqSheet").Range("A2").Resize(nxtRow - 1, UBound(outarr, 2)) = outarr
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
getting run time error - 104

application defined or object defined error

Sheets("ReqSheet").Range("A2").Resize(nxtRow - 1, UBound(outarr, 2)) = outarr
 
Upvote 0
Hi. Try this code, please.
The code will put the result in sheet ReqSheet.
Change sheet names to suit.

VBA Code:
Sub SplitIntoRows()
 Dim nxtRow As Long, vAllData As Variant, i As Long, m As Long
 Dim colB As Variant, colC As Variant, colD As Variant, colE As Variant, colF As Variant
  Application.ScreenUpdating = False
  vAllData = Sheets("QuerySheet").Range("B2:F" & Sheets("QuerySheet").Cells(Rows.Count, 1).End(3).Row).Value2
  nxtRow = 2
  With Sheets("ReqSheet")
   .[A:F].Clear
   Sheets("QuerySheet").[A1:F1].Copy .[A1]
    For i = 1 To Sheets("QuerySheet").Cells(Rows.Count, 1).End(3).Row - 1
     colB = Split(vAllData(i, 1), ",")
     colC = Split(vAllData(i, 2), ",")
     colD = Split(vAllData(i, 3), ",")
     colE = Split(vAllData(i, 4), ",")
     colF = Split(vAllData(i, 5), ",")
     For m = LBound(colB) To UBound(colB)
      .Cells(nxtRow, 2) = colB(m)
      .Cells(nxtRow, 3) = colC(m)
      .Cells(nxtRow, 4) = colD(m)
      .Cells(nxtRow, 5) = colE(m)
      .Cells(nxtRow, 6) = colF(m)
      nxtRow = nxtRow + 1
     Next m
    Next i
    .[A2] = 1: .Range("A2:A" & .Cells(Rows.Count, 2).End(3).Row).DataSeries , xlLinear
  End With
End Sub


only getting one data, dont know where is problem
 
Upvote 0
I have logged for the night.
Things to check,
Is ReqSheet the correct sheet name ?
If you hover over nxtrow what no do you see ?
If you modified Osvaldo's code please post the exact code you had working and I will have a look tomorrow.
(How many rows in your data ?)
 
Upvote 0
I have logged for the night.
Things to check,
Is ReqSheet the correct sheet name ?
If you hover over nxtrow what no do you see ?
If you modified Osvaldo's code please post the exact code you had working and I will have a look tomorrow.
(How many rows in your data ?)
Sheet name same
code is not modified
row 4294
using everything same
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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