Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9
Default Creating macros with if stmts

I am new to macros and I need to create a macro with an if statement. What
is the correct format, including spacing to make this work? I know the
wordin is If and else, but I can't seem to get the right format. Lets say if
A1=1 I want the first macro to work, if A1=2 I want the 2nd macro to work,
and if A1=3 the third.

Sub

Macro

End Sub

Sub

Macro

End Sub

Sub

Macro

End Sub

Any help would be appreciated, like I said I just can't quite make it work.

Thank you,

Lisa12
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,501
Default Creating macros with if stmts

Hi,

There are many ways to do this. Here's one. You would of course change the
msgbox in this example to your code.

Sub macro()
Select Case Range("A1").Value
Case Is = 1
macro1
Case Is = 2
macro2
Case Is = 3
macro3
Case else
'Do soemthing else
End Select
End Sub

Sub macro1()
MsgBox 1
End Sub

Sub macro2()
MsgBox 2
End Sub

Sub macro3()
MsgBox 3
End Sub

Mike

"Lisa12" wrote:

I am new to macros and I need to create a macro with an if statement. What
is the correct format, including spacing to make this work? I know the
wordin is If and else, but I can't seem to get the right format. Lets say if
A1=1 I want the first macro to work, if A1=2 I want the 2nd macro to work,
and if A1=3 the third.

Sub

Macro

End Sub

Sub

Macro

End Sub

Sub

Macro

End Sub

Any help would be appreciated, like I said I just can't quite make it work.

Thank you,

Lisa12

  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,124
Default Creating macros with if stmts

You could/should make it one macro but
Sub selectmacro()
Select Case Range("a1").Value
Case 1: macro1
Case 2: macro2
Case 3: macro2
Case Else
End Select
End Sub

Sub macro1()
MsgBox "1"
End Sub
Sub macro2()
MsgBox "2"
End Sub
Sub macro3()
MsgBox "3"
End Sub



--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Lisa12" wrote in message
...
I am new to macros and I need to create a macro with an if statement. What
is the correct format, including spacing to make this work? I know the
wordin is If and else, but I can't seem to get the right format. Lets say
if
A1=1 I want the first macro to work, if A1=2 I want the 2nd macro to work,
and if A1=3 the third.

Sub

Macro

End Sub

Sub

Macro

End Sub

Sub

Macro

End Sub

Any help would be appreciated, like I said I just can't quite make it
work.

Thank you,

Lisa12


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 42
Default Creating macros with if stmts

Lisa12,

If you want to get the macros to run just by changing the value in the cell
then you will probably have to use a slightly more complex approach, than the
previous posts.

Open the Visual basic Editor and double-click on Sheet1 in the Project
Explorer pane. This will open a code page associated with sheet1.
Click the left hand side dropdown box at the top and select 'Worksheet'.
Then click the right hand dropdown box and select 'Change'

Then use something like this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target = Application.Range("A1") Then
If Range("a1").Value = 1 Then 'Alternatively it can all go on one
line as in the second case
macro1
End If
If Range("a1").Value = 2 Then macro2
If Range("a1").Value = 3 Then macro3
End If
End Sub

Sub macro1()
MsgBox "Macro1"
End Sub

Sub macro2()
MsgBox "Macro2"
End Sub

Sub macro3()
MsgBox "Macro3"
End Sub


"Lisa12" wrote:

I am new to macros and I need to create a macro with an if statement. What
is the correct format, including spacing to make this work? I know the
wordin is If and else, but I can't seem to get the right format. Lets say if
A1=1 I want the first macro to work, if A1=2 I want the 2nd macro to work,
and if A1=3 the third.

Sub

Macro

End Sub

Sub

Macro

End Sub

Sub

Macro

End Sub

Any help would be appreciated, like I said I just can't quite make it work.

Thank you,

Lisa12

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
creating a report with macros Gemz Excel Worksheet Functions 7 January 18th 08 02:11 PM
macros creating NSNR Excel Discussion (Misc queries) 2 October 26th 07 08:19 AM
Monthly bank stmts KathyT Excel Worksheet Functions 5 January 3rd 07 05:32 AM
Creating Inventory Macros Rubix Excel Worksheet Functions 2 March 15th 06 01:02 AM
IF Statement that's conditional on multiple IF Stmts in other cell Pat Excel Worksheet Functions 1 March 26th 05 10:31 PM


All times are GMT +1. The time now is 07:53 PM.

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"