Reducing repetition

zerdan

New Member
Joined
Apr 12, 2011
Messages
7
I have this very repetitive code that I'm trying to figure out how to do more effectively. The idea behind it is that there are 7 radio buttons (one for each day of the week) and then a number of checkboxes. When the user selects a radio button it automatically recalls the checkboxes that are "TRUE/FALSE" for that day.

The checkboxes are added/removing 0.5 or 1 to cells and the range of the cells that they add these to increases by 24 for optionbutton (day).

This code has to be recycled for a bunch of checkboxes. What I started out doing (as you can see if you look at this sample) is changing the values by hand for each checkbox, but then I got to thinking there must be a way to do this with variables since it is so repetitive.

I was thinking something like

Variable X = AD8 (starting cell for optionbutton1)
Then it would add 24 to this for each multiplier of the optionbutton.

Any advice would be greatly appreciated!

Code:
Private Sub CheckBox6_Click()
With Sheets("Hours")
If OptionButton1 = True Then
 If CheckBox6 = True Then
  .Range("AD176") = True
  .Range("AD8").Value = 0.5
  .Range("AD9:AD19").Value = 1
  .Range("AD20").Value = 0.5
  Else
  .Range("AB176") = False
  .Range("AB8:AB20").Clear
  End If
ElseIf OptionButton2 = True Then
 If CheckBox4 = True Then
  .Range("AB177") = True
  .Range("AB31").Value = 0.5
  .Range("AB32:AB42").Value = 1
  .Range("AB43").Value = 0.5
  Else
  .Range("AB177") = False
  .Range("AB31:AB43").Clear
  End If
  ElseIf OptionButton3 = True Then
 If CheckBox4 = True Then
  .Range("AB178") = True
  .Range("AB55").Value = 0.5
  .Range("AB56:AB66").Value = 1
  .Range("AB67").Value = 0.5
  Else
  .Range("AB178") = False
  .Range("AB55:AB67").Clear
  End If
   ElseIf OptionButton4 = True Then
 If CheckBox4 = True Then
  .Range("AB179") = True
  .Range("AB79").Value = 0.5
  .Range("AB80:AB90").Value = 1
  .Range("AB91").Value = 0.5
  Else
  .Range("AB179") = False
  .Range("AB79:AB91").Clear
  End If
  ElseIf OptionButton5 = True Then
 If CheckBox4 = True Then
  .Range("AB180") = True
  .Range("AB103").Value = 0.5
  .Range("AB104:AB114").Value = 1
  .Range("AB115").Value = 0.5
  Else
  .Range("AB180") = False
  .Range("AB103:AB115").Clear
  End If
ElseIf OptionButton6 = True Then
   If CheckBox4 = True Then
  .Range("AB181") = True
  .Range("AB127").Value = 0.5
  .Range("AB128:AB138").Value = 1
  .Range("AB139").Value = 0.5
  Else
  .Range("AB181") = False
  .Range("AB127:AB139").Clear
  End If
ElseIf OptionButton7 = True Then
   If CheckBox4 = True Then
  .Range("AB182") = True
  .Range("AB151").Value = 0.5
  .Range("AB152:AB162").Value = 1
  .Range("AB163").Value = 0.5
  Else
  .Range("AB182") = False
  .Range("AB152:AB163").Clear
  End If
End If
End With
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,224,584
Messages
6,179,693
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top