View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Robert Logan Robert Logan is offline
external usenet poster
 
Posts: 3
Default Call subroutine based on combination of Option buttons (two groups)

Goal: Calculate average precipitation based on Month and Year.
Worksheets: "2016" and "2017"
Option Buttons: OptionButton1, OptionButton2, OptionButton3, OptionButton4,
Form: UserForm1

I have two groups of Option buttons on UserForm1 -- one group under "Month" (Frame1) and one group under "Year (Frame2).

I'd like to call a subroutine based on the combination of buttons selected.

For example, if a user selects January and selects 2016 (and then clicks "OK"), subroutine "January2016" is called. From there, I'd like to 1) activate sheet "2016" and 2) calculate the average precipitation for January and 3) put the value in the appropriate cell, in this case cell C9. Same for January 2017, etc.

Here's what "Form1" looks like. (The zeros are option buttons.)

Month Year [OK]
O January O 2016 [Cancel]
O February O 2017

Here's what the sheet, "2016" looks like. (Sheet "2017" is similar.)
-A- -B- -C-
1 Month Date Precip
2 Jan 01/01/17 5
3 Jan 01/15/17 10
4 Jan 01/31/17 15
5 Feb 02/01/17 20
6 Feb 02/15/17 30
7 Feb 02/28/17 40
8 ===============================
9 Jan AvgPrecip _____
10 Feb AvgPrecip _____

Here's my code. (It works so far but I haven't figured out how to go to the appropriate sheet and calculate the average rainfall.)

Sub CommandButton1_Click()
If Me.OptionButton1.Value = True And Me.OptionButton3.Value = True Then Call January2016

If Me.OptionButton1.Value = True And Me.OptionButton4.Value = True Then Call January2017

If Me.OptionButton2.Value = True And Me.OptionButton3.Value = True Then Call February2016

If Me.OptionButton2.Value = True And Me.OptionButton4.Value = True Then Call February2017
End Sub

Sub CommandButton2_Click()
Unload UserForm1
End Sub

Sub January2016()
MsgBox "January 2016"
' Go to Sheet 2016 and calculate average precipitation for January
End Sub

Sub January2017()
MsgBox "January 2017"
' Go to Sheet 2017 and calculate average precipitation for January
End Sub

Sub February2016()
MsgBox "February 2016"
' Go to Sheet 2017 and calculate average precipitation for January
End Sub

Sub February2017()
MsgBox "February 2017"
' Go to Sheet 2017 and calculate average precipitation for February
End Sub

Sub ClearOptionButtons()
UserForm1.OptionButton1.Value = False
UserForm1.OptionButton2.Value = False
UserForm1.OptionButton3.Value = False
UserForm1.OptionButton4.Value = False
End Sub