Excel VBA值dynamic命名文本框

底线修正:改变我的dynamic命名的文本框,在列和行之间有一个_分隔符,以消除名称的不明确性。

前面的代码:

Set cCntrl = PickTicketForm.Controls.Add("Forms.TextBox.1", "PalletNumber" & i & r, True)

固定:
Set cCntrl = PickTicketForm.Controls.Add("Forms.TextBox.1", "PalletNumber" & i & "_" & r, True)


我有一个用户表单,它有X行(dynamic)15个文本框列。

用户将数字input到文本框中。 然后,我希望他们点击用户窗体上的Printbutton来运行PrintLabel()子,并将这些值垂直放入电子表格(B24:Bxx)。 然后打印出电子表格并返回到用户表单。

我的问题是我似乎无法从文本框中获取值。

文本框名称采用multidimensional array样式格式:

PalletNumber & "row" & "column"

所以第一行是PalletNumber0_0PalletNumber0_15 。 下一行是PalletNumber1_0PalletNumber1_15

更新:

用户input值“1234”到文本框中,然后单击“查找”运行lookup() 。 然后,在电子表格中search数字并获取所有匹配的行,并将其放入用户窗体中。

这是代码片段

 For Each c In Worksheets("Sheet1").range("A2:A" & iRowCount) If c.value = OrderNumber Then ReDim Preserve aGetData(6, i) For a = 0 To 6 'Change this for total of columns Our first index will hold each col of data that is why 'it is set to 0 (arrays start at a base of zero, so '0,1,2,3,4,5 will be each col(A,B,C). aGetData(a, i) = c.Offset(0, a) 'This gets each value from col A,B and C Next a 'Get the data and set it into variables. ItemNumber = aGetData(5, i) ItemQty = aGetData(2, i) 'Create "ItemQuantity" text box. Set cCntrl = PickTicketForm.Controls.Add("Forms.Label.1", "ItemQuantity" & i, True) With cCntrl .Caption = ItemQty .Width = 85 .Height = 18 .Top = 86 + (i * 20) .Left = 40 .TextAlign = 1 'Left .Font.Name = "Arial Black" .Font.Size = "10" .BackColor = BackgroundColor End With 'Create "ItemNumber" box Set cCntrl = PickTicketForm.Controls.Add("Forms.Label.1", "ItemNumber" & i, True) With cCntrl .Caption = ItemNumber .Width = 340 .Height = 18 .Top = 86 + (i * 20) .Left = 86 .TextAlign = 1 'Left .Font.Name = "Arial Black" .Font.Size = "10" .BackColor = BackgroundColor End With 'Create each inputbox for the pallets. For r = 0 To 14 Set cCntrl = PickTicketForm.Controls.Add("Forms.TextBox.1", "PalletNumber" & i & "_" & r, True) With cCntrl .Width = 28 .Height = 18 .Top = 86 + (i * 20) .Left = 354 + (r * 30) .TextAlign = 1 'Left .Font.Name = "Arial Black" .Font.Size = "10" .BackColor = BackgroundColor .Text = i & r End With Next ri = i + 1 'Increment for array in case we find another order number 'Our second index "aGetData(index1,index2) is being resized 'this represents each order number found on the sheet LineCount = LineCount + 1 'Set the scroll bar height for the final form. ScrollBarHeight = 150 + (i * 20) End If Next c 

这里是代码片段:

 'Loop through first column completely ( 0 - 5 to test) ' i = 0 means go through every box in column 0 first For i = 0 To 5 'Loop through rows. For r = 0 To 2 '( 0 - 2 rows to test) ActiveCell.Offset(i, 0).value = PickTicketForm.Controls("PalletNumber" & i & r).Text Next r Next i 

这是用户表单布局:
用户窗体

电子表格输出应如下所示:

电子表格

您也应该在For i = 0到5循环中添加您的控件。 如果它仍然为空,则在searchPalletNumber00,PalletNumber01,PalletNumber02等时添加PalletNumber0,PalletNumber1,PalletNumber2等。