You can write a user defined function that can run a macro but you need to be aware that calling a macro from a function prohibits the physical alteration of the worksheet.
i.e you can only return a value to the calling cell.
For an example see the following code placed in a standard module:
Function Times10(CellData)
Macro1
Times10 = CellData * 10
End Function
Sub Macro1()
MsgBox "Called from a function"
End Sub
Now assuming you have a numerical value in Cell A1;in cell B1 type the following formula:
If I understand your formula, you want to run a macro whenever the value in either A12 or B12 is changed unless the net result after the change is that the sum (A12+B12) is less than three. You also want to display either "small" or blank in whatever cell your formula is in.
The way to do that is by putting code in the Worksheet_Change event for the sheet: [blue]
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Address = "$A$12" Or Target.Address = "$B$12" Then
If [A12] + [B12] >= 3 Then
Call MAKRO1
End If
End If
End Sub
[/color]
Put this in a code module: [blue]
Code:
Sub MAKRO1()
MsgBox "Running MAKRO1"
End Sub
[/color]
Now test it by putting this in cell C12: [blue]
Code:
=IF(A12+B12<3,"small","ok")
[/color]
Now try different values in A12 and B12 to see the effect.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.