How to run Visual Basic from Excel sheet

  • Thread starter Thread starter Kristian Ougaar
  • Start date Start date
K

Kristian Ougaar

How do I run a Visual Basic routine from a normal Excel cell. I would
like to do something like this:

=IF(RunIt=TRUE, Run my Visual Basic routine, Do nothing)

So if some calculations results in the variable "RunIt" is set equal to
TRUE, the Visual Basic routine must run.

Kristian
 
Hi Kristian,
You can only run functions from a worksheet. Not subs.
You can use a worksheet change event.
Do a google search for worksheet_change() and you should get some examples.
 
You can call a VB function (a user-defined function or UDF) from a cell like
this:

=IF(A1=A2,MultBy2(A1),"")

where MultBy2 is something like this:

Function MultBy2(Num As Double) As Double
MultBy2 = Num * 2
End Function

The called UDF is limited to returning a value to the cell it is called from
(just like Excel worksheet functions). It cannot take actions like
formatting cells, opening files, etc.

If you want to have actions take place when a sheet recalcs use the sheet's
Worksheet_Calculate event.
 

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

Back
Top