目前 我在EXCEL 中 有 50個CheckBox: CheckBox1~CheckBox50VB 中需要對其進行判斷;IF CheckBox1=True then A=A+1IF CheckBox1=False then A=AIF CheckBox2=True then A=A+1IF CheckBox2=False then A=A.......IF CheckBox2=True then A=A+1IF CheckBox2=False then A=A以上 要寫 N遍。但用循環 : For n=1 to 50IF CheckBox+n =True then A=A+1IF CheckBox+n=False then A=Anext(以上 又完全錯誤,要如何實現循環啊,避免寫IF N回 )
1 回答

Qyouu
TA貢獻1786條經驗 獲得超11個贊
1:首先對象CheckBox不能加變量n,對象是對象,變量是變量,不能搞在一起+- × ÷。。。
2.CheckBox對象的值,是0或1,不是True或False。
3:如果你知道Excel中對象CheckBox個數是1~50,那可以這樣試試。
'必需先引用Microsoft Excel 11.0 Object Library
Dim x As Integer, wBook As Workbook, wSheet As Worksheet, AppExcel As Excel.Application
Set AppExcel = CreateObject("Excel.Application")
AppExcel.Visible = False'不可見
If AppExcel Is Nothing Then Exit Sub'創建對象失敗退出
Set wBook = AppExcel.Workbooks.Open(FileName)'文件名,路徑自己設置
Set wSheet = AppExcel.Sheets(1)'CheckBox對象在x個表格,那你設定x表格,這里先設1
For x = 1 To 50
if wSheet.OLEObjects(CStr("CheckBox" & x)).Object.Value=0 then
A=A
Else
A=A+1
Endif
Next
添加回答
舉報
0/150
提交
取消