2014-10-08 60 views
0

我正在嘗試編寫消息的代碼,以便在複選框被選中時彈出。我的電子表格有很多複選框,我只希望它爲H列中的複選框執行此操作。我相信最好的方法可能是使用類似以下代碼的方式將宏指派給所有使用vba的複選框(除了這不是工作):vba - Msg if if if checked - but not if unchecked

ActiveSheet.Shapes.Range(Array("h9:h89")).Select 
    Selection.OnAction = "Sheet4.Checkbox" 

我的第二個問題是,我只希望消息彈出時檢查,而不是未選中時。當宏被分配到手動時,以下工作,但爲檢查和取消選中。註釋掉的部分是我嘗試過的一些事情的例子。我相信他們是ActiveX複選框。

Sub Checkbox() 

' Dim rangeVar As Range 
' 
' rangeVar = ("h9:h89") 
' 
'With rangeVar 
'  If WorksheetFunction.Or(.Cells) = True Then 
      MsgBox ("Are you sure you want to check this box?") 
      Exit Sub 
'  End If 
'End With 

' Dim chk As Checkbox 
' 
' For Each chk In rangeVar 
' If chk.Value = True Then 
'  MsgBox ("Are you sure you want to check this box?") 
'  Exit Sub 
' End If 
' Next chk 

End Sub 

謝謝!

回答

0

分配的作用:

Sub AssignClicks() 
    Dim cb 
    For Each cb In ActiveSheet.CheckBoxes 
     If Not Application.Intersect(cb.TopLeftCell, _ 
         ActiveSheet.Range("H:H")) Is Nothing Then 
      cb.OnAction = "Sheet1.ClickedIt" 

     End If 
    Next cb 
End Sub 

在表單代碼模塊:

Sub ClickedIt() 
    Dim cb As CheckBox 
    Set cb = Me.CheckBoxes(Application.Caller) 
    If cb.Value = 1 Then 
     MsgBox "checkbox at " & cb.TopLeftCell.Address() & " is checked" 
    End If 
End Sub 

...或者你可以將動作分配給所有的複選框,並使用代碼ClickedIt以確定是否目前的複選框在Col H.