VBA to insert rows above a negative number

jaygil

New Member
Joined
Feb 5, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to write a macro but it is beyond my skills at the moment. I want to be able to search a specific row in my spreadsheet to find any negative numbers, if a negative number is found I then want to insert 3 rows above the row that the negative number is found in.

any advice or help would be appreciated, thanks

Examples of the data and the desired result is attached below.

1675658990932.png



1675659005663.png
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Does your data contain formulas and/or formatting ?
Using an array will be faster but it will lose those features if they exist in your spreadsheet.
 
Upvote 0
MAybe this way, depending on the questions stated by @Alex Blakenburg
VBA Code:
Sub MM1()
Dim lr As Long, r As Long, lc As Integer, c As Integer
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
For r = lr To 3 Step -1
    For Cell = 1 To lc
        If Cells(r, Cell).Value < 0 Then
            Range(Rows(r), Rows(r + 2)).Insert
            Exit For
        End If
    Next Cell
Next r
End Sub
 
Upvote 0
Thanks for the responses, I will attempt that one Michael.

There is important formulas in the spreadsheet already and also more to come. The usual spreadsheet I will apply this to is usually much longer and also has further steps after this process.

Was just trying to break it down because i was stuck at this stage and continue to develop further.
 
Upvote 0
I'm not in Excel at the moment.
But the code provided can be shortened AND speeded up considerably if the criteria is limited to col G.
@Alex Blakenburg might step in and modify to suit
 
Upvote 0
Here is @Michael M's solution modified so that in only checks Column G for the negative per your post#6 clarification.

VBA Code:
Sub MM1_mod()
Dim lr As Long, r As Long, lc As Integer, c As Integer
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
For r = lr To 3 Step -1
    If Cells(r, "G").Value < 0 Then
        Range(Rows(r), Rows(r + 2)).Insert
    End If
Next r
End Sub
 
Upvote 0
Solution
Thanks again, I'm not on excel right now either but that looks logical to me I'll try it again first thing tomorrow 😀
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,752
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