Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-03-2024, 08:49 PM
soroush.kalantari soroush.kalantari is offline UDF function don't behave like Excel function with the same logic Windows 10 UDF function don't behave like Excel function with the same logic Office 2021
Competent Performer
UDF function don't behave like Excel function with the same logic
 
Join Date: Jun 2021
Posts: 124
soroush.kalantari is on a distinguished road
Default UDF function don't behave like Excel function with the same logic

I am used to use a specific formula to convert Shamsi date to milady date(see the attached excel column J). Because this formula is complex and cumbersome to write very often, I want to use a UDF function with the same logic instead.
Based on the formula logic, I have written the UDF Function. It works as expected for rows 2:16 but fails at rows 17:24)
To debug the problem step by step I have written a sub process with the same logic named “S2mtest” . Running this Marco. shows that the UDF function calculates the formula components as expected.(columns B:I) but. as attached picture shows, when inserting them in to final formula (last step) encounters a run time error 6( overflow)
Can you guide me on this issue? (My question is not about the formula logic, but about why the column J result are different to the column K)



The UDF function code:
'''
Function s2m_My(shamsidate As String) As Date
Dim y As Integer, m As Integer, a As Integer, b As Integer, e As Long, c As Integer
Dim cons As Date
Dim d As String


'
'cons = 1931 - 3 - 21
cons = 11403#



y = Left(shamsidate, 4) - 1300


m = Right(Left(Mid(shamsidate, 3, Len(shamsidate) - 1), 5), 2) + 0


d = Right(Mid(shamsidate, 3, Len(shamsidate)), 2)
'd = 1

a = y - 10

b = Int(a / 33)

If a Mod 33 = 32 Then
e = 7
Else

e = Int((a - b * 33) / 4)
End If

If m <= 6 Then
c = (m - 1) * 31 + d

Else

c = 186 + (m - 7) * 30 + d
End If



s2m_My = cons + a * 365 + b * 8 + e + c

End Function
'''
Attached Images
File Type: png shamsi_error.PNG (5.7 KB, 10 views)
Attached Files
File Type: xlsm Exchange Date.xlsm (27.7 KB, 3 views)
Reply With Quote
  #2  
Old 11-03-2024, 09:34 PM
Logit Logit is offline UDF function don't behave like Excel function with the same logic Windows 10 UDF function don't behave like Excel function with the same logic Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

Maybe try 'dimming' the following as LONG :

a As Long, b As Long, e As Long, c As Long
Reply With Quote
  #3  
Old 11-04-2024, 07:58 PM
soroush.kalantari soroush.kalantari is offline UDF function don't behave like Excel function with the same logic Windows 10 UDF function don't behave like Excel function with the same logic Office 2021
Competent Performer
UDF function don't behave like Excel function with the same logic
 
Join Date: Jun 2021
Posts: 124
soroush.kalantari is on a distinguished road
Default

Quote:
Originally Posted by Logit View Post
Maybe try 'dimming' the following as LONG :

a As Long, b As Long, e As Long, c As Long
Thank you very much. Your reply solved the problem.
Reply With Quote
  #4  
Old 11-04-2024, 08:24 PM
Logit Logit is offline UDF function don't behave like Excel function with the same logic Windows 10 UDF function don't behave like Excel function with the same logic Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

Glad to help
Reply With Quote
Reply

Tags
overflow error, udf function



Similar Threads
Thread Thread Starter Forum Replies Last Post
UDF function don't behave like Excel function with the same logic vlookup function between 2 workbook & logic stricky Excel 7 07-06-2023 01:44 AM
Excel function help ganesang Excel Programming 4 09-20-2021 01:48 AM
UDF function don't behave like Excel function with the same logic Restrict Editing function disable insert textbox function IanM_01 Word 5 11-21-2015 02:29 AM
UDF function don't behave like Excel function with the same logic #REF! Error in calling VBA function disappears when function is copied lcaretto Excel Programming 2 05-26-2014 07:19 PM
Creating a graph for Future Value function (FV function) bmoody Excel 2 11-06-2013 10:52 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft