If cell contains three values (23 + 34 + 12) split them into separate rows

pitaszek

Board Regular
Joined
Jul 20, 2012
Messages
85
Hi Guys,

The example is as follows. Diameter which has more numbers than one needs to be splitten into rows.
NumberNumber2NameDiameterDiameter-totalSome num
114114Tree123 + 45 + 239191
115115Tree224 + 568080
116116Tree3232323
117117Tree4454545
118118Tree534 + 21 + 90 + 45190190

<tbody>
</tbody>

As an example first row (Tree1) would split into the following:
NumberNumber2NameDiameterDiameter-totalSome num
114114Tree1239191
114114Tree1459191
114114Tree1239191

<tbody>
</tbody>

If there is only one value in Diameter column no actions should be taken and for cell with four values, three additional rows should be inserted etc. There might be up to ten values within a cell

For now my code puts empty rows below the rows which have more then 1 value in a diameter field but I have no idea how to split it like presented.

Original file for processing has more than 6 columns.

Code:
Sub fd()


Dim last_row As Integer


last_row = Cells(Rows.Count, 1).End(xlUp).Row


For i = 2 To last_row


'take value from column D
an_cell = Range("D" & i).Value


'cont how many pluses are there in a string
Countplus = Len(an_cell) - Len(Replace(an_cell, "+", ""))


'if number of pluses in a string is > 0 then insert additinal rows
If Countplus > 0 Then


    'loop inserting rows
    For j = 1 To Countplus
    Rows(i + 1).Insert
    Sheets(1).Range("A" & i, "AA" & i).Copy
    Sheets(1).Range("A" & i + 1).PasteSpecial xlPasteAll
    Sheets(1).Range("D" & i + 1).ClearContents
    
    Next j


End If


Next i


End Sub

Thanks for your assitance in advance. Happy to anwser any questions related.

Cheers,
Witek
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Untested, but try this
Code:
Sub SplitOnPlus()

   Dim Cnt As Long
   Dim splt As Long
   
   For Cnt = Range("D" & Rows.Count).End(xlUp).row To 2 Step -1
      If InStr(Range("D" & Cnt), ",") Then
         splt = UBound(Split(Range("D" & Cnt), "+"))
         Rows(Cnt + 1).Resize(splt).Insert
         Rows(Cnt).Resize(splt + 1).FillDown
         Range("D" & Cnt).Resize(splt + 1).Value = Application.Transpose(Split(Range("D" & Cnt), "+"))
         
      End If
   Next Cnt
      
End Sub
 
Last edited:
Upvote 0
Hi Fluff,

This seems to be working fine! I will do some testing and double confirm that.

I will try to understand the code. May I come with some questions to that?

Regards,
Witek
 
Upvote 0

Forum statistics

Threads
1,216,027
Messages
6,128,367
Members
449,444
Latest member
abitrandom82

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