Next without For VBA Error

Will85

Active Member
Joined
Apr 26, 2012
Messages
254
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am new to loops but learning. I cannot figure out why I am getting a compile error:

Sub DeleteRowsandComboboxes()
Dim Cel As Range
Dim Shp As Shape
Dim ro As Integer
Set Cel = Cells(1, 1)

Do Until Cel.End(xlDown).Row = 1048576
Set Cel = Cel.End(xlDown)
ro = Cel.Row
If Cel = "Yes" Then
Rows(ro).Delete
Set Cel = Cells(ro, 1)
Else
Set Cel = Cel.End(xlDown)
End If
Next

For Each Shp In ActiveSheet.Shapes
If Shp.Type = 8 Then
Shp.Select
If Selection.LinkedCell = "#REF!" Then Shp.Delete
End If
Next


End Sub
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
The Next needs to be a loop.

For Each
****
Next

Do Until
****
Loop

Code:
[COLOR=#333333][COLOR=#333333]Sub DeleteRowsandComboboxes()[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Dim Cel As Range[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Dim Shp As Shape[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Dim ro As Integer[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Set Cel = Cells(1, 1)[/COLOR][/COLOR]

[COLOR=#333333][COLOR=#333333]Do Until Cel.End(xlDown).Row = 1048576[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Set Cel = Cel.End(xlDown)[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]ro = Cel.Row[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]If Cel = "Yes" Then[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Rows(ro).Delete[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Set Cel = Cells(ro, 1)[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Else[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Set Cel = Cel.End(xlDown)[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]End If[/COLOR][/COLOR]
[COLOR=#333333]Loop[/COLOR]

[COLOR=#333333][COLOR=#333333]For Each Shp In ActiveSheet.Shapes[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]If Shp.Type = 8 Then[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Shp.Select[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]If Selection.LinkedCell = "#REF!" Then Shp.Delete[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]End If[/COLOR][/COLOR]
[COLOR=#333333][COLOR=#333333]Next[/COLOR][/COLOR]


[COLOR=#333333][COLOR=#333333]End Sub[/COLOR][/COLOR]
 
Upvote 0
thank you. I am now getting an overflow error on

ro = Cel.Row
 
Upvote 0
I changed my variable to long, and it appears to be running, but very slowly. been spinning its wheels for 5 minutes now.

I think I need to rethink my macro. I need a macro that will delete all rows in the spreadsheet if the value in column C from row 5 down = "Yes" There are breaks and blanks in between my "Yes" values, so it would have to go from the bottom up to determine the last row that could possibly need to be deleted.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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