VBA - Divide rows based on condition

Auctor Somnium

New Member
Joined
Sep 15, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
My office version is 365 in case that's relevant.
I never posted a question in this forum so sorry if my explanation isn't good enough.

This sheet is just to ilustrate, none of this is what is actually in my sheet but it is similar to what I need to do.
1663278405934.png

My objective is to split a row based if in "Type", "RD/Ready" shows up. If it does I want a new row to be made underneath the original one.

There are some criterias to this:
- The sheet will only have more rows as time goes on so it would be good if this macro is able to work on future rows as well;
- The values from the columns that aren't marked would just be duplicated to the new row created;
- In Row 2: Type should have only "Ready" written in column "F"and Row 1 should change to only "RD";
- In Row 1: Columns "Production Ready", "Total Cost Ready" should have set to 0 and that should happen to "Production RD", "Total Cost Ready" in Row 2.

OBS:
I called the original row as Row 1 and the row that should be created as Row 2 to facilitate in explaining
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I think you want something like this. The below code should work if the spreadsheet is set up exactly like the one you provide, but will break if any of the columns are moved around.

VBA Code:
Sub SplitRow()
Dim WB As Workbook
Set WB = Application.ActiveWorkbook
    Dim WS As Worksheet
    Set WS = WB.ActiveSheet
    
Dim i As Long
Dim NumofRows As Long

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

For i = 2 To NumofRows
    If WS.Range("F" & i).Value = "RD/Ready" Then
        WS.Range("F" & i + 1).EntireRow.Insert
         WS.Range("A" & i + 1 & ":" & "F" & i + 1).Value = WS.Range("A" & i & ":" & "F" & i).Value
         WS.Range("N" & i + 1 & ":" & "O" & i + 1).Value = WS.Range("A" & i & ":" & "F" & i).Value
         WS.Range("F" & i).Value = "RD"
         WS.Range("H" & i).Value = 0
         WS.Range("L" & i).Value = 0
         WS.Range("F" & i + 1).Value = "Ready"
         WS.Range("G" & i +1).Value = 0
         WS.Range("L" & i + 1).Value = 0
         
    Else: End If
Next
        
End Sub
 
Upvote 0
Let me know how it works for you. It checks each row for "RD/Ready" and then takes actions if it finds it. You should be able to run it as many times as you want as you insert new data and it will just ignore rows it has already split.
 
Upvote 0
I think you want something like this. The below code should work if the spreadsheet is set up exactly like the one you provide, but will break if any of the columns are moved around.

VBA Code:
Sub SplitRow()
Dim WB As Workbook
Set WB = Application.ActiveWorkbook
    Dim WS As Worksheet
    Set WS = WB.ActiveSheet
   
Dim i As Long
Dim NumofRows As Long

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

For i = 2 To NumofRows
    If WS.Range("F" & i).Value = "RD/Ready" Then
        WS.Range("F" & i + 1).EntireRow.Insert
         WS.Range("A" & i + 1 & ":" & "F" & i + 1).Value = WS.Range("A" & i & ":" & "F" & i).Value
         WS.Range("N" & i + 1 & ":" & "O" & i + 1).Value = WS.Range("N" & i & ":" & "O" & i).Value
         WS.Range("F" & i).Value = "RD"
         WS.Range("H" & i).Value = 0
         WS.Range("L" & i).Value = 0
         WS.Range("F" & i + 1).Value = "Ready"
         WS.Range("G" & i +1).Value = 0
         WS.Range("L" & i + 1).Value = 0
        
    Else: End If
Next
       
End Sub
Corrected a type=o
 
Upvote 0
Solution
This is nearly exactly what I needed there were a few things I had to add to make it work in the sheet I made because the values weren't copying the way they should in the created Row.
Thanks so much it really helped I'm going to adapt it to the actual spreadsheet now
 
Upvote 0
Sorry it was a bit of a rush job, glad you were able to work out the kinks.
 
Upvote 0
Yeah it worked, but for some reason in the actual sheet it isn't altering lines above 5175, I really don't know why
 
Upvote 0
It works if I run it again I think it's to do with the limits of the computer im using
 
Upvote 0

Forum statistics

Threads
1,214,577
Messages
6,120,359
Members
448,956
Latest member
Adamsxl

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