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. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,934

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

    Here's one alternative:

    Private Sub Workbook_Open()
        BuildMKR
    End Sub
    Option Explicit
    Public MKRTable  As Variant
    Sub BuildMKR()
        MKRTable = Array(WorksheetFunction.Transpose(Sheets("markers").Range("E6:e25").Value), WorksheetFunction.Transpose(Sheets("markers").Range("F6:F25").Value), WorksheetFunction.Transpose(Sheets("markers").Range("C6:C25").Value))
        MKRTable = WorksheetFunction.Transpose(MKRTable)
    End Sub
    Option Base 1
    
    Public Function WName(ByVal XCP As Range, ByVal YCP As Range, ByVal SD As String) As String
        Dim i           As Long, _
            n           As Long, _
            Nmin        As Long, _
            Nmax        As Long, _
            MinIndex    As Long, _
            Dist        As Single, _
            MinDist     As Single
        
        'Initialize constants corresponding to columns in MRKTable
        Const XMKR As Long = 1
        Const YMKR As Long = 2
        Const WMKR As Long = 3
        
        If IsEmpty(MKRTable) Then Call BuildMKR
        
        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
    
        MinDist = Sqr((XCP - MKRTable(Nmin, XMKR)) ^ 2 + (YCP - MKRTable(Nmin, YMKR)) ^ 2)
        MinIndex = Nmin
    
        For n = Nmin + 1 To Nmax
            'Debug.Print "n = "; n
            Dist = Sqr((XCP - MKRTable(n, XMKR)) ^ 2 + (YCP - MKRTable(n, YMKR)) ^ 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
            MKRTable(MinIndex, WMKR) = "DISTerr>3m"
        End If
    
        WName = MKRTable(MinIndex, WMKR)
    End Function
    Attached Files Attached Files
    Ben Van Johnson

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