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
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
5,860
Office Version
  1. 2019
Platform
  1. Windows
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,851
Office Version
  1. 2013
Platform
  1. Windows
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

 

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
5,860
Office Version
  1. 2019
Platform
  1. Windows
@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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,851
Office Version
  1. 2013
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,336
Messages
5,528,104
Members
409,802
Latest member
joeino

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top