How to test for a cverror condition returned from one UDF to another

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,525
Office Version
  1. 365
Platform
  1. Windows
I have a little UDF that will convert a column number into a column letter:
Code:
Public Function CvtColNum2Ltr(ByVal ColNum As Integer) As String
Const MaxCol As Long = 16384   'Numerical value of maximum column (XFD).
          
If ColNum > MaxCol Then                 'If column number if too large,
  CvtColNum2Ltr = CVErr(xlErrValue)    'Return a value error
  Exit Function
End If

CvtColNum2Ltr = Split(Cells(1, ColNum).Address, "$")(1)
End Function
The UDF is in one of my personal addins.

If I call this UDF from a worksheet, it works perfectly. But if I call it from another UDF that is called from that same worksheet, I get "Compile error: Sub or Function not defined".

What am I doing wrong?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Does your function reside in a STANDARD module?

Dave
 
Upvote 0
Does your function reside in a STANDARD module?
The CvtColNum2Ltr function is in an add-in module that is not attached to any worksheet. It is my understanding that that makes it a standard module.

The function that calls is, PCTally, is in Module1, a module that is attached to the Readings workbook. It is my understanding that that is not a standard module.

If I call CvtColNum2Ltr from sheet1 in the Readings workbook, it works. But if I call it from PCTally, which is called from that same sheet in that same workbook, it gets that error.

Did I answer your question?
 
Upvote 0
1) You have CvtColNum2Ltr defined as a string function, but for large column numbers, the CvtColNum2Ltr = CVErr(xlErrValue) line gives a mismatch error when called from VBA. Excel's worksheet UDF handler masks that by giving a #Val error. The function should be a variant type if you want error values returned.

2) You say that the Not Found error occurs when you use CvtColNum2Ltr in a different UDF. Could you post the code for that UDF please?
 
Upvote 0
1) You have CvtColNum2Ltr defined as a string function, but for large column numbers, the CvtColNum2Ltr = CVErr(xlErrValue) line gives a mismatch error when called from VBA. Excel's worksheet UDF handler masks that by giving a #Val error. The function should be a variant type if you want error values returned.
That makes sense. I should have thought of that. Here's the corrected code:
Code:
Public Function CvtColNum2Ltr(ByVal ColNum As Integer) As Variant
Const MaxCol As Long = 16384   'Numerical value of maximum column (XFD).

If ColNum > MaxCol Then    'If column number if too large,
   CvtColNum2Ltr = CVErr(xlErrValue)   'Return a value error
   Exit Function
End If

CvtColNum2Ltr = Split(Cells(1, ColNum).Address, "$")(1)

End Function


2) You say that the Not Found error occurs when you use CvtColNum2Ltr in a different UDF. Could you post the code for that UDF please?
The UDF that calls CvtColNum2Ltr is quite long. Here's the relevant parts excerpted:
Code:
Public Function PCTally(pAMRange As Range, pAMSkip As Range) As String

Dim OU as string
OU = CvtColNum2Ltr(27)

End Function
 
Upvote 0
Actually, I don't think those are the relevant parts. I would guess that somewhere in PCTally, there is a mis-spelling, or a missing character or a something to trigger the not found error, not just in the lines refering to CvtColNum2Ltr.
 
Upvote 0
Actually, I don't think those are the relevant parts. I would guess that somewhere in PCTally, there is a mis-spelling, or a missing character or a something to trigger the not found error, not just in the lines refering to CvtColNum2Ltr.
I simplified the code and it still fails.

Here's a simplified version of PCTally. It resides in a workbook code module that contains several other UDFs. It still gets the same "Sub or Function not defined" error. (The error message doesn't say "Not Found".) All of the other UDFs in that code module work and this one works, too, if I remove the call to CvtColNum2Ltr. I had been using it for months, but decided to add code so it could report the addre3ss of a cell containing invalid data.
[/CODE]
Public Function PCTTest() As String
MsgBox CvtColNum2Ltr(27)
PCTTest = "Done"
End Function
[/CODE]

And here's the CvtColNum2Ltr code again. It resides in an add-in module with dozens of other UDFs that all work.
Code:
Public Function CvtColNum2Ltr(ByVal ColNum As Integer) As Variant

Const MaxCol As Long = 16384 'Numerical value of maximum column (XFD).

If ColNum > MaxCol Then 'If column number if too large,
   CvtColNum2Ltr = CVErr(xlErrValue) 'Return a value error
   Exit Function
End If

CvtColNum2Ltr = Split(Cells(1, ColNum).Address, "$")(1)

End Function

I also tried moving PCTTest to the same add-in module with CvtColNum2Ltr. It then gets a Name error as if the worksheet cannot find it. But the same sheet can execute other UDFs in that add-in module.
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,960
Latest member
AKSMITH

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