JohnnyBravo1
New Member
- Joined
- Oct 26, 2021
- Messages
- 3
- Office Version
- 2016
- Platform
- Windows
Hi,
I have an issue, i though pretty simple one but cant handle it so i guess was wrong.
I have a UDF that calculates average of exchange rates between 2 dates
Any change in whole workbook (apart from the sheet in which the function is called) re-evaluates the function to #VALUE!. I tried both parametrizing udf to have these dates as input params, and activating the sheet, i have no other clue how to handle this issue. Could You help me out?
I have an issue, i though pretty simple one but cant handle it so i guess was wrong.
I have a UDF that calculates average of exchange rates between 2 dates
VBA Code:
Option Explicit
Public Function averageFromRange() As Double
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets("Exchange Rates")
Dim dateStart As Date: dateStart = sh.range("G1").Value
Dim dateEnd As Date: dateEnd = sh.range("G2").Value
Dim myRange As String
Dim rangeStart As range
Dim rangeEnd As range
Set rangeStart = sh.range("A:A").Find(What:=CStr(dateStart), LookAt:=xlWhole, LookIn:=xlValues).Offset(0, 1)
Set rangeEnd = sh.range("A:A").Find(What:=CStr(dateEnd), LookAt:=xlWhole, LookIn:=xlValues).Offset(0, 1)
If rangeStart Is Nothing Then
MsgBox ("Date " & dateStart & " out of range")
End If
If rangeEnd Is Nothing Then
MsgBox ("Date " & dateEnd & " out of range")
End If
If Not (rangeStart Is Nothing Or rangeEnd Is Nothing) Then
myRange = rangeStart.Address & ":" & rangeEnd.Address
averageFromRange = Application.WorksheetFunction.Average(range(myRange))
End If
End Function