Can you execute a macro from inside a formula? IF(A1=quot;Yquot;,execute.macro,quot;quot;)
Thanks
Adam, not with a formula, but you can use a worksheet change event like
this, put in sheet code
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count gt; 1 Then Exit Sub
If Target.Address = quot;$A$1quot; And UCase(Target.Value) = quot;Yquot; Then
'put your code here
End If
End Sub--
Paul B
Always backup your data before trying something new
Please post any response to the newsgroups so others can benefit from it
Feedback on answers is always appreciated!
Using Excel 2002 amp; 2003
quot;Adam Kroger @hotmail.comgt;quot; lt;adam_krogerlt;nospamgt; wrote in message
. ..
gt; Can you execute a macro from inside a formula?
IF(A1=quot;Yquot;,execute.macro,quot;quot;)
gt;
gt; Thanks
gt;
gt;
You can call a UDF, which is a macro that returns a result, but it can only
return a result, it cannot change other cells, or any worksheet attributes.
--
HTH
Bob Phillips
(remove nothere from email address if mailing direct)
quot;Adam Kroger @hotmail.comgt;quot; lt;adam_krogerlt;nospamgt; wrote in message
. ..
gt; Can you execute a macro from inside a formula?
IF(A1=quot;Yquot;,execute.macro,quot;quot;)
gt;
gt; Thanks
gt;
gt;
No.
Biff
quot;Adam Kroger @hotmail.comgt;quot; lt;adam_krogerlt;nospamgt; wrote in message
. ..
gt; Can you execute a macro from inside a formula?
gt; IF(A1=quot;Yquot;,execute.macro,quot;quot;)
gt;
gt; Thanks
gt;
what would I need to change to allow that UDF to be used in any cell?
This is what I want to run:
Sub ROll2D6()
Dim myCell As Range
Dim Sides As Integer
Dim Dies As Integer
Dim i As Integer
Dim myTemp As Integer
Sides = 6
Dies = 2
For Each myCell In Selection
Randomize
myTemp = 0
For i = 1 To Dies
myTemp = myTemp Application.RoundUp(Rnd() * Sides, 0)
Next i
myCell.Value = myTemp
Next myCell
End Subquot;Paul Bquot; gt; wrote in message
...
gt; Adam, not with a formula, but you can use a worksheet change event like
gt; this, put in sheet code
gt;
gt; Private Sub Worksheet_Change(ByVal Target As Range)
gt; If Target.Count gt; 1 Then Exit Sub
gt; If Target.Address = quot;$A$1quot; And UCase(Target.Value) = quot;Yquot; Then
gt; 'put your code here
gt; End If
gt; End Sub
gt;
gt;
gt; --
gt; Paul B
gt; Always backup your data before trying something new
gt; Please post any response to the newsgroups so others can benefit from it
gt; Feedback on answers is always appreciated!
gt; Using Excel 2002 amp; 2003
gt;
gt; quot;Adam Kroger @hotmail.comgt;quot; lt;adam_krogerlt;nospamgt; wrote in message
gt; . ..
gt;gt; Can you execute a macro from inside a formula?
gt; IF(A1=quot;Yquot;,execute.macro,quot;quot;)
gt;gt;
gt;gt; Thanks
gt;gt;
gt;gt;
gt;
gt;
That cannot be a UDF as you are trying to write to many cells, a UDF only
returns a result.
--
HTH
Bob Phillips
(remove nothere from email address if mailing direct)
quot;Adam Kroger @hotmail.comgt;quot; lt;adam_krogerlt;nospamgt; wrote in message
. ..
gt; what would I need to change to allow that UDF to be used in any cell?
gt;
gt; This is what I want to run:
gt;
gt; Sub ROll2D6()
gt; Dim myCell As Range
gt; Dim Sides As Integer
gt; Dim Dies As Integer
gt; Dim i As Integer
gt; Dim myTemp As Integer
gt;
gt; Sides = 6
gt; Dies = 2
gt; For Each myCell In Selection
gt; Randomize
gt; myTemp = 0
gt; For i = 1 To Dies
gt; myTemp = myTemp Application.RoundUp(Rnd() * Sides, 0)
gt; Next i
gt; myCell.Value = myTemp
gt; Next myCell
gt; End Sub
gt;
gt;
gt; quot;Paul Bquot; gt; wrote in message
gt; ...
gt; gt; Adam, not with a formula, but you can use a worksheet change event like
gt; gt; this, put in sheet code
gt; gt;
gt; gt; Private Sub Worksheet_Change(ByVal Target As Range)
gt; gt; If Target.Count gt; 1 Then Exit Sub
gt; gt; If Target.Address = quot;$A$1quot; And UCase(Target.Value) = quot;Yquot; Then
gt; gt; 'put your code here
gt; gt; End If
gt; gt; End Sub
gt; gt;
gt; gt;
gt; gt; --
gt; gt; Paul B
gt; gt; Always backup your data before trying something new
gt; gt; Please post any response to the newsgroups so others can benefit from it
gt; gt; Feedback on answers is always appreciated!
gt; gt; Using Excel 2002 amp; 2003
gt; gt;
gt; gt; quot;Adam Kroger @hotmail.comgt;quot; lt;adam_krogerlt;nospamgt; wrote in message
gt; gt; . ..
gt; gt;gt; Can you execute a macro from inside a formula?
gt; gt; IF(A1=quot;Yquot;,execute.macro,quot;quot;)
gt; gt;gt;
gt; gt;gt; Thanks
gt; gt;gt;
gt; gt;gt;
gt; gt;
gt; gt;
gt;
gt;
The way the macro works as currently written, it can return to multiple
cells; but in this instance, I just need it to return either the sum of 2
randomly generated numbers between 1 and 6 (simulating a dice roll), or a
single randomly generated number between 1 and 6, and then have the cell be
stable after that is done (not constantly recalculating with every chnage
made to the spreadsheet).quot;Bob Phillipsquot; gt; wrote in message
...
gt; That cannot be a UDF as you are trying to write to many cells, a UDF only
gt; returns a result.
gt;
gt; --
gt; HTH
gt;
gt; Bob Phillips
gt;
gt; (remove nothere from email address if mailing direct)
gt;
gt; quot;Adam Kroger @hotmail.comgt;quot; lt;adam_krogerlt;nospamgt; wrote in message
gt; . ..
gt;gt; what would I need to change to allow that UDF to be used in any cell?
gt;gt;
gt;gt; This is what I want to run:
gt;gt;
gt;gt; Sub ROll2D6()
gt;gt; Dim myCell As Range
gt;gt; Dim Sides As Integer
gt;gt; Dim Dies As Integer
gt;gt; Dim i As Integer
gt;gt; Dim myTemp As Integer
gt;gt;
gt;gt; Sides = 6
gt;gt; Dies = 2
gt;gt; For Each myCell In Selection
gt;gt; Randomize
gt;gt; myTemp = 0
gt;gt; For i = 1 To Dies
gt;gt; myTemp = myTemp Application.RoundUp(Rnd() * Sides, 0)
gt;gt; Next i
gt;gt; myCell.Value = myTemp
gt;gt; Next myCell
gt;gt; End Sub
gt;gt;
gt;gt;
gt;gt; quot;Paul Bquot; gt; wrote in message
gt;gt; ...
gt;gt; gt; Adam, not with a formula, but you can use a worksheet change event like
gt;gt; gt; this, put in sheet code
gt;gt; gt;
gt;gt; gt; Private Sub Worksheet_Change(ByVal Target As Range)
gt;gt; gt; If Target.Count gt; 1 Then Exit Sub
gt;gt; gt; If Target.Address = quot;$A$1quot; And UCase(Target.Value) = quot;Yquot; Then
gt;gt; gt; 'put your code here
gt;gt; gt; End If
gt;gt; gt; End Sub
gt;gt; gt;
gt;gt; gt;
gt;gt; gt; --
gt;gt; gt; Paul B
gt;gt; gt; Always backup your data before trying something new
gt;gt; gt; Please post any response to the newsgroups so others can benefit from
gt;gt; gt; it
gt;gt; gt; Feedback on answers is always appreciated!
gt;gt; gt; Using Excel 2002 amp; 2003
gt;gt; gt;
gt;gt; gt; quot;Adam Kroger @hotmail.comgt;quot; lt;adam_krogerlt;nospamgt; wrote in message
gt;gt; gt; . ..
gt;gt; gt;gt; Can you execute a macro from inside a formula?
gt;gt; gt; IF(A1=quot;Yquot;,execute.macro,quot;quot;)
gt;gt; gt;gt;
gt;gt; gt;gt; Thanks
gt;gt; gt;gt;
gt;gt; gt;gt;
gt;gt; gt;
gt;gt; gt;
gt;gt;
gt;gt;
gt;
gt;
- Aug 07 Thu 2008 20:45
Using a macro question.
close
全站熱搜
留言列表
發表留言