(VBA)如何从dynamic创build的checkbox中检索选定的checkbox数据

所以我有一个表单上有2个button,代码捕获数据表中的列标题,添加到数组,然后创build根据列标题重命名标题的checkbox。 然后检查这些信息是否先前已经保存在报告表中,并预选了checkbox。

这一切都在下面的代码中工作,我无法解决的是当我按下“OKButn”button,使它从这些dynamic创build的checkbox中获取数据选定的数据,并将其添加到数组,所以我可以输出结果到报告片。

这个button的代码保存在表单代码表中 – 只有这样我才能使button工作(我知道的)。 也可以dynamic地创buildbutton – 这是我可以做的,但是在被点击后让它们运行代码,这是我无法解决的。

这是我的代码到目前为止:

Option Explicit Public HdrArray(), HdrColArray() Public z, y, TotalHdrs, SavedHdrsCol, SavedHdrsRow, TotalSavedHdrs As Integer Public AddOption As Object Sub PopulateForm() ColumnCopyForm.Show vbModeless ColumnCopyForm.Caption = "Column Copy Selection """ Sheets("Data").Select 'Find total number of headers TotalHdrs = Sheets("Data").Cells(1, Columns.Count).End(xlToLeft).Column 'Find cell that records the 'required columns' SavedHdrsCol = Sheets("Report").Range("A1:zz100").Find("Required Columns", LookIn:=xlValues).Column SavedHdrsRow = Sheets("Report").Range("A1:zz100").Find("Required Columns", LookIn:=xlValues).Row 'Count total rows TotalSavedHdrs = Sheets("Report").Cells(Sheets("Data").Rows.Count, SavedHdrsCol).End(xlUp).Row For z = 0 To (TotalHdrs - 1) 'If Sheets("Data").Cells(1, 1 + z).Value = "Item Type" Then 'Delete Columns on Data Sheet 'Sheets("Data").Columns(z).EntireColumn.Delete 'Else 'Makes the array dynamic ReDim Preserve HdrArray(TotalHdrs, z) 'Adds the Data table header label to the array (column 0) HdrArray(0, z) = Sheets("Data").Cells(1, 1 + z).Value 'Adds the column number to the array (Column 1) HdrArray(1, z) = z 'Adds a check box - renaming it to the column title Set AddOption = ColumnCopyForm.Controls.Add("Forms.CheckBox.1", "LabelOpt" & z, True) With AddOption .Caption = HdrArray(0, z) .Left = 10 .Width = 200 .Top = .Height * z 'Automatically selects this if the option has been previously saved to copy to report sheet For y = 0 To (TotalSavedHdrs - 1) If Sheets("Report").Cells(SavedHdrsRow + 1 + y, SavedHdrsCol).Value = HdrArray(0, z) Then AddOption.Value = True 'Add info to Array HdrArray(2, z) = 1 End If Next y End With 'End If 'Make button visible - and format ColumnCopyForm.OKButn.Visible = True With ColumnCopyForm.OKButn .Caption = "Apply & Close" .Top = ColumnCopyForm.Height - 50 .Left = ColumnCopyForm.Width - 130 .Width = 70 .Height = 20 .ZOrder (0) End With 'Make button visible - and format ColumnCopyForm.CancelButn.Visible = True With ColumnCopyForm.CancelButn .Caption = "Cancel" .Top = ColumnCopyForm.Height - 50 .Left = ColumnCopyForm.Width - 50 .Width = 40 .Height = 20 .ZOrder (0) End With Next z End Sub 

这里是button的代码…

 Option Explicit Sub OKButn_Click() For y = 0 To (TotalHdrs - 1) MsgBox (HdrArray(0, y) & " - " & HdrArray(2, y)) 'Saves the preferences to the report sheet If HdrArray(2, y) = "1" Then Sheets("Report").Cells(SavedHdrsRow + 1 + y, SavedHdrsCol).Value = HdrArray(0, y) End If Next y Unload ColumnCopyForm End Sub 

这是我认为可能在button下工作的代码…

 Sub OKButn_Click() For y = 0 To (TotalHdrs - 1) Set LabelOptName = "LabelOpt" & (y + 1) If ColumnCopyForm.LabelOptName.Value = True Then HdrArray(2, y) = 1 End If MsgBox (HdrArray(0, y) & " - " & HdrArray(2, y)) 'Saves the preferences to the report sheet If HdrArray(2, y) = "1" Then Sheets("Report").Cells(SavedHdrsRow + 1 + y, SavedHdrsCol).Value = HdrArray(0, y) End If Next y Unload ColumnCopyForm End Sub 

但是在这一行上:

如果ColumnCopyForm.LabelOptName.Value = True然后

我得到一个编译错误:未find方法或数据成员

pipe理得到这个工作,感谢关于通过控制循环提示!

这是button上的代码工作…

 Sub OKButn_Click() z = 0 y = 0 'Loops through the controls on the form, to get values of the Checkboxes For Each ctlLoop In ColumnCopyForm.Controls 'Checks to see if the value is true so it can be recorded If ctlLoop.Value = True Then 'Records the column title in the correct place for future referencing Sheets("Report").Cells(SavedHdrsRow + 1 + y, SavedHdrsCol).Value = HdrArray(0, z) y = y + 1 End If z = z + 1 Next ctlLoop Unload ColumnCopyForm End Sub