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
Bookmarks