2017-02-27 150 views
3

底線修復:將我的動態命名文本框更改爲在列和行之間具有_分隔符,以消除名稱的不明確性。Excel VBA值動態命名文本框

以前的代碼:

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行有15個文本框列(動態) 。

用戶輸入的數字在文本框中。然後,我希望他們打的用戶窗體上的Print按鈕運行PrintLabel()子,並把這些值轉換成電子表格垂直(B24:BXX)。然後讓它打印出電子表格並返回到用戶表單。

我的問題是,我似乎無法從文本框獲取值。

文本框名稱是在多維數組風格的格式:

PalletNumber & "row" & "column"

所以,第一行是PalletNumber0_0通過PalletNumber0_15。下一行將是PalletNumber1_0PalletNumber1_15

更新:

用戶輸入的值 「1234」 到一個文本框,並點擊 「查找」 運行lookup()。然後,它會搜索電子表格中的數字,並獲取所有匹配的行並將其放入用戶窗體中。

這裏是代碼片段

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 r    

    i = 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 

這裏是用戶窗體佈局:
userform

電子表格輸出應該是這樣的:

spreadsheet

+1

似乎合乎邏輯。你有什麼錯誤(如果有的話)? – Absinthe

+4

你的「增加」循環會變成'r = 0到14',你的「閱讀器」循環變成'r = 0到2',我們不知道第一個循環中可能會有什麼。另外,如何顯示錶單以讓用戶輸入值也可能是相關的。你所顯示的代碼似乎與表單的*默認實例相同* - 如果你在一個'New PickTicketForm'中輸入值(如你真的那樣),那麼這些值不存在於* default實例*,您需要將表單實例/對象傳遞給循環代碼。 –

+5

首先,你的命名是不明確的。 「PalletNumber115」應該是第11行第5列還是第1行第15列。要麼使用固定數量的字符,要麼在行和列之間添加一個「_」 –

回答

0

你應該內添加控件對於i = 0到5環爲好。 如果它仍然爲空,則在搜索PalletNumber00,PalletNumber01,PalletNumber02等時添加PalletNumber0,PalletNumber1,PalletNumber2等。

+0

已更新至完整代碼,因此更易於理解。也有修復。 – Travis