counting records

G

Guest

can somebody provide me with an answer as to why the following function
returns an error on the if line?

the error message is "object required"

Function countrecords() as integer
Dim count As Integer
count = 0

For Each cell In Worksheets("Saved Records").Range("A1:A500")
If cell.Value Is Not Null Then
count = count + 1
End If
Next cell
countrecords = count
End Function
 
N

Norman Jones

Hi Panagiotis,

Try:

'=============>>
Public Function countrecords() As Integer
Dim count As Integer
Dim cell As Range
count = 0

For Each cell In Worksheets("Saved Records").Range("A1:A500")
If Not IsEmpty(cell.Value) Then
count = count + 1
End If
Next cell
countrecords = count
End Function
'<<=============
 
N

NickHK

Panagiotis,
The keyword "Is" is used with Objects.
Dim WB As Workbook
On Error Resume Next
Set WB=Workbooks.Open(PathToFile)
If Not WB Is Nothing Then.....

But cell.value is not an object.
Depending what you are test for:
cell.value<>0 or Not (cell.value=0)
cell.value<>""
cell.value<>Empty

NickHK
 
D

Duncan

or......


Function countrecords() As Integer
Dim count As Integer
count = 0


For Each cell In Worksheets("sheet1").Range("A1:A500")
If cell.Value <> isblank = True Then
count = count + 1
End If
Next cell
countrecords = count
MsgBox count
End Function
 
N

Norman Jones

Hi Panagiotis,

However, why not simply:

countrecords = Application.CountA(Range("A1:A500"))
 

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

Top