返回列表 上一主題 發帖

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

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

想請問
一般而言
Private Sub UserForm_Initialize()
End Sub
為 初始狀態的時候的狀態~


Private Sub CommandButton1_Click()
End Sub

但如果今天是想以
Controls("Checkbox" & gosj) 為範圍
表單自動判斷而不用 CommandButton 模式下

該怎控制什麼比較適合??
程式如下

'主程式區塊
  1. Private Sub CommandButton1_Click()


  2.     Dim mycheckbox1 As MSForms.CheckBox
  3.           Columns("a:c").Clear
  4.           Columns("a:c").Select
  5.     For kk = 1 To 100

  6.         Cells(kk, 1) = "王" & kk
  7.          
  8.     Next kk

  9.      aois = ActiveSheet.Range("c1").CurrentRegion.Rows.Count

  10.         Selection.ColumnWidth = 15

  11.    
  12.         For sjy = 1 To 450 Step 45


  13.             For sja = 1 To 900 Step 90

  14.                 Set mycheckbox1 = UserForm1.Frame1.Controls.Add("forms.checkbox.1")

  15.                 With mycheckbox1

  16.                     .Left = 10 + sja

  17.                     .Top = 10 + sjy

  18.                     .Width = 70

  19.                     .Height = 20

  20.                     .TextAlign = fmTextAlignCenter

  21.                     .BackColor = &HFFFFC0

  22.                     .Caption = .Name

  23.                 End With

  24.             Next

  25.         Next

  26.     Dim ct As Control

  27.     r = 1

  28.     For Each ct In Controls

  29.     If TypeName(ct.Object) = "ICheckboxControl" Then ct.Caption = Cells(r, 1): Cells(r, 2) = ct.Name

  30.         Cells(r, 3) = ct.Name: r = r + 1

  31.     Next
  32.       aois = ActiveSheet.Range("c1").CurrentRegion.Rows.Count
  33.     For hh = 1 To aois

  34.         For kh = 1 To aois

  35.             If Cells(hh, 3) <> "" Then

  36.                 If Mid(Cells(hh, 3), 1, 8) <> "CheckBox" Then

  37.                     Range("c" & hh).Select

  38.                     Selection.Delete Shift:=xlUp

  39.                
  40.                 End If

  41.             End If

  42.         Next

  43.     Next
  44.      
  45.     Cells(1, 3).Select

  46. End Sub
複製代碼
'需判斷區塊
  1. Private Sub CommandButton2_Click()
  2.      Dim mycheckbox1 As MSForms.CheckBox
  3.      aois = ActiveSheet.Range("c1").CurrentRegion.Rows.Count
  4.      For gosj = 1 To aois
  5.         With Controls("Checkbox" & gosj)
  6.             If .Value = True Then
  7.                 Label1.Caption = "現在核選:" & .Name
  8.             End If
  9.         End With
  10.      Next

  11. End Sub
複製代碼
有試驗過

Private Sub UserForm_Activate()

End Sub

此方式 但無反映

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

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

  少看=只看不做也枉然

回復 13# GBKEE


    測試成功
感謝大大~
抱歉 小弟資質愚昧~

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

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

  少看=只看不做也枉然

TOP

回復 12# mark15jill
見笑了 2010 尚未摸過
2003 如圖


TOP

大大您好
小弟的版本2010 似乎會有些問題產生~(如 CLASS)

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

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

  少看=只看不做也枉然

TOP

本帖最後由 GBKEE 於 2012-10-19 12:13 編輯

回復 9# mark15jill
CommandButton2 改為UserForm_Click (UserForm_Active 只有表單顯示時才有的  )   將Label1.Caption  顯示目前核選之Checkbox 的  
可也是可以 條件是已有產生 10*10 個 Checkbox控制項

點選任一 Checkbox ,產生效果如同原始 CommandButton2 之效果相同
2#  使用物件類別  2# Private Sub CommandButton1_Click() 名稱 改成
Private Sub UserForm_Activate()  或是 Private Sub UserForm_Initialize()  皆可以
  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.Name
  6.     複選
  7. End Sub
  8. Private Sub 複選()
  9.     Dim xlCcaptionas As String, E As MSForms.Control
  10.     With UserForm1
  11.         For Each E In .Controls
  12.             If InStr(UCase(E.Name), UCase("checkbox")) Then
  13.                 If E.Value = True Then xlCcaptionas = IIf(xlCcaptionas = "", E.Name, xlCcaptionas & "," & E.Name)
  14.             End If
  15.         Next
  16.         If xlCcaptionas <> "" Then
  17.             .Label1.Caption = "現在核選:" & xlCcaptionas
  18.         Else
  19.             .Label1.Caption = ""
  20.         End If
  21.     End With
  22. End Sub
複製代碼

TOP

本帖最後由 mark15jill 於 2012-10-19 11:15 編輯

