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:

A
DSH09306

B
1002444978

C
40

D
499

E​
6:33
F G
ZSH093251002445429401 5409:45Pres-
DBS01135100244482640100010:41Pres1
DBS01136100244482740100011:50Pres
DBS011361002444978401 00012:57
DSH093351002444983402 66616:01Pres
DSH093051002444977401 49917:41Pres
DSH093061002444978401 00018:48Pres

<colgroup><col><col><col span="3"><col><col></colgroup><tbody>
</tbody>

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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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