Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
ksp ksp is offline
external usenet poster
 
Posts: 1
Default 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


--
ksp
------------------------------------------------------------------------
ksp's Profile: http://www.excelforum.com/member.php...fo&userid=6267
View this thread: http://www.excelforum.com/showthread...hreadid=502787

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default 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   Report Post  
Posted to microsoft.public.excel.programming
ksp ksp is offline
external usenet poster
 
Posts: 1
Default Help With Insert Row Macro


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


--
ksp
------------------------------------------------------------------------
ksp's Profile: http://www.excelforum.com/member.php...fo&userid=6267
View this thread: http://www.excelforum.com/showthread...hreadid=502787

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.programming
ksp ksp is offline
external usenet poster
 
Posts: 1
Default Help With Insert Row Macro


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


--
ksp
------------------------------------------------------------------------
ksp's Profile: http://www.excelforum.com/member.php...fo&userid=6267
View this thread: http://www.excelforum.com/showthread...hreadid=502787



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Help With Insert Row Macro

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

Anita

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to insert a row after every 4th row Amandar097 Excel Discussion (Misc queries) 2 August 20th 09 04:47 AM
Macro to Insert Current Date into cell - Macro to "Save As" Guy[_2_] Excel Worksheet Functions 4 December 12th 08 08:20 PM
make a macro to insert a macro mithu Excel Discussion (Misc queries) 6 March 20th 07 06:04 PM
Insert Row Macro Help ksp Excel Programming 2 January 16th 06 09:39 AM
Insert A Row Macro Mark Williams New Users to Excel 2 September 30th 05 07:03 PM


All times are GMT +1. The time now is 09:01 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"