[英]is it possible to use vba to refer to and disable multiple activex check boxes
我有一個工作簿,我想在其中禁用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
您可以使用對象數組來實現它,如下所示:
If .Row = 1 Then
ActiveSheet.OLEObjects(Array("AttendMonday5", "AttendTuesday5", "AttendWednesday5", "AttendThursday5", "AttendFriday5", "Monday5", "Tuesday5", "Wednesday5", "Thursday5", "Friday5")).Enabled = False
End If
將此宏指定給所有表單控件復選框。 表單控件復選框需要鏈接到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
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.