Results 1 to 3 of 3

How to read an array one time only, and use it in a UDF evaluated in multiple cells?

Threaded View

geophysicist How to read an array one time... 08-13-2013, 12:08 PM
protonLeah Re: How to read an array one... 08-13-2013, 09:11 PM
geophysicist Re: How to read an array one... 08-14-2013, 09:22 AM
  1. #1
    Registered User
    Join Date
    08-06-2013
    Location
    the boonies
    MS-Off Ver
    Excel 2007
    Posts
    46

    Question How to read an array one time only, and use it in a UDF evaluated in multiple cells?

    This code I'm developing (my first VBA script) essentially works, but given real-world data attached to it, will be inefficient and likely slow.
    There is a reference sheet called "markers" containing 25000 lines (example attached only has 20) that need to be cross-referenced to about 30000 data points. Currently my code would need to read the 25000 lines at each of the 30000 data points getting cross-referenced. But, I'm sure there's a better way.

    Can you show me how to read the reference arrays in worksheet "markers" just one time (from what I've read I think this would need to be done outside of the UDF or with Public scope - not clear to me), while my UDF is called repeatedly and makes use of those arrays to make evaluations in worksheets "A" and "B" (see range A7:A16 in both those workseets).

    Thanks!

    Option Base 1
    Function WName(ByVal XCP As Range, ByVal YCP As Range, ByVal SD As Range) As String
    
    Dim i As Integer, n As Integer, Nmin As Integer, Nmax As Integer, MinIndex As Integer
    Dim Dist As Single, MinDist As Single
    Dim XMKR(6 To 25) As Single     'Ideally the array size would be defined by variables Nmin and Nmax but I get a compiling error saying the "needs to be constant".
    Dim YMKR(6 To 25) As Single     'I can live without Nmin and Nmax passed as variables to define the arrays if there is a way to read them once only, but refer to them repeatedly.
    Dim WMKR(6 To 25) As String     'My thinking to use variables to define the array dimensions repeatedly was to read from pre-defined appropriate portions of "markers" instead of the whole list.
    
    Select Case SD
       Case Is = "TsdA"
            Nmin = Range("markers!$B$2")
            Nmax = Range("markers!$B$3")
            Debug.Print "comment case A "; SD; Nmin; Nmax
       Case Is = "TsdB"
            Nmin = Range("markers!$C$2")
            Nmax = Range("markers!$C$3")
            Debug.Print "comment case B "; SD; Nmin; Nmax
    End Select
    
    'Debug.Print "got this far. Nmin = "; Nmin   'First time through, code evaluates this line BEFORE the Select Case block above ?!!
    For i = Nmin To Nmax
        Debug.Print "Reading arrays from markers worksheet.  i = "; i  'How can I read the markers worksheet arrays just once (outside of this procedure?) and use that memory block repeatedly?
        XMKR(i) = Range("markers!$E" & i)
        YMKR(i) = Range("markers!$F" & i)
        WMKR(i) = Range("markers!$C" & i)
    Next i
    
    MinDist = Sqr((XCP - XMKR(Nmin)) ^ 2 + (YCP - YMKR(Nmin)) ^ 2)
    MinIndex = Nmin
    
    For n = Nmin + 1 To Nmax
        'Debug.Print "n = "; n
        Dist = Sqr((XCP - XMKR(n)) ^ 2 + (YCP - YMKR(n)) ^ 2)
        If Dist < MinDist Then
            MinDist = Dist
            MinIndex = n
        End If
        If MinDist = 0 Then
            Exit For
        End If
    Next n
    
    If MinDist > 3 Then
        WMKR(MinIndex) = "DISTerr>3m"
    End If
    
    Debug.Print WMKR(MinIndex); MinDist; MinIndex; n
    WName = WMKR(MinIndex)
    'ActiveCell.Offset(0, 4).Value = MinDist   'Nice to have: Output the MinDist value four columns to the right of the WName function call.  This not working.  Need to update ActiveCell selection somehow first.
    
    End Function
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] SUMIFS with multiple criteria evaluated against array?
    By opheim in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-07-2013, 09:03 AM
  2. Replies: 11
    Last Post: 04-14-2013, 12:02 AM
  3. [SOLVED] Read last element of Split array and then discard it and resize the array.
    By staggers47 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-28-2012, 06:36 PM
  4. IF function: extending to read multiple cells/answers
    By balcony in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-25-2011, 02:08 PM
  5. Read Range To Array, Then Pass Array To Function
    By ProbablyNotARealName in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-20-2010, 12:29 PM

Tags for this Thread

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