Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

excel and macros

ResolvedVersion 2003

Nick has attended:
Excel Advanced course

Excel and Macros

When using an IF function in excel can you initiate a Macro in the true or false statement?

Edited on Fri 9 May 2008, 23:49

RE: Excel and Macros

Hi Nick, Thank you for your post, welcome to the forum, the answer to your question is; NO, The reason you cannot have a formula like =IF(A1>10,MyMacro,0)in a worksheet cell is because Excel must keep track of which cells are dependents and precedents of which other cells. It must do this in order to calculate the worksheet in the proper order. VBA code which could change worksheet cells could irreversible confuse the order of calculations. Therefore, Excel forbids code called from a worksheet cell from changing anything in the Excel environment. A FUNCTION procedure can only return a value to Excel, nothing more.

However, you could use the worksheet's Change event to do something like this.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Application.Intersect(Target, Range("A1")) Is Nothing Then
If Target.Value > 10 Then MsgBox "Put Your Code Here"
End If
End If
End Sub

regards Pete

Excel tip:

Entering text in Multiple Worksheets

If you have a number of worksheets in a workbook that require the same information (data or tables) on each worksheet, this can be done as follows:

Hold down the Ctrl key and click one or more of the additional worksheet tabs (i.e. Sheet2, Sheet3, etc).

In your mainsheet (Sheet1) enter the required data or design a table.

When done 'Click' on the other sheet tabs and you will see that the information entered in Sheet1 is on all the other selected worksheets.

NB Do not forget to deselect the worksheets - otherwise you may add data to the main worksheet and all the selected worksheets will also have that data!

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.