Here's the deal...In trying to get past my fear of Class Modules in Excel VBA, I decided to create a class that is an array, then add functions (methods) for adding elements, sorting an instance, etc. Those are things I keep re-writing in normal modules as functions/subs but hope use of classes might be a step forward.
Code Module
Public Type Thing
Name As String
SomeNumber As Double
End Type
Class Module
Private pSomething() As Thing
This is followed by all the usual Public LETs and GETs, plus a function for inserting new values into the array. Then I get to the sorting function/method. There is no problem with sorting by Name or SomeNumber, but so far that takes two function/methods. I would like to parameterize into a single function/mehod then use an optional parameter to control which field is to be used. The following works, but it seems a bit clunky
Function SortByField(Optional FieldName As String, Optional SortOrder As vbaSortOrder)
Dim strTemp As Thing
If SortOrder = 0 Then SortOrder = soBottomToTop
If Len(FieldName) = 0 Then FieldName = "Name"
Dim i As Long
Dim j As Long
Dim lngMin As Long
Dim lngMax As Long
lngMin = LBound(pSomething)
lngMax = UBound(pSomething)
For i = lngMin To lngMax - 1
For j = i + 1 To lngMax
If IIf(SortOrder = soBottomToTop, _
IIf(FieldName = "Name", pSomething(i).Name > pSomething(j).Name, _
pSomething(i).SomeNumber > pSomething(j).SomeNumber), _
IIf(FieldName = "Name", pSomething(i).Name < pSomething(j).Name, _
pSomething(i).SomeNumber < pSomething(j).SomeNumber)) _
Then
strTemp = pSomething(i)
pSomething(i) = pSomething(j)
pSomething(j) = strTemp
End If
Next j
Next i
End Function
What I would like to do is replace the following (and it's peer in the second part of this gawdawful IF(IIF...) nonsense
IIf(FieldName = "Name", pSomething(i).Name > pSomething(j).Name, pSomething(i).SomeNumber > pSomething(j).SomeNumber)
...with something like this
"pSomething(i)." & FieldName > "pSomething(j)." & FieldName
Direct Question: How do I get the string to evaluate/convert to code?
Indirect Question: Is there some other technique to pass in a fieldname and have it treated as something other than a string?
Thanks in advance for any help, assistance, guidance, direction, references, advice this is a fool's errand, or derisive comments :).