PC Review


Reply
Thread Tools Rate Thread

Macro to insert rows

 
 
The Grinch
Guest
Posts: n/a
 
      19th Jul 2005

Hi All,

I have a sorted column of data, with repeated data. I want to write
macro that starts at the top of the column and goes down inserting
row when it finds a non duplicated piece of data. EG...

1
1
1
1<---------------Insert row
5
5
5
5<---------------Insert row
10
10<---------------Insert row
62
62
62

Any suggestion/comments would be appreciated.

CHEERS

The Grinc

--
The Grinc
-----------------------------------------------------------------------
The Grinch's Profile: http://www.excelforum.com/member.php...nfo&userid=244
View this thread: http://www.excelforum.com/showthread.php?threadid=38841

 
Reply With Quote
 
 
 
 
Don Guillett
Guest
Posts: n/a
 
      19th Jul 2005
try this. change p to your columns
Sub insertwheredup()
For i = Cells(Rows.Count, "P").End(xlUp).Row To 2 Step -1
If Cells(i, "P") <> Cells(i - 1, "p") Then Rows(i).Insert
Next i
End Sub


--
Don Guillett
SalesAid Software
(E-Mail Removed)
"The Grinch" <(E-Mail Removed)> wrote
in message news:(E-Mail Removed)...
>
> Hi All,
>
> I have a sorted column of data, with repeated data. I want to write a
> macro that starts at the top of the column and goes down inserting a
> row when it finds a non duplicated piece of data. EG...
>
> 1
> 1
> 1
> 1<---------------Insert row
> 5
> 5
> 5
> 5<---------------Insert row
> 10
> 10<---------------Insert row
> 62
> 62
> 62
>
> Any suggestion/comments would be appreciated.
>
> CHEERS
>
> The Grinch
>
>
> --
> The Grinch
> ------------------------------------------------------------------------
> The Grinch's Profile:

http://www.excelforum.com/member.php...fo&userid=2449
> View this thread: http://www.excelforum.com/showthread...hreadid=388416
>



 
Reply With Quote
 
Gord Dibben
Guest
Posts: n/a
 
      20th Jul 2005
Grinch

Assuming Column A holds the data.

Sub InsertRow_At_Change()
Dim i As Long
With Application
.Calculation = xlManual
.ScreenUpdating = False
End With
For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
If Cells(i - 1, 1) <> Cells(i, 1) Then _
Cells(i, 1).Resize(1, 1).EntireRow.Insert
Next i
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
End With
End Sub


Gord Dibben Excel MVP

On Tue, 19 Jul 2005 12:04:36 -0500, The Grinch
<(E-Mail Removed)> wrote:

>
>Hi All,
>
>I have a sorted column of data, with repeated data. I want to write a
>macro that starts at the top of the column and goes down inserting a
>row when it finds a non duplicated piece of data. EG...
>
>1
>1
>1
>1<---------------Insert row
>5
>5
>5
>5<---------------Insert row
>10
>10<---------------Insert row
>62
>62
>62
>
>Any suggestion/comments would be appreciated.
>
>CHEERS
>
>The Grinch


 
Reply With Quote
 
Odysseus
Guest
Posts: n/a
 
      21st Jul 2005

I've used the below (above) script which and adjusted it slighly to loo
at the second column. This works if the first (A) is poplated, howeve
if A is empty it stops working... any ideas?

PS, how do I make it insert 2 or more rows?


Sub InsertRow_At_Change()
Dim i As Long
With Application
.Calculation = xlManual
.ScreenUpdating = True
End With
For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
If Cells(i - 1, 2) <> Cells(i, 2) Then _
Cells(i, 1).Resize(1, 1).EntireRow.insert
Next i
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
End With
End Su

--
Odysseu
-----------------------------------------------------------------------
Odysseus's Profile: http://www.excelforum.com/member.php...fo&userid=1456
View this thread: http://www.excelforum.com/showthread.php?threadid=38841

 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      21st Jul 2005
