You are here:

Excel/insert an additional row if A3>B2

Advertisement


Question
VBA
VBA  
Hi Jan,

Sorry, it is me again.

The script seems to be inserting row at one row higher than it should be. The yellow boxes indicate that there should not be an inserted row and the red ones indicate there should be a row inserted, just below it.

What i meant was (for example, see attached image), Row 116 & 117 should interchange with each other, likewise for Row 138 & 139, and so on.

Thank you very much.

Gerard Lee

Answer
A different approach then.

In cell C3, enter this formula:

=AND(A3>B2,A3<>"",B2<>"")

Copy this cell down to match the number of rows in both A and B.

Now adjust the macro to:

Option Explicit

Sub InsertARowOrNot()
   Dim lCt As Long
   Dim oCell As Range
   Set oCell = Range("C3")
   Do
       If oCell.Offset(lCt).Value And Not oCell.Offset(lCt).Value = "" Then
         oCell.Offset(lCt + 1).EntireRow.Insert
       End If
       lCt = lCt + 1
   Loop Until oCell.Offset(lCt).Value = "" And oCell.Offset(lCt + 1).Value = ""
End Sub  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jan Karel Pieterse

Expertise

Excel and Excel/VBA questions

Experience

Excel MVP

Organizations
Self employed Excel developer

Education/Credentials
Bachelor in Chemical Engineering

Awards and Honors
Microsoft MVP award since 2002

Past/Present Clients
Shell, Fortis bank, ABN-AMRO bank, Morgan Stanley, ...

©2016 About.com. All rights reserved.