Compile Error- Sub or Function not defined

Vaghela

New Member
Joined
Oct 28, 2017
Messages
16
Not sure what is going wrong with the following code, I am getting the Compile Error- Sub or Function not defined.

I am trying to delete all rows which dont have Cell value Ed.


Sub Macro1()

Dim r As Long
Dim FRow As Long
Dim LRow As Long

FRow = 2
LRow = 1000

For r = LRow To FRow Step -1
If Cell(r, 3) <> "Ed" Then
Rows("r").Select
Selection.Delete
End If
Next r

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You're missing an 's' for the line 'If Cell(r,3)....

Should be like this.

Code:
Sub Macro1()


Dim r As Long
Dim FRow As Long
Dim LRow As Long


FRow = 2
LRow = 1000


For r = LRow To FRow Step -1
If Cells(r, 3) <> "Ed" Then
Rows("r").Select
Selection.Delete
End If
Next r


End Sub
 
Upvote 0
And while you are here that's dangerous code. It doesnt specify a worksheet to work on. If inadvertently used on the wrong sheet it will basically delete everything. Specify your worksheets.
 
Upvote 0
There are some more corrections too. Don't use 'Select'. Also the 'r' should not be in double quotes.

Code:
Sub Macro1()


Dim r As Long
Dim FRow As Long
Dim LRow As Long


FRow = 2
LRow = 1000


For r = LRow To FRow Step -1
    If Cells(r, 3) <> "Ed" Then
        Rows(r).Delete
    End If
Next r


End Sub
 
Upvote 0
And while you are here that's dangerous code. It doesnt specify a worksheet to work on. If inadvertently used on the wrong sheet it will basically delete everything. Specify your worksheets.

how to define a worksheet, what if I plan to run the code with the relevant worksheet active.
 
Upvote 0
thanks for the quick response, but why using Select is not a good option,

just trying to develop some knowledge around coding.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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