ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Using a macro question. (https://www.excelbanter.com/excel-discussion-misc-queries/60456-using-macro-question.html)

Adam Kroger

Using a macro question.
 
Can you execute a macro from inside a formula? IF(A1="Y",execute.macro,"")

Thanks



Paul B

Using a macro question.
 
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 1 Then Exit Sub
If Target.Address = "$A$1" And UCase(Target.Value) = "Y" 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 & 2003

"Adam Kroger @hotmail.com" <adam_kroger<nospam wrote in message
. ..
Can you execute a macro from inside a formula?

IF(A1="Y",execute.macro,"")

Thanks





Bob Phillips

Using a macro question.
 
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)

"Adam Kroger @hotmail.com" <adam_kroger<nospam wrote in message
. ..
Can you execute a macro from inside a formula?

IF(A1="Y",execute.macro,"")

Thanks





Biff

Using a macro question.
 
No.

Biff

"Adam Kroger @hotmail.com" <adam_kroger<nospam wrote in message
. ..
Can you execute a macro from inside a formula?
IF(A1="Y",execute.macro,"")

Thanks




Adam Kroger

Using a macro question.
 
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 Sub


"Paul B" wrote in message
...
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 1 Then Exit Sub
If Target.Address = "$A$1" And UCase(Target.Value) = "Y" 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 & 2003

"Adam Kroger @hotmail.com" <adam_kroger<nospam wrote in message
. ..
Can you execute a macro from inside a formula?

IF(A1="Y",execute.macro,"")

Thanks







Bob Phillips

Using a macro question.
 
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)

"Adam Kroger @hotmail.com" <adam_kroger<nospam wrote in message
. ..
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 Sub


"Paul B" wrote in message
...
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 1 Then Exit Sub
If Target.Address = "$A$1" And UCase(Target.Value) = "Y" 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 & 2003

"Adam Kroger @hotmail.com" <adam_kroger<nospam wrote in message
. ..
Can you execute a macro from inside a formula?

IF(A1="Y",execute.macro,"")

Thanks









Adam Kroger

Using a macro question.
 
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).


"Bob Phillips" wrote in message
...
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)

"Adam Kroger @hotmail.com" <adam_kroger<nospam wrote in message
. ..
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 Sub


"Paul B" wrote in message
...
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 1 Then Exit Sub
If Target.Address = "$A$1" And UCase(Target.Value) = "Y" 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 & 2003

"Adam Kroger @hotmail.com" <adam_kroger<nospam wrote in message
. ..
Can you execute a macro from inside a formula?
IF(A1="Y",execute.macro,"")

Thanks












All times are GMT +1. The time now is 08:28 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com