+ Reply to Thread
Results 1 to 6 of 6

counting records

  1. #1
    Panagiotis Marantos
    Guest

    counting records

    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

  2. #2
    Norman Jones
    Guest

    Re: counting records

    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
    '<<=============


    ---
    Regards,
    Norman



    "Panagiotis Marantos" <[email protected]> wrote
    in message news:[email protected]...
    > 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




  3. #3
    Duncan
    Guest

    Re: counting records

    The error is with "Is Not Null"

    try

    If cell.Value <> "" Then

    Duncan


    Panagiotis Marantos wrote:

    > 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



  4. #4
    Duncan
    Guest

    Re: counting records

    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






    Norman Jones wrote:

    > 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
    > '<<=============
    >
    >
    > ---
    > Regards,
    > Norman
    >
    >
    >
    > "Panagiotis Marantos" <[email protected]> wrote
    > in message news:[email protected]...
    > > 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



  5. #5
    NickHK
    Guest

    Re: counting records

    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

    "Panagiotis Marantos" <[email protected]> wrote
    in message news:[email protected]...
    > 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




  6. #6
    Norman Jones
    Guest

    Re: counting records

    Hi Panagiotis,

    However, why not simply:

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


    ---
    Regards,
    Norman



    "Norman Jones" <[email protected]> wrote in message
    news:%[email protected]...
    > 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
    > '<<=============
    >
    >
    > ---
    > Regards,
    > Norman
    >
    >
    >
    > "Panagiotis Marantos" <[email protected]> wrote
    > in message news:[email protected]...
    >> 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

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1