是否可以使用vba来引用和禁用多个activex复选框

时间:2016-01-30 21:50:28

标签: vba excel-vba activex excel

我有一个工作簿,我想在其中禁用activex复选框。连续1个复选框确定是否启用或禁用该行中的所有其他复选框 我的代码工作正常但效率很低。有没有办法在不指定每个复选框的情况下引用多个activeX复选框。 在这个阶段,我有22行,每行重复这段代码。然后再次启用相同的复选框。你的帮助将不胜感激。以下是我需要帮助简化的代码部分。

If .Row = 1 Then
     ActiveSheet.OLEObjects("AttendMonday5").Enabled = False
     ActiveSheet.OLEObjects("AttendTuesday5").Enabled = False
     ActiveSheet.OLEObjects("AttendWednesday5").Enabled = False
     ActiveSheet.OLEObjects("AttendThursday5").Enabled = False
     ActiveSheet.OLEObjects("AttendFriday5").Enabled = False
     ActiveSheet.OLEObjects("Monday5").Enabled = False
     ActiveSheet.OLEObjects("Tuesday5").Enabled = False
     ActiveSheet.OLEObjects("Wednesday5").Enabled = False
     ActiveSheet.OLEObjects("Thursday5").Enabled = False
     ActiveSheet.OLEObjects("Friday5").Enabled = False
  End If

3 个答案:

答案 0 :(得分:1)

您可以使用对象数组来实现它,如下所示:

If .Row = 1 Then
  ActiveSheet.OLEObjects(Array("AttendMonday5", "AttendTuesday5", "AttendWednesday5", "AttendThursday5", "AttendFriday5", "Monday5", "Tuesday5", "Wednesday5", "Thursday5", "Friday5")).Enabled = False
End If

答案 1 :(得分:1)

修改

将此宏指定给所有表单控件复选框。表单控件复选框需要链接到列BH中的正确行。代码检查列BH中的所有已填充单元格,并启用或禁用行中的所有ActiveX复选框,前提是ActiveX控件在其名称末尾具有正确的行号(不要与控件混淆)字幕)。如果添加新行,则不需要添加代码,只需确保新控件的名称正确,并将此宏分配给新行上的表单控件。

Sub enableCheckboxes()
'Macro to enable or disable a row of activeX checkboxes based on a cell value in that row

    'Declaration of variable
    Dim rngLinked As Range, rngRow As Range
    Dim enableRow As Boolean
    Dim ws As Worksheet
    Dim chkBox As oleobject
    Dim rowNum As Long, rowNumLength As Long

    'Setting of object variables
    Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change to the actual name of your sheet.
    With ws
        Set rngLinked = .Range(.Cells(1, 60), .Cells(.Rows.Count, 60).End(xlUp)) 'Range set to column BH
    End With

    'Nested loops enable disable objects
    Application.ScreenUpdating = False 'Prevent screenupdating
    For Each rngRow In rngLinked 'Check column BH
        enableRow = rngRow 'See if has to be enabled or not
        rowNum = rngRow.Row 'Row to enable or disable
        rowNumLength = Len(CStr(rowNum)) 'Used to extract the rownumber from the checkbox' names
        For Each chkBox In ws.OLEObjects 'Iterate through the OLE objects on the sheet.
            If TypeName(chkBox.Object) = "CheckBox" And CLng(Right(chkBox.Name, rowNumLength)) = rowNum Then 'Check if object is checkbox and is on the current row
                If enableRow Then 'Check to enable or disable object
                    chkBox.Enabled = True
                Else
                    chkBox.Enabled = False
                End If
            End If
        Next chkBox
    Next rngRow
    Application.ScreenUpdating = True

End Sub

编辑结束

您可以尝试类似

的内容
Option Explicit

Sub disableAllCheckboxes()
    'This sub assumes all checkboxes on the sheet need to be disabled. Add logictests accordingly.

    Dim chkBox As OLEObject
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Worksheets("Sheet1") 'Replace Sheet1 with the name of your actual worksheet

    For Each chkBox In ws.OLEObjects
        If TypeName(chkBox.Object) = "CheckBox" Then
            chkBox.Enabled = False
        End If
    Next

End Sub

答案 2 :(得分:1)

    Sub enableCheckboxes()
'Macro to enable or disable a row of activeX checkboxes based on a cell value in that row

    'Declaration of variable
    Dim rngLinked As Range, rngRow As Range
    Dim enableRow As Boolean
    Dim ws As Worksheet
    Dim chkBox As OLEObject
    Dim rowNum As Long, rowNumLength As Long

    'Setting of object variables
    Set ws = ThisWorkbook.Worksheets("sheet1") 'Change to the actual name of your sheet.
    With ws
        Set rngLinked = .Range(.Cells(1, 60), .Cells(.Rows.Count, 60).End(xlUp)) 'Range set to column BH
    End With

    'Nested loops enable disable objects
    Application.ScreenUpdating = False 'Prevent screenupdating
    For Each rngRow In rngLinked 'Check column BH
        enableRow = rngRow 'See if has to be enabled or not
        rowNum = rngRow.Row 'Row to enable or disable
        rowNumLength = Len(CStr(rowNum)) 'Used to extract the rownumber from the checkbox' names
        For Each chkBox In ws.OLEObjects 'Iterate through the OLE objects on the sheet.

           If chkBox.Name Like "Attend*" & rowNum Then
                    If enableRow Then 'Check to enable or disable object
                    chkBox.Enabled = False
                    Else
                    chkBox.Enabled = True
                    End If
                    End If

           If chkBox.Name Like "*day" & rowNum Then 'rowNum Then
                    If enableRow Then
                    chkBox.Enabled = False
                    Else
                    chkBox.Enabled = True
                    End If
                    End If
Next chkBox
Next rngRow

Application.ScreenUpdating = True
End Sub