Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
In excel, I have a list of around 100 words. If, for example, cell A3 = A4, I would want to insert a row in between A3 and A4. If not, I would want to keep searching. Anyone know how to write a macro to do such a thing? Thanks!

Hi,
Try this code.
Add this to a standard module.
To use it, Select the column of cells to be tested, than run the sub.
Private Sub IRowIFEq() Dim rngOrigin As Range Dim intRows As Integer Dim n As Integer On Error GoTo ErrHnd 'Call this sub with column selected 'If more than one column - error If Selection.Columns.Count > 1 Then MsgBox "Please select a single column of words" GoTo ErrHnd End If 'count number of rows in selection intRows = Selection.Rows.Count 'set origin of selection 'A1 is top left cell in selection not cell A1 on worksheet Set rngOrigin = Selection.Range("A1") 'loop through each cell, starting at the end 'no need to test last row For n = intRows - 1 To 1 Step -1 If rngOrigin.Offset(n, 0).Value = rngOrigin.Offset(n + 1, 0).Value Then 'if cells equal add row between matching cells rngOrigin.Offset(n + 1, 0).EntireRow.Insert Shift:=xlShiftDown End If Next n 'normal end of sub Exit Sub 'error handler ErrHnd: Err.Clear End SubInstead of selecting the cells each time you could use a named range, and basically replace 'Selection' in the code with Range("named range")
Regards
PS If posting about Excel again, may I suggest that you post in the Office Software forum

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |