How can I delete rows accordinng to formula

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am trying to delete bunch of empty rows, which are regularly spaced in my
worksheet, using "FOR-NEXT" loop. But, it is giving me error. What I my I
doing wrong here. Thanks.


Sub Macro1 ()
For i = 1 To 100
n = 3 * i - 1
ActiveSheet.Rows("n, n + 1").Select
Next
Selection.Delete Shift:=xlUp
End Sub
 
First, delete rows from the higher numbered rows first, or your loop won't
work.

For i = 100 to 1 step -1
rows(3*i).delete Shift:=xlUp
rows(3*i-1).delete Shift:=xlUp
Next
 
Sub Macro1 ()
For i = 100 To 1 Step -1
n = 3 * i - 1
ActiveSheet.Rows(n, n + 1).Select
Next
Selection.Delete Shift:=xlUp
End Sub
 
In complete correction. Should be

Sub Macro1 ()
For i = 100 To 1 Step -1
n = 3 * i - 1
ActiveSheet.Rows(n).Resize(2).Select
Selection.Delete Shift:=xlUp
Next
End Sub
 
Thank you very much, Patrick.

My original plan was to select all the rows by the help of a loop and then,
I was hoping to delete them all at once. Also, I did not realize that Rows( )
object would not accept something like two variables, as such Rows (n,n+1).

Reversing would makes sense!

Jack
 
Thanks, Tom.
I was not avare of of the fact that you could use "size( )" with Row ( )
object. My original plan was to select all the rows by a loop and then get
out of the loop and delete them all at once, rather then reversing and
deleting one at a time.

Would it be not possible?
 
Range("5:6,9:10,17:18,23:24").Delete Shift:=xlUp

build the text string
if n is the row
text = text & "," & (n -1) & ":" & n
 
Sub Macro1 ()
Dim rng as Range, rng1 as Range
For i = 1 To 100
n = 3 * i - 1
set rng = ActiveSheet.Rows(n).Resize(2)
if not rng1 is nothing then
set rng1 = union(rng1,rng)
else
set rng1 = rng
end if
Next

rng1.Delete Shift:=xlUp
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top