Hello!
I am new to VBA and still have to learn some of the basics, but I am trying to learn as I go.
I am trying to code an Array Function in Excel VBA to take a range of dates and get the maximum value (date) that is less than a given value (the constraint, which would also be a date).
I will be using this function in a macro, though I can't seem to get it to work. The code currently will run, but the value given is a #VALUE! Error that tells me that "A value used in the formula is the wrong type".
Here is what I currently have:
Here is the text version so you can copy/paste
Function MaxIf_Array(List, Constraint) As Double
'Function takes a list and returns the Max value that is below a given constraint
Dim Const_Array As Variant
Dim Cell As Range
Dim SmallerThan As Long
'Transfer data to Const_Array while filtering for data smaller than Constraint
For Each Cell In List
If Not Cell > Constraint Then
SmallerThan = SmallerThan + 1
ReDim Preserve Const_Array(1 To SmallerThan)
Const_Array(SmallerThan) = Cell.Value
End If
Next Cell
'Use the Worksheet Max function on values in Array
MaxIfArray = WorksheetFunction.Max(Const_Array)
End Function
Any help would be appreciated!
Thanks!
I am new to VBA and still have to learn some of the basics, but I am trying to learn as I go.
I am trying to code an Array Function in Excel VBA to take a range of dates and get the maximum value (date) that is less than a given value (the constraint, which would also be a date).
I will be using this function in a macro, though I can't seem to get it to work. The code currently will run, but the value given is a #VALUE! Error that tells me that "A value used in the formula is the wrong type".
Here is what I currently have:
Here is the text version so you can copy/paste
Function MaxIf_Array(List, Constraint) As Double
'Function takes a list and returns the Max value that is below a given constraint
Dim Const_Array As Variant
Dim Cell As Range
Dim SmallerThan As Long
'Transfer data to Const_Array while filtering for data smaller than Constraint
For Each Cell In List
If Not Cell > Constraint Then
SmallerThan = SmallerThan + 1
ReDim Preserve Const_Array(1 To SmallerThan)
Const_Array(SmallerThan) = Cell.Value
End If
Next Cell
'Use the Worksheet Max function on values in Array
MaxIfArray = WorksheetFunction.Max(Const_Array)
End Function
Any help would be appreciated!
Thanks!