PDA

View Full Version : Solved: condense long code



gabethegrape
10-17-2010, 11:23 PM
I have this long bit of code that's part of a bigger algorithm and I'm trying to condense it because it's getting out of hand. Basically, I want the equation to produce 5 types of graphs - the first (cboIntervals.value = 1) is linear. The 2nd to 5th (cboIntervals.value = 2 to 5) are like steps. Any ideas? Can I use a loop statements in combination with Case? Thanks for your help!



If cboIntervals.Value = 1 Then


Select Case cboSessions.Value

Case 1 To 2
txtDivision.Value = 1
Case 3
txtDivision.Value = 2
Case 4
txtDivision.Value = 3
Case 5
txtDivision.Value = 4
Case 6
txtDivision.Value = 5
Case 7
txtDivision.Value = 6
Case 8
txtDivision.Value = 7
Case 9
txtDivision.Value = 8
Case 10
txtDivision.Value = 9
Case 11
txtDivision.Value = 10
Case 12
txtDivision.Value = 11
Case 13
txtDivision.Value = 12
Case 14
txtDivision.Value = 13
Case 15
txtDivision.Value = 14
Case 16
txtDivision.Value = 15
Case 17
txtDivision.Value = 16
Case 18
txtDivision.Value = 17
Case 19
txtDivision.Value = 18
Case 20
txtDivision.Value = 19
Case 21
txtDivision.Value = 20
Case 22
txtDivision.Value = 21
Case 23
txtDivision.Value = 22
Case 24
txtDivision.Value = 23
Case 25
txtDivision.Value = 24

End Select

ElseIf cboIntervals.Value = 2 Then

Select Case cboSessions.Value

Case 1 To 4
txtDivision.Value = 1
Case 5 To 6
txtDivision.Value = 2
Case 7 To 8
txtDivision.Value = 3
Case 9 To 10
txtDivision.Value = 4
Case 11 To 12
txtDivision.Value = 5
Case 13 To 14
txtDivision.Value = 6
Case 15 To 16
txtDivision.Value = 7
Case 17 To 18
txtDivision.Value = 8
Case 19 To 20
txtDivision.Value = 9
Case 21 To 22
txtDivision.Value = 10
Case 23 To 24
txtDivision.Value = 11
Case 25
txtDivision.Value = 12

End Select

ElseIf cboIntervals.Value = 3 Then

Select Case cboSessions.Value

Case 1 To 6
txtDivision.Value = 1
Case 7 To 9
txtDivision.Value = 2
Case 10 To 12
txtDivision.Value = 3
Case 13 To 15
txtDivision.Value = 4
Case 16 To 18
txtDivision.Value = 5
Case 19 To 21
txtDivision.Value = 6
Case 22 To 24
txtDivision.Value = 7
Case 25
txtDivision.Value = 8

End Select

ElseIf cboIntervals.Value = 4 Then

Select Case cboSessions.Value

Case 1 To 8
txtDivision.Value = 1
Case 9 To 12
txtDivision.Value = 2
Case 13 To 16
txtDivision.Value = 3
Case 17 To 20
txtDivision.Value = 4
Case 21 To 24
txtDivision.Value = 5
Case 25
txtDivision.Value = 6

End Select

ElseIf cboIntervals.Value = 5 Then

Select Case cboSessions.Value

Case 1 To 10
txtDivision.Value = 1
Case 11 To 15
txtDivision.Value = 2
Case 16 To 20
txtDivision.Value = 3
Case 21 To 25
txtDivision.Value = 4

End Select

End If

macropod
10-17-2010, 11:56 PM
Hi gabethegrape,

Try something along the lines of:

Select Case Round(cboSessions.Value / cboIntervals.Value)
Case 1
txtDivision.Value = 1
Case Else
txtDivision.Value = txtDivision.Value - 1
End Select

Edited for even simpler code.

gabethegrape
10-18-2010, 01:02 AM
ha! brilliant. works perfectly. thank you!

macropod
10-18-2010, 01:06 AM
You're welcome.