+ Reply to Thread
Results 1 to 8 of 8

A Macro to Insert Rows

Hybrid View

  1. #1
    Registered User
    Join Date
    06-26-2007
    Posts
    3

    A Macro to Insert Rows

    Hey guys,

    So here's the scenario.

    I have a list that looks sort of like this:

    EXAMPLE A XXXXXXX EXAMPLE A
    EXAMPLE A XXXXXXX EXAMPLE A
    EXAMPLE B XXXXXXX EXAMPLE B
    EXAMPLE B XXXXXXX EXAMPLE B
    EXAMPLE C XXXXXXX EXAMPLE C
    EXAMPLE C XXXXXXX EXAMPLE C

    The list about 1900 rows long. What I need to do is to find a way to automatically seperate and group each "Example" by name, that is, placing a row inbetween Example A and Example B, B and C so it looks like this:

    EXAMPLE A XXXXXXX EXAMPLE A
    EXAMPLE A XXXXXXX EXAMPLE A

    EXAMPLE B XXXXXXX EXAMPLE B
    EXAMPLE B XXXXXXX EXAMPLE B

    EXAMPLE C XXXXXXX EXAMPLE C
    EXAMPLE C XXXXXXX EXAMPLE C

    And I need to do this every month when I run a certain report. Is there a way to have Excel automatically insert the rows for me? I doubt there is an "Insert Row" function, but is there possibly a way to do it in VBA? I've tried using macros, but that doesn't seem to do the trick.

    Thanks so much!

  2. #2
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Trustnduzt,

    What exactly will you be breaking on. I don't think you will be using the words EXAMPLE A, EXAMPLE B, EXAMPLE C. It is not very difficult to insert a row, but we need to code it based on the exact type of value that will be in Column A (I'm assuming).

    In other words, I can easily go out there and code to grab the A out of the string EXAMPLE A, but that isn't really going to do you any good, if EXAMPLE A is not what is going to appear in your live data.
    Sincerely,
    Jeff

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    This breaks on the value in column A.

    Sub aaa()
      For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        If Cells(i - 1, 1) <> Cells(i, 1) Then
          Cells(i, 1).EntireRow.Insert
        End If
      Next i
    End Sub

    rylo

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Does this do what you want?
    Sub TND()
        Dim i As Long
        For i = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If Cells(i, "A") <> Cells(i - 1, "A") _
                And Cells(i, "A") <> "" _
                And Cells(i - 1, "A") <> "" Then
                Rows(i).Insert
            End If
        Next
    End Sub

  5. #5
    Registered User
    Join Date
    06-26-2007
    Posts
    3
    Thank you so much for that code, it works like a charm! Sorry I didn't provide a better template to work with, but that macro seems to work just fine!

    Thanks again!

  6. #6
    Registered User
    Join Date
    05-07-2005
    Posts
    9
    I have the following script that works fine for a single row insert on column b but would like to make it adjustable to insert anywhere from 2 to 16 rows.

    Thanks

  7. #7
    Registered User
    Join Date
    05-07-2005
    Posts
    9
    Sorry forgot to include sample script.

    Sub HTH()
    Dim i As Long
    Dim mycnt As Long
    mycnt = Worksheets("Sheet15").Range(Range("b2"), Range("B65536").End(xlUp)).Rows.Count
    For i = mycnt To 1 Step -1
    If Cells(i, 2).Value <> Cells(i + 1, 2).Value Then
    Range(Cells(i, 2).Offset(1, -1), Cells(i, 2).Offset(1, 6)).Insert Shift:=xlDown
    End If
    Next i

    End Sub

+ 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