#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 460
Default insert row

I have rows in a large spreadsheet marked with an "x" based on criteria in an
"if" statement. I would like to insert a row above those marked rows. Is
there an easy way to accomplish this?

Then if possible, I would like to copy the values/formulas from an existing
row into the new inserted rows. Is it possible to automate this?

Thanks in advance for any help.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default insert row


This little sub will take into account that your search range will gro
as you insert rows. Let me know if it helps.

Sub FindXAndInsert()
Dim rSearch As Range
Dim rTarget As Range
Dim lRow As Long
Dim lRows As Long

' Assuming "SearchColumn" is a Named range
Set rSearch = Range("SearchColumn")

' Get number of Rows
lRows = rSearch.Rows.Count

' Loop through Rows
lRow = 1
Do While lRow <= lRows

' Set a range object
Set rTarget = rSearch.Range("A1").Offset(lRow - 1)

' Test for 'x'
If rTarget.Value = "x" Then
' Insert row above target
rTarget.EntireRow.Insert

' Mark inserted so you can see
rTarget.Offset(-1).Value = "Inserted From: "
rTarget.Address(False, False)

' Account for inserted row and the current target skippin
first
' If you don't skip the first row you just end up pushin
your selection down the sheet
If lRow 1 Then lRow = lRow + 1

' Bump row count up one to account for inserted ro
skipping first
If lRow 1 Then lRows = lRows + 1
End If

' Iterate Next Row
lRow = lRow + 1
Loop
End Su

--
tssop
-----------------------------------------------------------------------
tssopa's Profile: http://www.excelforum.com/member.php...fo&userid=1660
View this thread: http://www.excelforum.com/showthread.php?threadid=31465

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 460
Default insert row

This is exactly what I was looking for. It works great.

Thanks for your help.

"tssopa" wrote:


This little sub will take into account that your search range will grow
as you insert rows. Let me know if it helps.

Sub FindXAndInsert()
Dim rSearch As Range
Dim rTarget As Range
Dim lRow As Long
Dim lRows As Long

' Assuming "SearchColumn" is a Named range
Set rSearch = Range("SearchColumn")

' Get number of Rows
lRows = rSearch.Rows.Count

' Loop through Rows
lRow = 1
Do While lRow <= lRows

' Set a range object
Set rTarget = rSearch.Range("A1").Offset(lRow - 1)

' Test for 'x'
If rTarget.Value = "x" Then
' Insert row above target
rTarget.EntireRow.Insert

' Mark inserted so you can see
rTarget.Offset(-1).Value = "Inserted From: " &
rTarget.Address(False, False)

' Account for inserted row and the current target skipping
first
' If you don't skip the first row you just end up pushing
your selection down the sheet
If lRow 1 Then lRow = lRow + 1

' Bump row count up one to account for inserted row
skipping first
If lRow 1 Then lRows = lRows + 1
End If

' Iterate Next Row
lRow = lRow + 1
Loop
End Sub


--
tssopa
------------------------------------------------------------------------
tssopa's Profile: http://www.excelforum.com/member.php...o&userid=16605
View this thread: http://www.excelforum.com/showthread...hreadid=314655


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 row / insert column command buttons fairgreen Excel Worksheet Functions 1 October 29th 07 02:41 PM
How can I insert a date with an icon (calendar) insert Alfredo Mederico[_2_] Excel Discussion (Misc queries) 4 September 21st 07 01:20 AM
Can I auto insert a worksheet when I insert a value in a cell. iainc Excel Worksheet Functions 0 April 27th 06 08:37 AM
Insert Next? Or insert a variable number of records...how? Tom MacKay Excel Discussion (Misc queries) 0 April 20th 06 10:44 PM
Insert cell/format/text/fontsize and auto insert into header? Unfurltheflag Excel Programming 2 November 3rd 04 05:39 PM


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