Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Addition of rows in table with loop

I have the following code to help me insert a row in between groups o
similar data.
However, it doesn't work after the first row has been inserted. Coul
someone tell me what's wrong with my code?

For Each c In ActiveWorkbook.Worksheets("CIT
GF").Range(myrange.Address)

If Cells(c.Row, 9).Value < Cells(c.Row + 1, 9).Value Then
'Address = c.Address
c.EntireRow.Offset(1, 0).Select
Selection.Insert Shift:=xlDown
'Range("c.Row,c.column").Offset(1, 0).Select
c = c.Offset(3, 0)
End If
Next c


Regard

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Addition of rows in table with loop

Desmond,

Change

c = c.Offset(3, 0)

To

Set c = c.Offset(3, 0)


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"desmondleow " wrote
in message ...
I have the following code to help me insert a row in between

groups of
similar data.
However, it doesn't work after the first row has been inserted.

Could
someone tell me what's wrong with my code?

For Each c In ActiveWorkbook.Worksheets("CITI
GF").Range(myrange.Address)

If Cells(c.Row, 9).Value < Cells(c.Row + 1, 9).Value Then
'Address = c.Address
c.EntireRow.Offset(1, 0).Select
Selection.Insert Shift:=xlDown
'Range("c.Row,c.column").Offset(1, 0).Select
c = c.Offset(3, 0)
End If
Next c


Regards


---
Message posted from http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Addition of rows in table with loop

Hi...

It still doesn't work. It keeps inserting new rows and doesn't move
down to the next line of data. Is there any reason why?

Regards,
Desmond


---
Message posted from http://www.ExcelForum.com/

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Addition of rows in table with loop

The problem is that when you insert the row you change the are being
processed, and the inserted row becomes part of the next test. This never
matches, so you insert another row at the same point, and so on, until you
get an error.

What you need to do is work from the bottom up, like so

With ActiveWorkbook.Worksheets("CITIGF").Range(myrange)
For i = Cells(Rows.Count, .Column).End(xlUp).Row To .Row + 1 Step -1
If Cells(i, 9).Value < Cells(i - 1, 9).Value Then
'Address = c.Address
Cells(i, 1).EntireRow.Insert Shift:=xlDown
End If
Next i
End With


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"desmondleow " wrote in message
...
I have the following code to help me insert a row in between groups of
similar data.
However, it doesn't work after the first row has been inserted. Could
someone tell me what's wrong with my code?

For Each c In ActiveWorkbook.Worksheets("CITI
GF").Range(myrange.Address)

If Cells(c.Row, 9).Value < Cells(c.Row + 1, 9).Value Then
'Address = c.Address
c.EntireRow.Offset(1, 0).Select
Selection.Insert Shift:=xlDown
'Range("c.Row,c.column").Offset(1, 0).Select
c = c.Offset(3, 0)
End If
Next c


Regards


---
Message posted from http://www.ExcelForum.com/



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
How can I loop through a the values in multiple rows EMarre Excel Discussion (Misc queries) 6 May 13th 10 01:50 PM
Loop relative rows PST Excel Discussion (Misc queries) 1 May 20th 07 12:23 PM
PIVOT TABLE Custom Column addition to calculate sum and Percentage [email protected] Excel Discussion (Misc queries) 0 May 8th 07 11:04 PM
For Each Loop with Pivot Table RestlessAde Excel Discussion (Misc queries) 2 August 8th 05 05:42 PM
How do I delete rows and columns in With With End Loop? Bob Benjamin Excel Programming 3 November 16th 03 12:26 AM


All times are GMT +1. The time now is 02:29 PM.

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"