+ Reply to Thread
Results 1 to 3 of 3

Insert rows

Hybrid View

  1. #1
    Registered User
    Join Date
    03-31-2008
    Posts
    15

    Insert rows

    I am trying to create a macro that will insert a row if the first character in a cell is different to the cell below. I've tried various options but cannot get the macro to see the first characters of the cells.

    Example:

    aaa
    abb
    baa
    bbb
    ccc
    ccd
    ddd
    dde

    would become:

    aaa
    abb

    baa
    bbb

    ccc
    ccd

    ddd
    dde

    Thanks for any help.

  2. #2
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    You can try to use this macro:
    Sub Macro1()
       Dim lastRow As Long
       Dim r As Long
       Dim myColumn As String
       
       'here works on column 'A'
       myColumn = "a"
       
       lastRow = Cells(Rows.Count, myColumn).End(xlUp).Row
       For r = lastRow To 2 Step -1
          If Left(Cells(r, myColumn), 1) <> Left(Cells(r - 1, myColumn), 1) Then
             Rows(r).Insert
          End If
       Next
    End Sub
    Regards,
    Antonio

  3. #3
    Registered User
    Join Date
    03-31-2008
    Posts
    15
    Exactly what I needed, thanks so much for that.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1