Consulting

Results 1 to 6 of 6

Thread: Trobule inserting a row through VBA

  1. #1
    VBAX Regular
    Joined
    Feb 2006
    Location
    Perth, Western Australia
    Posts
    8
    Location

    Solved: Trouble inserting a row through VBA

    Hi All,

    I'm having a bit of a problem with inserting an entire row when values in a row are identical. The problem is: I have a list of numbers in column 2 (sorted in ascending order). I need to insert a column between values that are different, whilst keeping the others "grouped" by blank rows.

    If anyone could provide any help, it would be much appreciated.

    Thanks.
    Last edited by Daniel3581; 02-27-2006 at 10:31 PM.

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi Daniel, welcome to VBAX!

    Do you think you could upload a small scaled down example of your file? Or give a few examples?

  3. #3
    VBAX Regular
    Joined
    Feb 2006
    Location
    Perth, Western Australia
    Posts
    8
    Location
    Hi Zack,

    Thanks for your response, but I have just managed to solve the problem.

    Thanks anyway.

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Ah, ok. Would you mind posting how you solved the problem, so that others who search the forums will get the benefit if your hard work?

    Don't forget, you can mark your own threads as solved by going to Thread Tools | Mark Thread Solved | Perform Action.

  5. #5
    VBAX Regular
    Joined
    Feb 2006
    Location
    Perth, Western Australia
    Posts
    8
    Location
    The code I used for this was :


    [vba]Sub InsertHeader()

    Dim X as long, EndRow as long
    Dim ID1 as string, ID2 as string

    Range(Cells(65536, 2), Cells(65536, 2).End.xlUp)).Select
    EndRow=Selection.Row

    ID1=Range("B2")
    ID2=""

    For X = 1 to EndRow
    If ID1 <> ID2 then
    Cells(X, 2).Select
    Selection.EntireRow.Insert
    ActiveCell.OffSet(1, 0).Select
    X=X+1
    End If
    ID1=ID2
    ActiveCell.Offset(1, 0).Select
    ID1 = ActiveCell.Value
    Next X

    End Sub[/vba]



    .....Now, to look for that solved button

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    How about using something like this instead ...

    [vba]Sub InsertHeader_x2()

    Dim X As Long, EndRow As Long
    Dim ID2 As String

    EndRow = Cells(Rows.Count, 2).End(xlUp).Row
    ID2=""

    For X = EndRow To 1 Step -1
    If Cells(X, 2) <> ID2 Then Cells(X, 2).EntireRow.Insert
    Next X

    End Sub[/vba]

    Or even better yet ..

    [vba]Sub InsertHeader_x3()

    Dim X As Long, ID2 As String
    For X = Cells(Rows.Count, 2).End(xlUp).Row To 1 Step -1
    If Cells(X, 2) <> ID2 Then Cells(X, 2).EntireRow.Insert
    Next X

    End Sub[/vba]

Posting Permissions

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