Add row above based on specific cell criteria.

Chad8

New Member
Joined
Nov 27, 2016
Messages
3

I've hit a wall it seems I am wondering if anyone can help meet out. I'm building a Excel macro but stuck on how to accomplish the following procedure.

Step 1:
If any cell(s) in “J” column is greater than or equal to the value in Cell M4, Insert 1 new row above all cells that meet the criteria.

Step 2: Copy and move all cells that meet criteria in step 1: up 1 and to the right 1 (paste in new row). Then enter 0 in the in the original cell that it was copied and moved from.

<strike></strike>
I would post a screenshot to better explain however this site is not exactly newbie friendly I see
.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

I've hit a wall it seems I am wondering if anyone can help meet out. I'm building a Excel macro but stuck on how to accomplish the following procedure.

Step 1:
If any cell(s) in “J” column is greater than or equal to the value in Cell M4, Insert 1 new row above all cells that meet the criteria.

Step 2: Copy and move all cells that meet criteria in step 1: up 1 and to the right 1 (paste in new row). Then enter 0 in the in the original cell that it was copied and moved from.

<strike></strike>
I would post a screenshot to better explain however this site is not exactly newbie friendly I see
.

try this

****** NOTE change the set ws=sheets("Chad8") to your sheet name

Code:
Sub Chad8()
Dim ws As Worksheet
Dim lngrow As Long, lnghead As Long
Dim rng As Range
Dim i As Variant, j As Variant

    Set ws = Sheets("Chad8")
    With ws
        j = ws.Range("M4").Value
        lnghead = ws.Range("J" & ws.Rows.Count).End(xlUp).End(xlUp).Row
        lngrow = cells.Find(What:="*", _
            after:=cells(1), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Row

        For i = lngrow To lnghead Step -1
            Set rng = ws.cells(i, 10)
            rng.Select
            If rng.Value > j Then
                rng.EntireRow.Insert
                rng.Offset(-1, 1).Value = rng.Value
                rng.Value = 0
            End If
        Next
    End With
End Sub
 
Upvote 0
Thanks RCbricker you are most helpful. The code worked with just a couple minor bugs.

1. the cells that contain values equal to the value in cell "M4" did not transfer over.
2. the 'J' column title in cell J1 changes from "SD" and gets transferred over. I would prefer the title to remain as a title.

Glad I found this site. Learning VBA is like learning a new language but something I am very interested in doing.
 
Upvote 0
Thanks RCbricker you are most helpful. The code worked with just a couple minor bugs.

1. the cells that contain values equal to the value in cell "M4" did not transfer over.
2. the 'J' column title in cell J1 changes from "SD" and gets transferred over. I would prefer the title to remain as a title.

Glad I found this site. Learning VBA is like learning a new language but something I am very interested in doing.

Anyone?
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,036
Members
449,062
Latest member
mike575

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