UDF evaluates after every change

JohnnyBravo1

New Member
Joined
Oct 26, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. 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

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
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?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Looks like the last part is taking the currently active sheet instead of the defined sheet. rangeStart.Address and rangeEnd.Address will only return the range, not the sheet name.
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 Range
    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
        Set myRange = Range(rangeStart, rangeEnd)
        averageFromRange = Application.WorksheetFunction.Average(myRange)
    End If

End Function
 
Upvote 0
Solution
Hmm, that would make sense. But how to fix it? i can hardcode the reference to active sheet's name in concat for myRange, because the function wont be reused in any other sheet (i hope), would it solve the issue?
 
Upvote 0
Are you saying that the changes that I made didn't make any difference?
Well, it does, but some other problem occured. Somehow, ssome of the parts are evaluating to #VALUE! now, but its the case for other thread so i will try to fix it myself.
 
Upvote 0
some of the parts are evaluating to #VALUE! now
Which parts?

I haven't tested your code but I would expect a #VALUE! error if one or both dates are out of range as you have not specified a default value for when that happens.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top