Computing.Net > Forums > Windows Vista > Coding VBA

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Coding VBA

Reply to Message Icon

Name: phoenix197927
Date: November 8, 2009 at 15:48:28 Pacific
OS: Windows Vista
Subcategory: General
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Humar
Date: November 8, 2009 at 18:18:13 Pacific
Reply:

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 Sub

Instead 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


0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Coding VBA

VBA code excel 2007 move down one row www.computing.net/answers/windows-vista/vba-code-excel-2007-move-down-one-row/4905.html

Code 10 www.computing.net/answers/windows-vista/code-10/1196.html

Vista Retailing Color Coded www.computing.net/answers/windows-vista/vista-retailing-color-coded/102.html