Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 136
Default VBA code to insert rows in table

I have a table (list) of property addresses sorted by location, which is the
left-most column in the table. Sometimes, locations happen to be rather long.
To save space on the printed copy, I would like to set the locations column
out of the print range, and, in order to break the table according to
locations, insert a new table row just above any row which starts a new
location.
I'm in need for good VBA code which will 1) scan the Locations column, top
to bottom, for a change in location, and when such a change is detected 2)
insert a new table row just above that row 3) Copy the new location text and
4) paste it one row above and one column to the right from the copied cell.
Could any one please help me with this code?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default VBA code to insert rows in table

Public Sub ProcessData()
Dim iLastRow As Long
Dim i As Long

With ActiveSheet

iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = iLastRow To 2 Step -1
If .Cells(i, "A").Value < .Cells(i - 1, "A") Then
.Rows(i).Insert
.Cells(i, "B").Value = .Cells(i + 1, "A").Value
End If
Next i
.Rows(1).Insert
.Cells(1, "B").Value = .Cells(2, "A").Value

End With

End Sub


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Tevuna" wrote in message
...
I have a table (list) of property addresses sorted by location, which is
the
left-most column in the table. Sometimes, locations happen to be rather
long.
To save space on the printed copy, I would like to set the locations
column
out of the print range, and, in order to break the table according to
locations, insert a new table row just above any row which starts a new
location.
I'm in need for good VBA code which will 1) scan the Locations column, top
to bottom, for a change in location, and when such a change is detected 2)
insert a new table row just above that row 3) Copy the new location text
and
4) paste it one row above and one column to the right from the copied
cell.
Could any one please help me with this code?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VBA code to insert rows in table

try this

Sub moveaddr()

Const AddressCol = "A"

RowCount = 1
Do While Not IsEmpty(Cells(RowCount + 1, AddressCol))

If Cells(RowCount, AddressCol) < _
Cells(RowCount + 1, AddressCol) Then

Cells(RowCount + 1, AddressCol).EntireRow.Insert
RowCount = RowCount + 1
Cells(RowCount, AddressCol).Offset(0, 1) = _
Cells(RowCount + 1, AddressCol)
End If

RowCount = RowCount + 1
Loop

End Sub

"Tevuna" wrote:

I have a table (list) of property addresses sorted by location, which is the
left-most column in the table. Sometimes, locations happen to be rather long.
To save space on the printed copy, I would like to set the locations column
out of the print range, and, in order to break the table according to
locations, insert a new table row just above any row which starts a new
location.
I'm in need for good VBA code which will 1) scan the Locations column, top
to bottom, for a change in location, and when such a change is detected 2)
insert a new table row just above that row 3) Copy the new location text and
4) paste it one row above and one column to the right from the copied cell.
Could any one please help me with this code?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 136
Default VBA code to insert rows in table

Many thanks to you, Joel.
I've edited the code to reflect my actual situation and to add some nice
formatting, it printes beautifully.
Here is the code: (Maybe some redundancies, I'm not experienced, yet)

Sub breakBorough()

Const AddressCol = "b"

RowCount = 4
Do While Not IsEmpty(Cells(RowCount + 1, AddressCol))

If Cells(RowCount, AddressCol) < _
Cells(RowCount + 1, AddressCol) Then

Cells(RowCount + 1, AddressCol).EntireRow.Insert
RowCount = RowCount + 1
Cells(RowCount + 1, AddressCol).Select
Selection.Copy
ActiveCell.Offset(-1, 0).Range("A1:O1").Select
Selection.ClearContents
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = -0.249977111117893
.PatternTintAndShade = 0
End With
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Selection.Font
.Name = "Calibri"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.Copy
ActiveCell.Offset(-1, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False



End If

RowCount = RowCount + 1
Loop

End Sub

"Joel" wrote:

try this

Sub moveaddr()

Const AddressCol = "A"

RowCount = 1
Do While Not IsEmpty(Cells(RowCount + 1, AddressCol))

If Cells(RowCount, AddressCol) < _
Cells(RowCount + 1, AddressCol) Then

Cells(RowCount + 1, AddressCol).EntireRow.Insert
RowCount = RowCount + 1
Cells(RowCount, AddressCol).Offset(0, 1) = _
Cells(RowCount + 1, AddressCol)
End If

RowCount = RowCount + 1
Loop

End Sub

"Tevuna" wrote:

I have a table (list) of property addresses sorted by location, which is the
left-most column in the table. Sometimes, locations happen to be rather long.
To save space on the printed copy, I would like to set the locations column
out of the print range, and, in order to break the table according to
locations, insert a new table row just above any row which starts a new
location.
I'm in need for good VBA code which will 1) scan the Locations column, top
to bottom, for a change in location, and when such a change is detected 2)
insert a new table row just above that row 3) Copy the new location text and
4) paste it one row above and one column to the right from the copied cell.
Could any one please help me with this code?

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
Insert rows in linked table that are added to secondary table Ida LaValley Excel Programming 0 August 24th 06 09:19 PM
Insert rows in linked table that are added to secondary table Ida LaValley Excel Discussion (Misc queries) 2 August 21st 06 10:52 PM
Insert two rows in one line of code? quartz[_2_] Excel Programming 4 November 10th 05 04:31 PM
Editing code to insert two rows achidsey Excel Programming 2 August 30th 05 03:14 PM
Help with some code to insert rows Gareth[_3_] Excel Programming 2 December 11th 03 10:10 PM


All times are GMT +1. The time now is 12:34 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"