Excel 當有很多CheckBox1_Click時,要如何簡化程式
該如何簡化以下程式
Private Sub CheckBox1_Click()
If CheckBox1.Value = False Then
TextBox1.Enabled = False
TextBox1 = ""
TextBox2 = ""
Else
TextBox1.Enabled = True
End If
TextBox270 = Str(Val(TextBox2) + Val(TextBox4) + Val(TextBox6) + _
Val(TextBox8) + Val(TextBox10) + Val(TextBox12) + Val(TextBox14) + _
Val(TextBox16) + Val(TextBox18) + Val(TextBox20) + Val(TextBox22) + _
Val(TextBox24) + Val(TextBox26))
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = False Then
TextBox3.Enabled = False
TextBox3 = ""
TextBox4 = ""
Else
TextBox3.Enabled = True
End If
TextBox270 = Str(Val(TextBox2) + Val(TextBox4) + Val(TextBox6) + _
Val(TextBox8) + Val(TextBox10) + Val(TextBox12) + Val(TextBox14) + _
Val(TextBox16) + Val(TextBox18) + Val(TextBox20) + Val(TextBox22) + _
Val(TextBox24) + Val(TextBox26))
End Sub
Private Sub CheckBox3_Click()
If CheckBox3.Value = False Then
TextBox5.Enabled = False
TextBox5 = ""
TextBox6 = ""
Else
TextBox5.Enabled = True
End If
TextBox270 = Str(Val(TextBox2) + Val(TextBox4) + Val(TextBox6) + _
Val(TextBox8) + Val(TextBox10) + Val(TextBox12) + Val(TextBox14) + _
Val(TextBox16) + Val(TextBox18) + Val(TextBox20) + Val(TextBox22) + _
Val(TextBox24) + Val(TextBox26))
End Sub
2011-05-13 22:06:46 補充
To:chu 大大
我不會使用陣列
將重複的句子獨立寫出一個,再利用迴圈來執行
如下方式:
Sub 使用迴圈()
Dim A%, V%, B%, Y%
V = 1
For A = 1 To 3
If UserForm1.Controls("CheckBox" & A).Value = False Then ' UserForm1為表單
UserForm1.Controls("TextBox" & V).Enabled = False ' UserForm1為表單
UserForm1.Controls("TextBox" & V).Value = "" ' UserForm1為表單
UserForm1.Controls("TextBox" & V + 1).Value = "" ' UserForm1為表單
Else
UserForm1.Controls("TextBox" & V).Enabled = True ' UserForm1為表單
End If
V = V + 2
Next A
B = 0
For Y = 1 To 14
B = B + Str(Val(UserForm1.Controls("TextBox" & Y * 2).Value)) ' UserForm1為表單
Next Y
TextBox270 = B
End Sub
Private Sub CheckBox1_Click()
使用迴圈
End Sub
Private Sub CheckBox2_Click()
使用迴圈
End Sub
Private Sub CheckBox3_Click()
使用迴圈
End Sub
留言
張貼留言