Insert row according cell value

mrmaso

New Member
Joined
Oct 10, 2017
Messages
16
Hello,
could me somebody help with macro which will add new rows according some cell. For example:
[TABLE="width: 514"]
<colgroup><col><col><col span="3"><col><col></colgroup><tbody>[TR]
[TD]
A
DSH09306[/TD]
[TD]
B
1002444978[/TD]
[TD]
C
40[/TD]
[TD]
D
499[/TD]
[TD="align: right"]

E​
6:33[/TD]
[TD] F[/TD]
[TD] G[/TD]
[/TR]
[TR]
[TD]ZSH09325[/TD]
[TD]1002445429[/TD]
[TD]40[/TD]
[TD]1 540[/TD]
[TD="align: right"]9:45[/TD]
[TD]Pres[/TD]
[TD]-[/TD]
[/TR]
[TR]
[TD]DBS01135[/TD]
[TD]1002444826[/TD]
[TD]40[/TD]
[TD]1000[/TD]
[TD="align: right"]10:41[/TD]
[TD]Pres[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD]DBS01136[/TD]
[TD]1002444827[/TD]
[TD]40[/TD]
[TD]1000[/TD]
[TD="align: right"]11:50[/TD]
[TD]Pres[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]DBS01136[/TD]
[TD]1002444978[/TD]
[TD]40[/TD]
[TD]1 000[/TD]
[TD="align: right"]12:57[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]DSH09335[/TD]
[TD]1002444983[/TD]
[TD]40[/TD]
[TD]2 666[/TD]
[TD="align: right"]16:01[/TD]
[TD]Pres[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]DSH09305[/TD]
[TD]1002444977[/TD]
[TD]40[/TD]
[TD]1 499[/TD]
[TD="align: right"]17:41[/TD]
[TD]Pres[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]DSH09306[/TD]
[TD]1002444978[/TD]
[TD]40[/TD]
[TD]1 000[/TD]
[TD="align: right"]18:48[/TD]
[TD]Pres[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I would like to add row if in column F will be (Pres) and in add row will be copy previous row.

Thank you
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
here is a vba solution

Code:
Sub mrmaso()
    Dim lr As Long, i As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    For i = lr To 1 Step -1
        If Range("F" & i) = "Pres" Then
            Range("F" & i + 1).EntireRow.Insert
        End If
    Next i
    Application.ScreenUpdating = True
    MsgBox "Completed"
End Sub

How to install your new code
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Press Alt+F11 to open the Visual Basic Editor
Choose Insert > Module
Edit > Paste the macro into the module that appeared
Close the VBEditor
Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)


To run the Excel VBA code:
Press Alt-F8 to open the macro list
Select a macro in the list
Click the Run button
 
Upvote 0
alan:

I looked at helping with this question but was not able to do this part of the users request:

I would like to add row if in column F will be (Pres)
and in add row will be copy previous row.

Sounds like he wants inserted row to be a copy of previous row. The one with Pres in column F

 
Upvote 0
@Answer. Thanks for pointing that out. I missed it the first time around. Here is some modified code

Code:
Option Explicit


Sub mrmaso()
    Dim lr As Long, i As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    For i = lr To 1 Step -1
        If Range("F" & i) = "Pres" Then
            Range("F" & i + 1).EntireRow.Insert
            Range("A" & i & ":G" & i).Copy
            Range("A" & i + 1).PasteSpecial xlPasteValues
        End If
    Next i
    Application.ScreenUpdating = True
    MsgBox "Completed"
End Sub
 
Upvote 0
alan:
Yours works now.

Here is how I was going to do mine.
I forgot about running script backwards when inserting row.
Code:
Option Explicit

Sub Mine()
    Dim lr As Long, i As Long
    lr = Range("F" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    For i = lr To 1 Step -1
        If Cells(i, "F") = "Pres" Then
            Rows(i).Offset(1).Insert
            Rows(i).Copy Rows(i + 1)
            
        End If
    Next i
    Application.ScreenUpdating = True
    MsgBox "Completed"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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