+ Reply to Thread
Results 1 to 3 of 3

Arrays passed into function

Hybrid View

  1. #1
    Registered User
    Join Date
    06-04-2013
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    2

    Arrays passed into function

    Hi
    My VBA is a little rusty, but here goes. I'm trying to write a function that takes two arrays and counts the number of cells with numbers in them according to certain rules. The problem is that whatever I do, a #VALUE error appears in the worksheet cell when I try to run it. My code is below, and the array inputs are meant to be ranges in the worksheet.
    Thanks in advance.

    Public Function countOwn(id As Variant, ByRef Xarr() As Variant, ByRef Yarr As Variant) As Long

    If UBound(Xarr, 1) <> UBound(Yarr, 1) Or UBound(Xarr, 2) > 1 Or UBound(Yarr, 2) > 1 Then
    MsgBox "The input arrays must be the same length and of dimension 1"
    Exit Function
    End If

    Dim i As Integer
    countOwn = 0
    For i = 1 To UBound(Xarr, 1)
    If Xarr(i) = id And IsNumeric(Yarr(i)) Then countOwn = countOwn + 1
    Next i

    End Function

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Arrays passed into function

    you need to pass the ranges as type excel.range and for each c in rngX.cells....

    you could get this with a formula
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    06-04-2013
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Arrays passed into function

    Thanks, that works perfectly. The modified code is below.

    Public Function countOwn(id As Variant, Xarr As Range, Yarr As Range) As Long

    If Xarr.Rows.Count <> Yarr.Rows.Count Or Xarr.Columns.Count > 1 Or Yarr.Columns.Count > 1 Then
    MsgBox "The input arrays must be the same length and of dimension 1"
    Exit Function
    End If

    Dim xDat As Variant, yDat As Variant
    xDat = Xarr
    yDat = Yarr

    Dim i As Integer
    countOwn = 0
    For i = LBound(xDat, 1) To UBound(xDat, 1)
    If xDat(i, 1) = id And IsNumeric(yDat(i, 1)) Then countOwn = countOwn + 1
    Next i

    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