JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
I may be in over my head here, but I cannot figure out what the heck is going on.
I have a complicated UDF that gets an error because one of the variables that gets data from a passed range is sometimes Empty and sometimes not. The UDF is called from several locations in the sheet. If I set the sheet calculation setting to Manual, the error never occurs. I select a cell that calls the UDF, press F2 to open it up, then tab out and it executes. Never an error.
If I switch it to Automatic, each cell that calls the UDF immediately calls it twice. The first time, the variable is empty. The second time, it has the correct data. I have verified this by stepping through the code. It tracks the address of the calling cell and the address of the cell in error. These are always the same.
The actual data in the cells are numeric. They are definitely not empty.
As I said, the UDF is very complicated (possibly too complicated for me to handle), so I will show snippets of the code that I think are relevant.
The UDF processes data in 5 fixed ranges and 1 variable range that are all passed as parameters. Here's the Function statement:
All of the first 6 range parameters are 1 row by 16 columns. The columns are D:S.
And here is a typical calling expression in a cell (D16 in this case).
pRatings is on the same row as the cell from where it is called. If called from anywhere on row 22, it is defined as:
The other ranges are fixed.
In the UDF code, I capture the address of the calling cell and the address of each cell in the ranges as they are being processed. Here's the code for that:
The error occurs in the WtdRtgNum routine. Both RBestI and RWrstI test as Empty. The MshgBox message is:
I set breakpoints in the code that calls WtdRtgNum and it also says that the variables are Empty.
Can anyone suggest what might be wrong?
I have a complicated UDF that gets an error because one of the variables that gets data from a passed range is sometimes Empty and sometimes not. The UDF is called from several locations in the sheet. If I set the sheet calculation setting to Manual, the error never occurs. I select a cell that calls the UDF, press F2 to open it up, then tab out and it executes. Never an error.
If I switch it to Automatic, each cell that calls the UDF immediately calls it twice. The first time, the variable is empty. The second time, it has the correct data. I have verified this by stepping through the code. It tracks the address of the calling cell and the address of the cell in error. These are always the same.
The actual data in the cells are numeric. They are definitely not empty.
As I said, the UDF is very complicated (possibly too complicated for me to handle), so I will show snippets of the code that I think are relevant.
The UDF processes data in 5 fixed ranges and 1 variable range that are all passed as parameters. Here's the Function statement:
Code:
Public Function WtdRtg(pRatings As Range, _
pRtgsBest As Range, _
pRtgsWrst As Range, _
pRtgTypes As Range, _
pRtgReq As Range, _
pRtgWts As Range, _
ParamArray PArgs() _
) As Variant
And here is a typical calling expression in a cell (D16 in this case).
Code:
=wtdrtg(Ratings,RtgsBest,RtgsWrst,RtgTypes,RtgReq,RtgWts)
pRatings is on the same row as the cell from where it is called. If called from anywhere on row 22, it is defined as:
Code:
='Sheet1'!$D22:$S22
Code:
pRtgsBest ='Sheet1'!$D$10:$S$10
pRtgsWrst ='Sheet1'!$D$11:$S$11
pRtgTypes ='Sheet1'!$D$12:$S$12
pRtgReq ='Sheet1'!$D$13:$S$13
pRtgWts ='Sheet1'!$D$15:$S$15
Code:
Dim CallerAddr As String 'Address of calling cell
CallerAddr = Application.Caller.Address
Dim iCol As Long 'Loop index
Dim RBestIAaddr As String
For iCol = Col1 To Coln 'Loop through columns
. . .
RBestI = pRtgsBest(1, iCol) 'Get the next best (max) rating
RBestIAaddr = pRtgsBest(1, iCol).Address
RWrstI = pRtgsWrst(1, iCol) 'Get the next worst (min) rating
RTypeI = pRtgTypes(1, iCol) 'Get the next rating type
RReqI = pRtgReq(1, iCol) 'Get the next required/optional setting
RtgI = pRatings(1, iCol) 'Get next rating
. . .
RtgINew = WtdRtgNum(RBestI, RWrstI, RReqI, RtgI, ScaleAve, pErrMsgSw, MyName)
. . .
Next iCol
Private Function WtdRtgNum(RBestI, RWrstI, RReqI, RtgI, ScaleAve, pErrMsgSw, MyName)
. . .
'***** This is where the error occurs *****
If Not IsNumeric(RBestI) Or IsEmpty(RBestI) Or _
Not IsNumeric(RWrstI) Or IsEmpty(RWrstI) Then
If pErrMsgSw Then MsgBox "Invalid Best (" & RBestI & ") or Worst (" & RWrstI & ")", , MyName
WtdRtgNum = CVErr(xlErrValue) 'Return error
Exit Function
End If
. . .
End Function
Code:
Invalid Best () or Worst ()
I set breakpoints in the code that calls WtdRtgNum and it also says that the variables are Empty.
Can anyone suggest what might be wrong?