VBA Cell Addressing

G

Guest

I've got what must be a simple addressing problem but it's driving me
crazy. I can create a simple macro that just looks like:

Sub TestMacro
ActiveCell(1,2) = "Done"
End Sub

And it does exactly what I'd expect. It plugs a value into the cell to
the right of the cursor. Now if I try to do the similar thing with a
Function instead of a Macro it just refuses to co-operate. For example:

Function TestFunct(CellRef)
ActiveCell(1,2) = "Done"
End Function

This will croak with a #VALUE! error. Likewise:

Function TestFunct(CellRef)
CellRef(1,2) = "Done"
End Function

Also refuses to co-operate with the same error. Yet both of these
approaches work fine for me in a Macro.

How do I write to an arbitrary cell location from a Function?

Thanks.

Bill
 
F

Fredrik Wahlgren

Bill Martin -- (Remove NOSPAM from address) said:
I've got what must be a simple addressing problem but it's driving me
crazy. I can create a simple macro that just looks like:

Sub TestMacro
ActiveCell(1,2) = "Done"
End Sub

And it does exactly what I'd expect. It plugs a value into the cell to
the right of the cursor. Now if I try to do the similar thing with a
Function instead of a Macro it just refuses to co-operate. For example:

Function TestFunct(CellRef)
ActiveCell(1,2) = "Done"
End Function

This will croak with a #VALUE! error. Likewise:

Function TestFunct(CellRef)
CellRef(1,2) = "Done"
End Function

Also refuses to co-operate with the same error. Yet both of these
approaches work fine for me in a Macro.

How do I write to an arbitrary cell location from a Function?

Thanks.

Bill

You can't write to an arbitraty cell from a function. It has to be a Sub.

/Fredrik
 
B

Bob Phillips

Presumably, you are trying to do this from a worksheet? You can't.

A worksheet function can only return a value, it cannot change any of the
cell or worksheet attributes.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
G

Guest

Fredrik said:
You can't write to an arbitraty cell from a function. It has to be a Sub.

/Fredrik
Well, that explains that. Alternatively can I call a Sub from a
Function, and have that Sub write to an arbitrary cell?

Thanks...

Bill
 
B

Bob Phillips

No, see my reply. The best you can do is use the Worksheet_Change event to
monitor a cell, and if it changes, then trigger your changes.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
G

Guest

Bob Phillips wrote:

No, see my reply. The best you can do is use the Worksheet_Change event to monitor a cell, and if it changes, then trigger your changes.

Thanks Bob.  I'll have to muddle along some other how.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top