回復 8# GBKEE
  1. Private Sub CommandButton2_Click()

  2.      Dim mycheckbox1 As MSForms.CheckBox

  3.      aois = ActiveSheet.Range("c1").CurrentRegion.Rows.Count

  4.      For gosj = 1 To aois

  5.         With Controls("Checkbox" & gosj)

  6.             If .Value = True Then

  7.                 Label1.Caption = "現在核選:" & .Name

  8.             End If

  9.         End With

  10.      Next


  11. End Sub
複製代碼

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

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

  少看=只看不做也枉然

TOP

回復 8# GBKEE

抱歉 小弟說的不清楚~
再次說明小弟 的疑惑點~
如上面程式所示,目前是利用CommandButton2 作為觸發點(即 點CommandButton2  , Label1.Caption   才會顯示目前核選之Checkbox 的 Caption值 )  
若以UserForm_Active  為首的話,勢必會產生錯誤(因尚未 產生 10*10 個 Checkbox控制項 )

疑惑點如下
1.若想在 10*10 個 Checkbox控制項  產生後,點選任一 Checkbox ,產生效果如同原始 CommandButton2 之效果相同,但不用透過 CommandButton 控制,該用何者方式描寫比較適當。
2.若核選為複選,該如何判斷?

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

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

  少看=只看不做也枉然

TOP

回復 7# mark15jill
沒附檔,只能猜猜看
主程式區塊   是UserForm中  
Private Sub CommandButton1_Click()   改成  
Private Sub UserForm_Activate()  與 Private Sub UserForm_Initialize() 是一樣的
主程式區塊   是其他模組中
  1. Private Sub CommandButton1_Click()
  2. '
  3. '

  4.   Next
  5.    Cells(1, 3).Select
  6. UserForm1.Show   
  7. End Sub     
複製代碼

TOP

RE: 關於Checkbox核選判斷 [已解決]

感謝 GBKEE  的指教
有找到方法去判別..
  1. Private Sub CommandButton1_Click()
  2.     Dim mycheckbox1 As MSForms.CheckBox
  3.           Columns("a:c").Clear
  4.           Columns("a:c").Select
  5.     For kk = 1 To 100

  6.         Cells(kk, 1) = "王" & kk
  7.          
  8.     Next kk

  9.      aois = ActiveSheet.Range("c1").CurrentRegion.Rows.Count

  10.         Selection.ColumnWidth = 15

  11.    
  12.         For sjy = 1 To 450 Step 45


  13.             For sja = 1 To 900 Step 90

  14.                 Set mycheckbox1 = UserForm1.Frame1.Controls.Add("forms.checkbox.1")

  15.                 With mycheckbox1

  16.                     .Left = 10 + sja

  17.                     .Top = 10 + sjy

  18.                     .Width = 70

  19.                     .Height = 20

  20.                     .TextAlign = fmTextAlignCenter

  21.                     .BackColor = &HFFFFC0

  22.                     .Caption = .Name

  23.                 End With

  24.             Next

  25.         Next

  26.     Dim ct As Control

  27.     r = 1

  28.     For Each ct In Controls

  29.     If TypeName(ct.Object) = "ICheckboxControl" Then ct.Caption = Cells(r, 1): Cells(r, 2) = ct.Name

  30.         Cells(r, 3) = ct.Name: r = r + 1

  31.     Next
  32.       aois = ActiveSheet.Range("c1").CurrentRegion.Rows.Count
  33.     For hh = 1 To aois

  34.         For kh = 1 To aois

  35.             If Cells(hh, 3) <> "" Then

  36.                 If Mid(Cells(hh, 3), 1, 8) <> "CheckBox" Then

  37.                     Range("c" & hh).Select

  38.                     Selection.Delete Shift:=xlUp

  39.                
  40.                 End If

  41.             End If

  42.         Next

  43.     Next
  44.      
  45.     Cells(1, 3).Select


  46. End Sub
複製代碼

Private Sub CommandButton2_Click()
     Dim mycheckbox1 As MSForms.CheckBox
     aois = ActiveSheet.Range("c1").CurrentRegion.Rows.Count
     For gosj = 1 To aois
        With Controls("Checkbox" & gosj)
            If .Value = True Then
                Label1.Caption = "現在核選:" & .Name
            
            End If
        End With
     Next
End Sub

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

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

  少看=只看不做也枉然

TOP

回復 4# GBKEE

抱歉 讓大大您見笑了~
因最近  VB2010 和 VBA 同時用到
有些語法 語串 導致常常會混亂
如 同一事件 兩邊的語法可能就有差異
有試著修正  但跑出來的成果不盡理想

再次感謝大大的耐心解說

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

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

  少看=只看不做也枉然

TOP

        靜思自在 : 看別人不順眼,是自己修養不夠。
返回列表 上一主題