How to set the active cell from a UDF?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,676
Office Version
  1. 365
Platform
  1. Windows
I am working on a UDF that processes a large table of data. If it detects an error, I would like it to make that the active cell on return to the sheet. Here's my code that isn't working. I've simplified the code, but I think all the relevant parts are included. The statement in question is the next to last one.

Code:
Public Function MyFun(Data as Range) As Variant
Dim errmsg As String
Dim RngColBeg As Long
Dim RngColEnd As Long
RngColBeg = 1
RngColEnd = Data.Columns.Count

Dim iCol As Long
Dim CellAddr As String
For iCol = RngColBeg To RngColEnd
   CellAddr= Data(1, iCol).Address
      . . .
  If (some error occurs) Then
    errmsg = "...text of error message..."
    MyFun = ErrCode(errmsg,CellAddr): Exit Function
  End If
      . . .
Next iCol
   . . .
End Function


Public Function ErrCode(errtxt as string, celladdr as string) As Variant
   . . .
Dim errmsg as string
errmsg = (standard error message including the cell address)
Dim Button As Long
Button = MsgBox(errmsg, vbYesNoCancel + vbDefaultButton2)
   . . . 

ErrCode = (some error code)
ActiveSheet.Range(pErrCellAddr).Select

End Function

Can someone suggest a working solution?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
A function cannot select a cell, it can only return a result (unless called by a macro).
 
Last edited:
Upvote 0
A function cannot select a cell, it can only return a result (unless called by a macro).

Well, I guess that's why it didn't work. Rats! :mad:

Do you know why the geniuses at M$FT designed it that way? Would something horrible happen if it was allowed?

Why doesn't that statement get an error to let me know it isn't going to work? Would that make VBA less cryptic and too easy to use? :banghead:

Thanks
 
Upvote 0
Do you know why the geniuses at M$FT designed it that way? Would something horrible happen if it was allowed?
All worksheet functions (not just UDF's) work that way. That is, they cannot change the worksheet environment (e.g. formatting, changing the selection, etc.)
For example, with the SUM function you cannot have it select another cell or italicize the result if it doesn't produce a predetermined total.
 
Upvote 0
All worksheet functions (not just UDF's) work that way. That is, they cannot change the worksheet environment (e.g. formatting, changing the selection, etc.)
OK, but why?

For example, with the SUM function you cannot have it select another cell or italicize the result if it doesn't produce a predetermined total.
What do you mean, "if it doesn't produce a predetermined result"?

It seems to me that I cannot make SUM do anything other than add up the contents of some cells. With the built-in functions, like SUM, they do what they do. But with UDFs, they do what I tell them to do. So why can't I tell them to select a cell (for example)?
 
Upvote 0
OK, but why?


What do you mean, "if it doesn't produce a predetermined result"?

It seems to me that I cannot make SUM do anything other than add up the contents of some cells. With the built-in functions, like SUM, they do what they do. But with UDFs, they do what I tell them to do. So why can't I tell them to select a cell (for example)?

For the same reasons that built-in functions can't do it. If you want to change the worksheet environment, you need a macro - not a function. Functions return results.
 
Upvote 0
For the same reasons that built-in functions can't do it.
And what are those reasons?

If you want to change the worksheet environment, you need a macro - not a function. Functions return results.
You are giving me the rule, not the reason behind the rule. Why did M$FT decree that macros can change the environment, but functions cannot?
 
Upvote 0
That's a very helpful reference, thanks.

But it still does not tell me why this is how it is.

This is like me getting pulled over for going 45 in a 35 zone. The exchange between me and the officer goes like this:

  • M: Why did you pull me over.
  • O: You were going 45 in a 35 zone.
  • M: Why can't I go 45?
  • O: Because the speed limit here is 35.
  • M: Why it is 35?
  • O: Because that's what it says on the sign.
  • M: But why does it say that?
  • O: Because that is the speed limit in all zones like this one.
  • M: But why is the speed limit 35 in zones like this one?
  • O: Because that's what the law says.
  • M: But why does the law say that?
  • O: Because that's what the legislature passed.
  • M: But why did they pass that law?
  • O: Because they got a majority of the votes.

At no point did the cop say, "because studies have shown that going over 35 is unsafe in areas like this". That's the "why". All of the other answers were the "what".

So now I ask again: Why is there a rule that functions cannot alter the worksheet environment?
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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