返回列表 上一主題 發帖

多選CheckBox的VBA怎麼寫?

回復 1# maikoyuki
  1. Private Sub CommandButton1_Click()
  2. Dim Ct As OLEObject
  3. For Each Ct In Me.OLEObjects
  4.   If Ct.progID = "Forms.CheckBox.1" Then
  5.    If Ct.Object.Value = True Then mystr = IIf(mystr = "", Ct.Object.Caption, mystr & "," & Ct.Object.Caption)
  6.   End If
  7. Next
  8. [B11] = mystr
  9. End Sub

  10. Private Sub CommandButton2_Click()
  11. For Each Ct In Me.OLEObjects
  12.   If Ct.progID = "Forms.CheckBox.1" Then
  13.    Ct.Object.Value = False
  14.   End If
  15. Next
  16. [B11] = ""
  17. End Sub
複製代碼
學海無涯_不恥下問

TOP

回復 3# stillfish00
沒錯不包含表單控制項
因為是在工作表中按鈕的程序,所以ME就是指該工作表
progID可選取該物件後,看編輯列中的刮號內文字
   
學海無涯_不恥下問

TOP

回復 7# c_c_lai


    2#程式碼適用於工作表內ActiveX控制項
請注意所謂表單控制項跟ActiveX控制項的區別
學海無涯_不恥下問

TOP

  1. Sub ex() '讀取勾選之表單CheckBox控制項
  2. Dim Sp As Shape
  3. For Each Sp In ActiveSheet.Shapes
  4.   If Sp.FormControlType = xlCheckBox Then
  5.   If Sp.ControlFormat.Value = 1 Then mystr = IIf(mystr = "", Sp.TextFrame.Characters.Text, mystr & "," & Sp.TextFrame.Characters.Text)
  6.   End If
  7. Next
  8. MsgBox mystr
  9. End Sub
  10. Sub ex1() '取消勾選CheckBox
  11. Dim Sp As Shape
  12. For Each Sp In ActiveSheet.Shapes
  13.   If Sp.FormControlType = xlCheckBox Then
  14.      Sp.ControlFormat.Value = False
  15.   End If
  16. Next
  17. End Sub
複製代碼
回復 9# c_c_lai
學海無涯_不恥下問

TOP

回復 6# maikoyuki
  1. Sub 清除控制項的值()
  2. Dim Ct As OLEObject, k
  3. For Each Ct In Me.OLEObjects
  4.    k = Ct.Object.Value
  5.    Ct.Object.Value = IIf(TypeName(k) = "Boolean", False, IIf(TypeName(k) = "String", "", Null))
  6. Next
  7. End Sub
複製代碼
學海無涯_不恥下問

TOP

        靜思自在 : 盡多少本份,就得多少本事。
返回列表 上一主題