我正在动态创建一个用户表单,其中组合框和文本框的数量是可变的。然后我想将这些数据分发到不同的表格中。
为此,我想避免.Insertlines xx, "blabla"
对userform进行编码,以便在单击按钮时调用模块中的sub。由于组合框和文本框的数量是可变的,我得到了一个For ... Next循环来处理它。
我的问题是我无法弄清楚如何获取用户形式的值User form: UF2
以下是我在模块中创建用户表单的代码的开头(我不会放置所有内容,只是为了显示上下文:
UF2在公共子
中设置Option Explicit
Public Nb As Integer 'Number of people for creating UF2 form
Public Datbox As Date 'Date for record
Public UF2 As Object 'Forms for operator time
Public PckF As Object 'Form for number of operators/workstation
Public WC As String 'memory which workcenter ticked
Public Sub Packing()
' DIM的许多线条进一步
'Adatable name list for combobox
ThisWorkbook.Worksheets("Employees").Select
ActiveSheet.Range("A2").Select
Lstdata = ActiveSheet.Range("A2", ActiveSheet.Range("A2").End(xlDown)).Address
ThisWorkbook.Worksheets("Commands").Select
'Packing record userform creation
Set UF2 = ThisWorkbook.VBProject.VBComponents.Add(3) 'global form
With UF2
.Properties("Caption") = "Packing record"
.Properties("Width") = "250"
.Properties("Height") = "110"
.Properties("Height") = .Properties("Height") + Nb * 25
End With
Set CB2650 = UF2.Designer.Controls.Add("forms.Checkbox.1")
With CB2650
.Top = 0
.Left = 10
.Width = 50
.Height = 30
.Caption = "Liners (2650)"
.TextAlign = fmTextAlignCenter
End With
依此类推,等等......
我的问题就在这里,当我点击" Pack"按钮它正在调用此Sub
Public Sub SavePack()
Dim e, F As Integer
Dim OP, OPh, Pkbin As String 'OP=operator name, OPh= hours value, Pkbin=Number of bins packed
For e = 1 To Nb
OP = UF2.Controls("ComboBox" & e).value
OPh = UF2.Controls("TextBox" & e).value
'Checking value of variables (WIP)
MsgBox ("OP value is " & OP)
MsgBox ("oph value is " & OPh)
With ThisWorkbook.Sheets("Team sheet record")
.Range("A1").End(xlDown).Offset(1, 0).value = OP
.Range("A1").End(xlDown).Offset(0, 2).value = OPh
.Range("A1").End(xlDown).Offset(0, 3).value = Datbox
.Range("A1").End(xlDown).Offset(0, 4).value = WC
End With
BOOM !! OP= UF2.Controls("ComboBox" & e).value
我尝试了很多不同的东西,最接近的是:
UserForm2.Controls("ComboBox" & e).value
(没有错误 - 因为userform在创建时命名如此)但返回的值为空。
你有任何提示/想法吗? 谢谢。
PS:我使用了UF2.something else.value但它返回了一个"错误424 - 需要对象"它已经设置为" With ... End With"