+ Reply to Thread
Results 1 to 6 of 6

Help With Insert Row Macro

  1. #1
    Registered User
    Join Date
    02-18-2004
    Posts
    71

    Help With Insert Row Macro

    I need some help with a macro to insert one row at a point where there is text in a particular column.

    The worksheet has multiple columns, but the two relevant ones are headed Category (Column A) and Tasks (Column D). Each Category description has at least one task, but can have up to 20. I have been using the macro below to insert one row before each new category. However, what I have discovered is that this is actual inserting one row after the category description. This is fine as long as there is only ever one task, but this is not the case.

    Everything that I have tried so far seems to only cause the macro to go into a loop

    Any input / help would be appreciated

    Thanks

    Karen

    Sub InsertRows()
    With Range("A10:A2498")
    On Error Resume Next
    Set C = .Find(What:="*", LookIn:=xlValues)
    If Not C Is Nothing Then
    firstAddress = C.Address
    Do
    C.Offset(1, 0).EntireRow.Insert
    Set C = .FindNext(C)
    Loop While Not C Is Nothing And C.Address <> firstAddress
    End If
    End With
    End Sub

  2. #2
    Anita
    Guest

    Re: Help With Insert Row Macro

    Hi Karen,

    The reason you were getting stuck in the loop is the loop while check
    was looking for something in the firstAddress in which you found text.
    But once you found it the first time, you shift it down, so the loop
    will never find anything in that cell again. Try the following:

    Sub InsertRows()
    With Range("A10:A2498")
    On Error Resume Next
    Set C = .Find(What:="*", LookIn:=xlValues)
    If Not C Is Nothing Then
    firstaddress = C.Offset(1, 0).Address
    Do
    C.Offset(-1, 0).EntireRow.Insert
    Set C = .FindNext(C)
    Loop While Not C Is Nothing And C.Address > firstaddress
    End If
    End With
    End Sub

    Hope this works for you,
    Anita


  3. #3
    Registered User
    Join Date
    02-18-2004
    Posts
    71
    Hi Anita

    It sort of worked. I am no longer getting the endless loop, but I suspect that it is still looping as it is now inserting 6 rows between each - any ideas why?

    Thanks

    Karen

  4. #4
    Registered User
    Join Date
    02-18-2004
    Posts
    71
    Hi Anita

    Thought you might like to know that I played around with the changes you suggested (see below) and it now works perfectly - thanks heaps for your input

    Karen


    Sub InsertRows()
    With Range("A10:A2499")
    On Error Resume Next
    Set C = .Find(What:="*", LookIn:=xlValues)
    If Not C Is Nothing Then
    firstaddress = C.Offset(1, 0).Address
    Do
    C.Offset(0, 0).EntireRow.Insert
    Set C = .FindNext(C)
    Loop While Not C Is Nothing And C.Address <> firstaddress
    End If

    End With

    End Sub

  5. #5
    Anita
    Guest

    Re: Help With Insert Row Macro

    Oops. On inserting the row I was using -1 rows when I shouldn't have
    been offsetting it at all. Here's the Do Loop with a change to line 2.

    Do
    c.EntireRow.Insert
    Set c = .FindNext(c)
    Loop While Not c Is Nothing And c.Address > firstaddress


  6. #6
    Anita
    Guest

    Re: Help With Insert Row Macro

    Thanks so much for letting me know that it worked out for you!

    Anita


+ 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