Loop to select and delete a row

JonElCanche

Board Regular
Joined
Aug 25, 2011
Messages
59
Below is the loop. An error occurs at Rows("y:y").Select
The y value that would be found in cell C500 would always match the row that would be selected and deleted. For example if cell C500 had the value 13, I would want Rows("13:13").select and then deleted. Any ideas?



Sub Macro2()
'
' Macro2 Macro
'


'
For y = 1 To 100


If Range("c500").Value = y Then
Rows("y:y").Select
Selection.Delete Shift:=xlUp
Sheets("Dashboard").Select
End If


Next y


End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The issue is that "y:y" is a string rather than variable. The y's don't result in a number between 1 and 100 as desired by the loop, but rather just the constant "y:y"
 
Upvote 0
Try
Code:
Sub Macro2()
Dim y As Long
    For y = 1 To 100
        If Range("c5").Value = y Then Rows(y).Delete
    Next y
Sheets("Dashboard").Activate
End Sub
 
Upvote 0
That worked like a charm Michael, thank you. What is the difference between your code and mine? We both used the variable y to specify the row?


Try
Code:
Sub Macro2()
Dim y As Long
    For y = 1 To 100
        If Range("c5").Value = y Then Rows(y).Delete
    Next y
Sheets("Dashboard").Activate
End Sub
 
Upvote 0
I just tried to use this exact same code on a different page and stepping through it everything works perfectly until it deletes the desired row. After deleting the correct row it tries to continues through the loop as it should but errors. Any ideas why that would be? After the desired row is deleted Range("c500").Value does result in an #N/A because the corresponding item is no longer in the list, but that is the same as the other page where everything works flawlessly.


Try
Code:
Sub Macro2()
Dim y As Long
    For y = 1 To 100
        If Range("c5").Value = y Then Rows(y).Delete
    Next y
Sheets("Dashboard").Activate
End Sub
 
Upvote 0
In regard to your first question, see b.downeys post
For the 2nd question try reversing the loop
Code:
Sub Macro2()
Dim y As Long
    For y = 100 To 1 step -1
        If Range("c5").Value = y Then Rows(y).Delete
    Next y
Sheets("Dashboard").Activate
End Sub
 
Upvote 0

Forum statistics

Threads
1,206,826
Messages
6,075,094
Members
446,119
Latest member
BrianAndrews

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top