Insert a blank row If the condition meets

Jyotirmaya

Board Regular
Joined
Dec 2, 2015
Messages
216
Office Version
  1. 2019
Platform
  1. Windows
I have a excel sheet where there are many Balance rows

I want a macro that if in any Balance row there are negative value in FGHI column followed by any positive value a new row will be insert above the balance row.
 
What distinguishes a Balance row from other rows?
Is the word Balance in column A for all of them?
Is there a formula in a particular column for balance rows, but not for other rows?

Start with this:

Code:
Option Explicit

Sub InsertRowBelowNegativeEntriesInFGHI()

    Dim lLastColRow As Long
    Dim lLastRow As Long
    Dim lColIndex As Long
    Dim lRowIndex As Long
    Dim bIsNegative As Boolean
    Dim bIsBalanceRow As Boolean
    
    For lColIndex = 6 To 10
        lLastColRow = Cells(Rows.Count, lColIndex).End(xlUp).Row
        If lLastColRow > lLastRow Then lLastRow = lLastColRow
    Next
    
    For lRowIndex = lLastRow - 1 To 2 Step -1
        bIsBalanceRow = True  'Change this to set bIsBalanceRow to True if lRowIndex is a Balance Row
        bIsNegative = False
        For lColIndex = 6 To 10
            If Cells(lRowIndex, lColIndex).Value < 0 Then
                bIsNegative = True
                Exit For
            End If
        Next
        If bIsBalanceRow And bIsNegative Then
            If Cells(lRowIndex + 1, 6) & Cells(lRowIndex + 1, 7) & _
                Cells(lRowIndex + 1, 8) & Cells(lRowIndex + 1, 9) <> vbNullString Then
                'If FGHI in the row below the negative value is not blank
                Cells(lRowIndex + 1, 1).EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
            End If
        End If
        bIsBalanceRow = False
    Next
    
End Sub
 
Upvote 0
What distinguishes a Balance row from other rows?
Is the word Balance in column A for all of them?
Is there a formula in a particular column for balance rows, but not for other rows?

Start with this:

Code:
Option Explicit

Sub InsertRowBelowNegativeEntriesInFGHI()

    Dim lLastColRow As Long
    Dim lLastRow As Long
    Dim lColIndex As Long
    Dim lRowIndex As Long
    Dim bIsNegative As Boolean
    Dim bIsBalanceRow As Boolean
    
    For lColIndex = 6 To 10
        lLastColRow = Cells(Rows.Count, lColIndex).End(xlUp).Row
        If lLastColRow > lLastRow Then lLastRow = lLastColRow
    Next
    
    For lRowIndex = lLastRow - 1 To 2 Step -1
        bIsBalanceRow = True  'Change this to set bIsBalanceRow to True if lRowIndex is a Balance Row
        bIsNegative = False
        For lColIndex = 6 To 10
            If Cells(lRowIndex, lColIndex).Value < 0 Then
                bIsNegative = True
                Exit For
            End If
        Next
        If bIsBalanceRow And bIsNegative Then
            If Cells(lRowIndex + 1, 6) & Cells(lRowIndex + 1, 7) & _
                Cells(lRowIndex + 1, 8) & Cells(lRowIndex + 1, 9) <> vbNullString Then
                'If FGHI in the row below the negative value is not blank
                Cells(lRowIndex + 1, 1).EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
            End If
        End If
        bIsBalanceRow = False
    Next
    
End Sub

Hello pbornemeier, the word Balance is there in Column A & yes there is a formula for balance rows but not for others

I have a excel sheet where there are many Balance rows. In Column A there are Demand Collection Balance Rows, where Balance = Demand - Collection.

So there are multiple Balance Rows are there in that sheet. Column FGHIJ are inter related.

I want a macro that if in any Balance row there are negative value in FGHI column followed by any positive value a new row will be insert above the balance row.

for example

1. there is a negative value in F column -100 and positive value in either G or H or I or J column
2. there is a negative value in G column -100 and positive value in either H or I or J column
3.there is a negative value in I column -100 and positive value in either I or J column
4.there is a negative value in I column -100 and positive value in J column

In the above situation a blank row should be insert over the Balance row

If there is any positive value in F column but negative value in G column then row should not be insert.

example 40 -100 0 0 0 in FGHIJ column

Another example 40 -100 20 30 40

Here a new row should be inserted because the negative value is followed by positive values in the column

A new row should insert only if a negative value is followed by a positive value in in F G H I J column serially from left to right.

I want a VBA macro to insert rows above the Balance Row where the condition fulfills


Kindly check the image to understand better, in the blue marked columns a new row should insert as it fulfills the criteria but in the yellow marked columns it should not as the negative value doesn't followed by a positive value.

byadjustment.JPG


With your code a Blank row inserts but when it is followed by a zero then also it inserts a blank row, it should be insert when if any value having value 0+
 
Upvote 0
Code:
Option Explicit

Sub InsertRowBelowNegativeEntriesInFGHI()

    Dim lLastColRow As Long
    Dim lLastRow As Long
    Dim lColIndex As Long
    Dim lRowIndex As Long
    Dim bInsert As Boolean
    Dim bIsBalanceRow As Boolean
    Dim vFPos As Variant
    Dim vGPos As Variant
    Dim vHPos As Variant
    Dim vIPos As Variant
    Dim vJPos As Variant
    Dim sTrigger As String
    
    For lColIndex = 6 To 10
        lLastColRow = Cells(Rows.Count, lColIndex).End(xlUp).Row
        If lLastColRow > lLastRow Then lLastRow = lLastColRow
    Next
    
    For lRowIndex = lLastRow - 1 To 2 Step -1
        If UCase(Cells(lRowIndex, 1).Value) = "BALANCE" Then
            'On a BALANCE row
            bInsert = False
            vFPos = Cells(lRowIndex, "F").Value
            vGPos = Cells(lRowIndex, "G").Value
            vHPos = Cells(lRowIndex, "H").Value
            vIPos = Cells(lRowIndex, "I").Value
            vJPos = Cells(lRowIndex, "J").Value
            
            If vFPos < 0 And (vGPos > 0 Or vHPos > 0 Or vIPos > 0 Or vJPos > 0) Then bInsert = True: 'sTrigger = "F"
            If vGPos < 0 And (vHPos > 0 Or vIPos > 0 Or vJPos > 0) Then bInsert = True: 'sTrigger = "G"
            If vHPos < 0 And (vIPos > 0 Or vJPos > 0) Then bInsert = True: 'sTrigger = "H"
            If vIPos < 0 And (vJPos > 0) Then bInsert = True: 'sTrigger = "I"
            
            If bInsert Then
                Cells(lRowIndex + 1, 1).EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
                'Debug.Print lRowIndex, sTrigger
            End If
            
        End If
    Next
    
End Sub
 
Upvote 0

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