我正在尝试为我的工作表中的复选框创建一个变量,这样我就可以在循环中反对更多的变量。
看起来像这样: The worksheet
目的是使日程表(蓝色背景)能够突出显示您使用复选框选择的名称(绿色列表左侧)。
为了做到这一点,我希望循环遍历所有这些复选框,看看它们是否适用于同一行上的相应名称。我已经走到了这一步:
Sub Test_ReplaceWithArray()
'State var
Dim Names(54) As String
Dim ChkBx As String
Dim Personal As Range
Dim n, m, i, j, ChkNr, numOfEmployees, numOfWeeks As Integer
'Set var
Set Personal = Range("A3:A55")
numOfEmployees = Application.WorksheetFunction.CountA(Personal)
numOfWeeks = Worksheets("Schemaläggning").numOfWeeksBox.Value
n = 1
m = 3
i = 3
j = 3
ChkNr = 1
ChkBx = ("CheckBox" & ChkNr)
'Fill array
Do Until n > numOfEmployees
Cells(i, 1).Select
If IsEmpty(ActiveCell) = False _
And ChkBx = True Then
Names(n) = ActiveCell.Value
i = i + 1
n = n + 1
ChkNr = ChkNr + 1
ElseIf IsEmpty(ActiveCell) = True Then
i = i + 1
n = n + 1
ChkNr = ChkNr + 1
End If
Loop
'Make Bold or Grey if in array
Do Until m > numOfWeeks + 2
Cells(m, j).Select
If j <= 7 And IsInArray(ActiveCell.Value, Names) = True Then
Selection.Font.Bold = True
j = j + 1
ElseIf j <= 7 And IsInArray(ActiveCell.Value, Names) = False Then
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.349986266670736
End With
j = j + 1
ElseIf j = 8 Then
j = 3
m = m + 1
End If
Loop
End Sub
重点是将行上的名称(在绿色列表中)放入数组中,然后查看计划中的每个名称。如果计划的单元格中的名称包含在数组中,则名称将加粗,但如果不是,则它将变为灰色。我已经做了一个比较函数来查看名称是否包含在有效的数组中(在继续复选框引用之前对其进行测试)。
但我在<:p>处得到运行时错误13
If IsEmpty(ActiveCell) = False _
And ChkBx = True Then
我肯定怀疑我不知道如何正确引用控件,但我不知道该怎么做。任何帮助表示赞赏。
答案 0 :(得分:0)
Dim cCont As Control
For Each cCont In Me.Controls
If TypeName(cCont) = "CheckBox" Then
cCont.Value = False
End If
Next cCont
这是我用来遍历用户表单中所有控件的示例。希望您可以根据自己的需要进行调整。
表示工作表对象:
Sub LoopListBoxes()
Dim OleObj As OLEObject
For Each OleObj In ActiveSheet.OLEObjects
If OleObj.progID = "Forms.ListBox.1" Then
MsgBox OleObj.Object.ListCount
End If
Next OleObj
End Sub
CheckBox = Forms.CheckBox.1
ComboBox = Forms.ComboBox.1
CommandButton = Forms.CommandButton.1
Frame = Forms.Frame.1
Image = Forms.Image.1
Label = Forms.Label.1
ListBox = Forms.ListBox.1
MultiPage = Forms.MultiPage.1
OptionButton = Forms.OptionButton.1
ScrollBar = Forms.ScrollBar.1
SpinButton = Forms.SpinButton.1
TabStrip = Forms.TabStrip.1
TextBox = Forms.TextBox.1
ToggleButton = Forms.ToggleButton.1
借来自: