Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 287
Default assigning macro to cell

Is it possible to run a macro depending on the input in a cell?
I would like to run:
Macro 1 if I input 2005 in cell b4
Macro 2 if I input 2006 in cell b4
Macro 3 if I input 2007 in cell b4

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default assigning macro to cell

Adam,
Add this code to the sheet which contains the B4 cell . In VBA
project window, right click on (say) Sheet1, click View Code and copy this
code into the sheet.


Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Target = Range("$b$4") Then
With Target
Select Case .Value ' Assumes cell format is General
Case Is = 2005
Call Macro1
Case Is = 2006
Call Macro2
Case Is = 2007
Call Macro3
End Select
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub



HTH

"Adam" wrote:

Is it possible to run a macro depending on the input in a cell?
I would like to run:
Macro 1 if I input 2005 in cell b4
Macro 2 if I input 2006 in cell b4
Macro 3 if I input 2007 in cell b4

Thanks

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default assigning macro to cell

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
With Target
Select Case .Value
Case 2005: Macro1
Case 2006: Macro2
Case 2007: Macro3
End Select
End With

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Adam" wrote in message
...
Is it possible to run a macro depending on the input in a cell?
I would like to run:
Macro 1 if I input 2005 in cell b4
Macro 2 if I input 2006 in cell b4
Macro 3 if I input 2007 in cell b4

Thanks



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default assigning macro to cell

Your IF statement says

IF Target.Value = Range("$b$4").Value

so any cell having the same value as Cell B4 (including B4) will execute the
case statement. I doubt that is what your intent is (and it isn't the
stated intent of the OP).

Try

If Target.Address = "$B$4" Then

Instead

--
Regards,
Tom Ogilvy


"Toppers" wrote in message
...
Adam,
Add this code to the sheet which contains the B4 cell . In

VBA
project window, right click on (say) Sheet1, click View Code and copy this
code into the sheet.


Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Target = Range("$b$4") Then
With Target
Select Case .Value ' Assumes cell format is General
Case Is = 2005
Call Macro1
Case Is = 2006
Call Macro2
Case Is = 2007
Call Macro3
End Select
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub



HTH

"Adam" wrote:

Is it possible to run a macro depending on the input in a cell?
I would like to run:
Macro 1 if I input 2005 in cell b4
Macro 2 if I input 2006 in cell b4
Macro 3 if I input 2007 in cell b4

Thanks



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default assigning macro to cell

I am sure Bob meant

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
If Target.Address = "$B$4" Then
Application.EnableEvents = False
With Target
Select Case .Value
Case 2005: Macro1
Case 2006: Macro2
Case 2007: Macro3
End Select
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub

--
Regards,
Tom Ogilvy


"Bob Phillips" wrote in message
...
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
With Target
Select Case .Value
Case 2005: Macro1
Case 2006: Macro2
Case 2007: Macro3
End Select
End With

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Adam" wrote in message
...
Is it possible to run a macro depending on the input in a cell?
I would like to run:
Macro 1 if I input 2005 in cell b4
Macro 2 if I input 2006 in cell b4
Macro 3 if I input 2007 in cell b4

Thanks







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default assigning macro to cell

Indeed, thanks Tom.

Bob


"Tom Ogilvy" wrote in message
...
I am sure Bob meant

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
If Target.Address = "$B$4" Then
Application.EnableEvents = False
With Target
Select Case .Value
Case 2005: Macro1
Case 2006: Macro2
Case 2007: Macro3
End Select
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub

--
Regards,
Tom Ogilvy


"Bob Phillips" wrote in message
...
Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
With Target
Select Case .Value
Case 2005: Macro1
Case 2006: Macro2
Case 2007: Macro3
End Select
End With

ws_exit:
Application.EnableEvents = True
End Sub

'This is worksheet event code, which means that it needs to be
'placed in the appropriate worksheet code module, not a standard
'code module. To do this, right-click on the sheet tab, select
'the View Code option from the menu, and paste the code in.


--

HTH

RP
(remove nothere from the email address if mailing direct)


"Adam" wrote in message
...
Is it possible to run a macro depending on the input in a cell?
I would like to run:
Macro 1 if I input 2005 in cell b4
Macro 2 if I input 2006 in cell b4
Macro 3 if I input 2007 in cell b4

Thanks







  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default assigning macro to cell

Thanks Tom. .. Too much of a hurry!

"Tom Ogilvy" wrote:

Your IF statement says

IF Target.Value = Range("$b$4").Value

so any cell having the same value as Cell B4 (including B4) will execute the
case statement. I doubt that is what your intent is (and it isn't the
stated intent of the OP).

Try

If Target.Address = "$B$4" Then

Instead

--
Regards,
Tom Ogilvy


"Toppers" wrote in message
...
Adam,
Add this code to the sheet which contains the B4 cell . In

VBA
project window, right click on (say) Sheet1, click View Code and copy this
code into the sheet.


Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ws_exit:
Application.EnableEvents = False
If Target = Range("$b$4") Then
With Target
Select Case .Value ' Assumes cell format is General
Case Is = 2005
Call Macro1
Case Is = 2006
Call Macro2
Case Is = 2007
Call Macro3
End Select
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub



HTH

"Adam" wrote:

Is it possible to run a macro depending on the input in a cell?
I would like to run:
Macro 1 if I input 2005 in cell b4
Macro 2 if I input 2006 in cell b4
Macro 3 if I input 2007 in cell b4

Thanks




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
assigning value in macro tina Excel Programming 4 October 29th 09 03:33 PM
assigning macro carrera Excel Discussion (Misc queries) 16 March 21st 08 09:00 PM
Assigning A Macro to A Button Holly Excel Worksheet Functions 1 January 18th 08 10:04 AM
Assigning Macro Problem mai[_2_] Excel Programming 4 September 3rd 04 05:24 PM
Macro assigning help PLEASE Jim Rech Excel Programming 0 February 10th 04 10:03 PM


All times are GMT +1. The time now is 07:31 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"