You're still looking at Column A in your For... statement. Try:

For i = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1

To get more than one row, change the number of rows in your Resize
method, e.g., for 3 rows:

Cells(i, 1).Resize(3, 1).EntireRow.insert

In article <(E-Mail Removed)>,
Odysseus <(E-Mail Removed)>
wrote:

> I've used the below (above) script which and adjusted it slighly to look
> at the second column. This works if the first (A) is poplated, however
> if A is empty it stops working... any ideas?
>
> PS, how do I make it insert 2 or more rows?
>
>
> Sub InsertRow_At_Change()
> Dim i As Long
> With Application
> .Calculation = xlManual
> .ScreenUpdating = True
> End With
> For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
> If Cells(i - 1, 2) <> Cells(i, 2) Then _
> Cells(i, 1).Resize(1, 1).EntireRow.insert
> Next i
> With Application
> .Calculation = xlAutomatic
> .ScreenUpdating = True
> End With
> End Sub

 
Reply With Quote
 
Gord Dibben
Guest
Posts: n/a
 
      21st Jul 2005
Revised to work on column B and insert two rows.

To insert more than two rows, adjust the resize range as in .Resize(3, 2) or
(4, 2)

Sub InsertRow_At_Change()
Dim i As Long
With Application
.Calculation = xlManual
.ScreenUpdating = False
End With
For i = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
If Cells(i - 1, 2) <> Cells(i, 2) Then _
Cells(i, 2).Resize(2, 2).EntireRow.Insert
Next i
With Application
.Calculation = xlAutomatic
.ScreenUpdating = True
End With
End Sub


Gord

On Thu, 21 Jul 2005 09:10:35 -0500, Odysseus
<(E-Mail Removed)> wrote:

>
>I've used the below (above) script which and adjusted it slighly to look
>at the second column. This works if the first (A) is poplated, however
>if A is empty it stops working... any ideas?
>
>PS, how do I make it insert 2 or more rows?
>
>
>Sub InsertRow_At_Change()
>Dim i As Long
>With Application
>Calculation = xlManual
>ScreenUpdating = True
>End With
>For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
>If Cells(i - 1, 2) <> Cells(i, 2) Then _
>Cells(i, 1).Resize(1, 1).EntireRow.insert
>Next i
>With Application
>Calculation = xlAutomatic
>ScreenUpdating = True
>End With
>End Sub


 
Reply With Quote
 
Odysseus
Guest
Posts: n/a
 
      22nd Jul 2005

Thanks very muc

--
Odysseu
-----------------------------------------------------------------------
Odysseus's Profile: http://www.excelforum.com/member.php...fo&userid=1456
View this thread: http://www.excelforum.com/showthread.php?threadid=38841

 
Reply With Quote
 
The Grinch
Guest
Posts: n/a
 
      4th Aug 2005

Guys, thanks for all the help!! i've now come to a solution, thank
agai

--
The Grinc
-----------------------------------------------------------------------
The Grinch's Profile: http://www.excelforum.com/member.php...nfo&userid=244
View this thread: http://www.excelforum.com/showthread.php?threadid=38841

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to insert Rows marc747@excite.com Microsoft Excel Programming 4 17th Aug 2008 02:00 PM
Insert rows macro leonidas Microsoft Excel Programming 1 24th Jun 2006 01:38 AM
asking again, macro to insert rows =?Utf-8?B?THVrZQ==?= Microsoft Excel Worksheet Functions 12 18th Sep 2005 07:32 PM
Macro insert data, need to insert rows =?Utf-8?B?QnJhZA==?= Microsoft Excel Misc 1 28th May 2004 04:33 PM
Insert Rows Macro =?Utf-8?B?SmFzb24gV2F0dHM=?= Microsoft Excel Programming 19 5th Feb 2004 09:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:18 PM.