我正在尝试使用动态创建的用户表单,并根据检查的框,灰显某些单元格。
作为背景,这是注塑设备。 QA设置正在运行的腔数。此动态用户窗体根据工作表上输入的腔数创建复选框。
Option Explicit
Private Sub UserForm_Initialize()
Dim col As Long
Dim row As Long
Dim lcol As Long
Dim i As Long
Dim j As Long
Dim chkBox As MsForms.CheckBox
Dim l As MsForms.Frame
Dim t As MsForms.Label
Set l = Me.Controls.Add("Forms.Frame.1", "cavz", True)
l.Caption = "BLOCKED CAVITIES"
l.Height = 195
Set t = l.Controls.Add("Forms.Label.1", "mark")
t.Caption = "Mark all cavities that are currently blocked:"
t.Width = 175
t.Top = 10
col = 2 'Set your column index here
row = 8
lcol = 17
j = 1
For i = col To lcol
Set chkBox = l.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
If Worksheets("QA").Cells(row, i).Value <> "" Then
chkBox.Caption = Worksheets("QA").Cells(row, i).Value
ElseIf Worksheets("QA").Cells(row, i).Value = "" Then
GoTo 10
End If
If i <= 9 Then
'MsgBox "i = " & i
chkBox.Left = 5
chkBox.Top = 5 + ((i - 1) * 20)
ElseIf i > 9 Then
j = j + 1
'MsgBox "j = " & j
chkBox.Left = 100
chkBox.Top = 5 + ((j - 1) * 20)
End If
10
Next i
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub CommandButton1_Click()
Dim x As Control
Dim cavz As MsForms.Frame
For Each x In cavz.Controls
If x.Value = True Then
If x.Value = Range("B8") Then
Range("B8:B14").Select
Selection.Interior.ColorIndex = 16
End If
End If
Next x
End Sub
我开始做一件事,不知怎的,这就是我的代码变成了什么。这不是最新的代码,但我仍然认为自己是新手。另外,这是我第一次无法找到答案,因此我第一次寻求帮助。所以,任何帮助将不胜感激!
谢谢!
更新 这很棒!谢谢唐。但是,我确实有一个跟进问题。也许我长期以来一直在反对这一点,但我想不出一个更有效的方法来做到这一点。这就是我现在正在做的事情:
For i = 2 To 17
Set ctl = Controls.Item("CheckBox_" & i)
If ctl = True Then
If i = 2 Then
Range(Cells(j, c), Cells(m, c)).Select
Selection.Interior.ColorIndex = 16
Range(Cells(k, c), Cells(m, c)).Select
With Selection
.Merge
Cells(k, c) = "BLOCKED"
.Orientation = 90
.VerticalAlignment = xlCenter
.HorizontalAlignment = xlCenter
End With
End If
Next i
等
如果i = 1到17,我正在写作,但我觉得有一种更有效的方式而且我无法破解它。