返回列表 上一主題 發帖

[發問] 請教 關於 程式開頭 的相關疑惑(承接 CheckBox 核選問題)

回復 3# mark15jill
你附檔模組 沒加上  Option Explicit  所以我測試時是沒問題
依你的能力,是可以試著修改的
2#
  1. Private Sub CommandButton1_Click()
  2.     Dim mycheckbox1 As MSForms.CheckBox
  3.     Dim Sh As Worksheet
  4.     Set Sh = ActiveSheet ' **少了這程式碼請加入 **
複製代碼

TOP

回復 2# GBKEE

大大 謝謝您的解答。
但出現  ReDim Preserve TheCheck(0 To         變數未定義

  多做多想多學習,少看少錯少迷途

  多做=多多練習,多多編寫。
  多想=想想為什麼人家程式要那樣寫,如果換成自己,又會怎寫。
  多學習=學習人家的發問並解答,學習人家的寫法

  少看=只看不做也枉然

TOP

本帖最後由 GBKEE 於 2012-10-18 09:15 編輯

回復 1# mark15jill
使用物件類別 試試看
  1. Dim TheCheck() As New Class1  '動態陣列 宣告為新的Class1物件類別物件
  2. Private Sub CommandButton1_Click()
  3.     Dim mycheckbox1 As MSForms.CheckBox
  4.     Dim Sh As Worksheet
  5.      Set Sh = ActiveSheet   
  6.     Sh.Columns("c:c").Clear
  7.     Sh.Columns("c:c").ColumnWidth = 15
  8.     kk = 1
  9.     For sjy = 1 To 450 Step 45
  10.         For sja = 1 To 900 Step 90
  11.             With Frame1.Controls.Add("forms.checkbox.1")
  12.                 Sh.Cells(kk, 1) = "王" & kk
  13.                 Sh.Cells(kk, 3) = .Name
  14.                 .Left = 10 + sja
  15.                 .Top = 10 + sjy
  16.                 .Width = 70
  17.                 .Height = 20
  18.                 .TextAlign = fmTextAlignCenter
  19.                 .BackColor = &HFFFFC0
  20.                 .Caption = Sh.Cells(kk, 1)
  21.                 ReDim Preserve TheCheck(0 To kk - 1)              '重新配置 動態陣列
  22.                 Set TheCheck(kk - 1).xlCheckbox = Controls(.Name)
  23.                 '動態陣列的元素 指定到物件模組內的物件
  24.             End With
  25.             kk = kk + 1
  26.         Next
  27.     Next
  28. End Sub
複製代碼
  1. 'VBA插入 物件類別模組 自動命名為Class1 的程式碼
  2. Option Explicit
  3. Public WithEvents xlCheckbox As MSForms.CheckBox
  4. Private Sub xlcheckbox_Click()
  5.     If xlCheckbox = True Then UserForm1.Label1.Caption = xlCheckbox.Caption
  6. End Sub
複製代碼

TOP

關於Checkbox核選判斷

已設 10 * 10 個 Checkbox 控制項
想請問  如何隨機核選時 在Label1.Caption  出現該控制項之Caption 內容。
  1. Private Sub CommandButton1_Click()
  2.     Dim mycheckbox1 As MSForms.CheckBox
  3.     For kk = 1 To 100
  4.         Cells(kk, 1) = "王" & kk
  5.     Next kk
  6.          Columns("c:c").Clear
  7.           Columns("c:c").Select
  8.         Selection.ColumnWidth = 15
  9.    
  10.         For sjy = 1 To 450 Step 45

  11.             For sja = 1 To 900 Step 90
  12.                 Set mycheckbox1 = UserForm1.Frame1.Controls.Add("forms.checkbox.1")
  13.                 With mycheckbox1
  14.                     .Left = 10 + sja
  15.                     .Top = 10 + sjy
  16.                     .Width = 70
  17.                     .Height = 20
  18.                     .TextAlign = fmTextAlignCenter
  19.                     .BackColor = &HFFFFC0
  20.                     .Caption = .Name
  21.                 End With
  22.             Next
  23.         Next
  24.     Dim ct As Control

  25.     r = 1
  26.     For Each ct In Controls
  27.     If TypeName(ct.Object) = "ICheckboxControl" Then ct.Caption = Cells(r, 1): Cells(r, 2) = ct.Name
  28.         Cells(r, 3) = ct.Name: r = r + 1
  29.     Next
  30.      aois = ActiveSheet.Range("c1").CurrentRegion.Rows.Count
  31.     For hh = 1 To aois
  32.         For kh = 1 To aois
  33.             If Cells(hh, 3) <> "" Then
  34.                 If Mid(Cells(hh, 3), 1, 8) <> "CheckBox" Then
  35.                     Range("c" & hh).Select
  36.                     Selection.Delete Shift:=xlUp
  37.                
  38.                 End If
  39.             End If
  40.         Next
  41.     Next
  42.         
  43.    
  44.     Cells(1, 3).Select
  45. End Sub
複製代碼
自創checkbox.rar (19.46 KB)

  多做多想多學習,少看少錯少迷途

  多做=多多練習,多多編寫。
  多想=想想為什麼人家程式要那樣寫,如果換成自己,又會怎寫。
  多學習=學習人家的發問並解答,學習人家的寫法

  少看=只看不做也枉然

TOP

        靜思自在 : 【時間無法遮擋】怕時間消逝,花了許多心血,想盡各式方法要遮擋時間,結果是:浪費了更多時間,且一無所成!
返回列表 上一主題