Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 313
Default Insert Rows using VBA

I am using the following code to try and find a "1" in column "R". Whenever I
find one I want to insert a row. With the code I have here I find the "1" in
row "4" but it inserts a row at row "48", then row "50, etc. What do I need
to do to insert a row at rows "4", "15",


LoopCol2 = 1
LoopRow2 = 4

While Not IsEmpty(Worksheets("Break & Lunch Schedule").Cells(Looprow2,
LoopCol2))etcf.
With wsPh
If wsPh.Range("r" & Looprow2 + 0) = 1 Then wsPh.Range("r"
& Looprow2).End(xlDown).Select
Selection.EntireRow.Insert
Looprow2 = Looprow2 + 1
' ETop = ETop + 1
Else
Looprow2 = Looprow2 + 1
' ETop = ETop + 1
End If
End With
Wend
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default Insert Rows using VBA

Tony,

Maybe a slightly different approach

Sub insert_row()
Sht = "Break & Lunch Schedule"
lastrow = Sheets(Sht).Cells(Rows.Count, "R").End(xlUp).Row
For x = lastrow To 1 Step -1
If Sheets(Sht).Cells(x, 18) = 1 Then
Sheets(Sht).Rows(x).EntireRow.Insert
End If
Next
End Sub


Mike

"Tony" wrote:

I am using the following code to try and find a "1" in column "R". Whenever I
find one I want to insert a row. With the code I have here I find the "1" in
row "4" but it inserts a row at row "48", then row "50, etc. What do I need
to do to insert a row at rows "4", "15",


LoopCol2 = 1
LoopRow2 = 4

While Not IsEmpty(Worksheets("Break & Lunch Schedule").Cells(Looprow2,
LoopCol2))etcf.
With wsPh
If wsPh.Range("r" & Looprow2 + 0) = 1 Then wsPh.Range("r"
& Looprow2).End(xlDown).Select
Selection.EntireRow.Insert
Looprow2 = Looprow2 + 1
' ETop = ETop + 1
Else
Looprow2 = Looprow2 + 1
' ETop = ETop + 1
End If
End With
Wend

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 313
Default Insert Rows using VBA

Mike, that did the trick. Thanks. One more thing I want to add colour to the
blank lines. Where would I put the following code to do that?

.Interior.ColorIndex = 1

"Mike H" wrote:

Tony,

Maybe a slightly different approach

Sub insert_row()
Sht = "Break & Lunch Schedule"
lastrow = Sheets(Sht).Cells(Rows.Count, "R").End(xlUp).Row
For x = lastrow To 1 Step -1
If Sheets(Sht).Cells(x, 18) = 1 Then
Sheets(Sht).Rows(x).EntireRow.Insert
End If
Next
End Sub


Mike

"Tony" wrote:

I am using the following code to try and find a "1" in column "R". Whenever I
find one I want to insert a row. With the code I have here I find the "1" in
row "4" but it inserts a row at row "48", then row "50, etc. What do I need
to do to insert a row at rows "4", "15",


LoopCol2 = 1
LoopRow2 = 4

While Not IsEmpty(Worksheets("Break & Lunch Schedule").Cells(Looprow2,
LoopCol2))etcf.
With wsPh
If wsPh.Range("r" & Looprow2 + 0) = 1 Then wsPh.Range("r"
& Looprow2).End(xlDown).Select
Selection.EntireRow.Insert
Looprow2 = Looprow2 + 1
' ETop = ETop + 1
Else
Looprow2 = Looprow2 + 1
' ETop = ETop + 1
End If
End With
Wend

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 a worksheet that do not change adjoining rows craigandmel Excel Discussion (Misc queries) 2 April 29th 08 10:26 PM
How do i insert blank rows between data that is thousands of rows paul.eatwell Excel Discussion (Misc queries) 5 April 14th 08 10:49 PM
Insert rows: Formats & formulas extended to additonal rows Twishlist Excel Worksheet Functions 0 October 22nd 07 04:23 AM
How do I insert blank rows between rows in completed worksheet? bblue1978 Excel Discussion (Misc queries) 1 October 26th 06 07:02 PM
How do i insert of spacer rows between rows in large spreadsheets laurel Excel Discussion (Misc queries) 0 April 24th 06 01:38 PM


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