Hello VBA and UDF world,
I have a UDF/Custom Function that works the 1st Time it's ran, but when I try to use the data in a pivot table the function attempts to recalculate and returns #VALUE. I have researched this issue, came to some conclusions, but can't figure it out entirely...
I found out that UDFs sometimes return #VALUE when there is potential confusion between data types. I have strings and integers that are being combined with this function and this was a potential issue. So, I fixed it by assigning these variables as variants. There seems to be other issues going on, too...
Here's what happens: I have a set of data and I'm adding an additional column for my UDF. I set the UDF equal to the applicable cells and it works perfectly. I then try to use this data to calculate multiple pivot tables.
On the first pivot table calculation, it works.
On the second pivot table calculation, the UDF function changes to #VALUES and the UDF module opens amidst the executing pivot table creating code and tries rerunning/recalculating.
Any thoughts on what's going wrong here?
Here's my UDF:
I have a UDF/Custom Function that works the 1st Time it's ran, but when I try to use the data in a pivot table the function attempts to recalculate and returns #VALUE. I have researched this issue, came to some conclusions, but can't figure it out entirely...
I found out that UDFs sometimes return #VALUE when there is potential confusion between data types. I have strings and integers that are being combined with this function and this was a potential issue. So, I fixed it by assigning these variables as variants. There seems to be other issues going on, too...
Here's what happens: I have a set of data and I'm adding an additional column for my UDF. I set the UDF equal to the applicable cells and it works perfectly. I then try to use this data to calculate multiple pivot tables.
On the first pivot table calculation, it works.
On the second pivot table calculation, the UDF function changes to #VALUES and the UDF module opens amidst the executing pivot table creating code and tries rerunning/recalculating.
Any thoughts on what's going wrong here?
Here's my UDF:
Code:
Function FTSTING(t As Variant, cDate As Variant) As String
Dim Month As Variant
'cDate format = YYYYMM
t= Trim(t)
Month = Right(cDate, 2)
Select Case Month
Case "01"
Month = "G"
Case "02"
Month = "S"
Case "03"
Month = "U"
Case "04"
Month = "K"
Case "05"
Month = "K"
Case "06"
Month = "S"
Case "07"
Month = "H"
Case "08"
Month = "I"
Case "09"
Month = "L"
Case "10"
Month = "Q"
Case "11"
Month = "C"
Case "12"
Month = "R"
End Select
'Year
cDate = Mid(cDate, 4, 1)
'Building the fuction
FTSTRING= t& Month & cDate
End Function