+ Reply to Thread
Results 1 to 3 of 3

Insert Row at Variable Location

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    67

    Insert Row at Variable Location

    Hi Folks,

    I had a quick question for something that should be simple but I can't figure it out.

    I have a column of sorted values, it goes

    A
    A
    A
    A
    A
    B
    B
    B
    B
    B

    and I want to insert a row to separate the A's from the B's. But there will be a random number of A's.

    I can figure out how to count where the A's end. But I can't figure out how to insert a row. Any suggestions? I tried the two below but they don't seem to work... Many thanks,

    The for section is counting the A's, and it works, it's what comes next that's giving me a headache!

    EDIT: Solved, it actualy was really simple. If anyone has the same problem, please see code below
    Dim test As Long
    
    test = 1
    
       For i = 1 To tempcount
            If Range("C" & i).Value = "A" Then
            forcount = forcount + 1
            End If
        Next
    
      Rows(tempcount).Insert
    Last edited by agentred; 12-03-2009 at 10:18 PM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Insert Row at Variable Location

    Hi agentred
    try
    Option Explicit
    Sub ptest()
    Dim a#, i#
     a = Range("a" & Rows.Count).End(xlUp).Row - 1  '.Resize(, 1)
               For i = a - 1 To 1 Step -1
                          If Cells(i, 1).Value <> Cells(i + 1, 1).Value Then
               Cells(i + 1, 1).Insert Shift:=xlDown
                End If
                   Next
           End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Insert Row at Variable Location

    Or
    for the entire row
    Row(i + 1).Insert Shift:=xlDown
    dont forget to mark the post solve

+ 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