A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Insert Rows using VBA



 
 
Thread Tools Display Modes
  #1  
Old April 13th, 2009, 02:48 PM posted to microsoft.public.excel.misc
Tony
external usenet poster
 
Posts: 593
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  
Old April 13th, 2009, 03:04 PM posted to microsoft.public.excel.misc
Mike H
external usenet poster
 
Posts: 8,419
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  
Old April 13th, 2009, 03:17 PM posted to microsoft.public.excel.misc
Tony
external usenet poster
 
Posts: 593
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

 




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 09:02 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.