#1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 7
Default Inserting a Row

Using VBA in a macro, I want to insert a new row everytime the data changes
in the rows of column A. Example
A1=Chevy
A2=Chevy
A3=Dodge
A4=Dodge.

A new row would be inserted between Chevy and Dodge when I run the macro.
Any help would be appreciate, thanks inadvance.
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 7,247
Default Inserting a Row

Here's some VBA code that will do what you want. It tests the values in
column A to see if a new row should be inserted. Note that it works its way
upwards in the sheet, starting at the last used cell in column A and move up
to row 1.

Sub AAA()
Dim LastRow As Long
Dim FirstRow As Long
Dim RowNdx As Long
Dim Temp As Variant

LastRow = Cells(Rows.Count, "A").End(xlUp).Row
FirstRow = 1 'adjust as necessary
Temp = Cells(LastRow, "A").Value
For RowNdx = LastRow To FirstRow Step -1
If Cells(RowNdx, "A").Value < Temp Then
Temp = Cells(RowNdx, "A").Value
Rows(RowNdx + 1).Insert
End If
Next RowNdx
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)d


"Curtd" wrote in message
...
Using VBA in a macro, I want to insert a new row everytime the data
changes
in the rows of column A. Example
A1=Chevy
A2=Chevy
A3=Dodge
A4=Dodge.

A new row would be inserted between Chevy and Dodge when I run the macro.
Any help would be appreciate, thanks inadvance.


  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 11,501
Default Inserting a Row

Hi,

Try this

Sub marine()
lastrowA = ActiveSheet.Range("A65536").End(xlUp).Row
For x = lastrowA To 2 Step -1
Cells(x, 1).Select
If Cells(x, 1).Value < Cells(x - 1, 1).Value Then
Selection.EntireRow.Insert
End If
Next
End Sub

Mike

"Curtd" wrote:

Using VBA in a macro, I want to insert a new row everytime the data changes
in the rows of column A. Example
A1=Chevy
A2=Chevy
A3=Dodge
A4=Dodge.

A new row would be inserted between Chevy and Dodge when I run the macro.
Any help would be appreciate, thanks inadvance.

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,345
Default Inserting a Row

Try something like:

Sub InsertIt()
Dim LastRow As Long
Dim x As Long

LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For x = LastRow To 3 Step -1
If Cells(x, 1).Value < Cells(x - 1, 1).Value Then
If Cells(x, 1).Value < "" Then
Cells(x, 1).EntireRow.Insert shift:=xlDown
End If
End If
Next x

End Sub


--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


Replace @mailinator.com with @tiscali.co.uk


"Curtd" wrote in message
...
Using VBA in a macro, I want to insert a new row everytime the data
changes
in the rows of column A. Example
A1=Chevy
A2=Chevy
A3=Dodge
A4=Dodge.

A new row would be inserted between Chevy and Dodge when I run the macro.
Any help would be appreciate, thanks inadvance.



  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 2,345
Default Inserting a Row

I thought that the line:

If Cells(x, 1).Value < "" Then

would stop another blank row being inserted if the Macro is run for a second
time but it does not. Better to use soemthing like:

Sub InsertIt()
Dim LastRow As Long
Dim x As Long

LastRow = Cells(Rows.Count, 1).End(xlUp).Row

Application.ScreenUpdating = False

For x = LastRow To 3 Step -1
If Cells(x, 1).Value < Cells(x - 1, 1).Value Then
If Cells(x, 1).Value < "" Then
If Cells(x - 1, 1).Value < "" Then
Cells(x, 1).EntireRow.Insert Shift:=xlDown
End If
End If
End If
Next x

Application.ScreenUpdating = True

End Sub


--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


Replace @mailinator.com with @tiscali.co.uk


"Sandy Mann" wrote in message
...
Try something like:

Sub InsertIt()
Dim LastRow As Long
Dim x As Long

LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For x = LastRow To 3 Step -1
If Cells(x, 1).Value < Cells(x - 1, 1).Value Then
If Cells(x, 1).Value < "" Then
Cells(x, 1).EntireRow.Insert shift:=xlDown
End If
End If
Next x

End Sub


--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


Replace @mailinator.com with @tiscali.co.uk


"Curtd" wrote in message
...
Using VBA in a macro, I want to insert a new row everytime the data
changes
in the rows of column A. Example
A1=Chevy
A2=Chevy
A3=Dodge
A4=Dodge.

A new row would be inserted between Chevy and Dodge when I run the macro.
Any help would be appreciate, thanks inadvance.








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
Inserting? SteveB Excel Discussion (Misc queries) 1 May 18th 07 05:16 AM
Inserting a tab [email protected] Excel Discussion (Misc queries) 4 January 12th 07 05:29 PM
inserting zero dfg Excel Worksheet Functions 1 May 22nd 06 12:28 PM
inserting a row.. angelb88 Excel Discussion (Misc queries) 1 April 5th 05 05:40 PM
inserting a row erin m Excel Discussion (Misc queries) 1 January 13th 05 05:51 PM


All times are GMT +1. The time now is 10:25 PM.

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

About Us

"It's about Microsoft Excel"