2017-02-21 83 views
0

我有一個用戶窗體,其中包含20個組合框,然後每個組合框旁邊有3個相應的文本框(所以共有60個文本框)。每個Combobox顯示相同的兩個選項(選擇1和選擇2)。與Combobox相鄰的3個文本框分別用於描述,項目數量和每個項目的價格。爲什麼For循環複製過程?

我已經使用For循環來循環20個Comboboxes。循環中的代碼將文本框中的輸入寫入表格格式的Excel工作表中。 Combobox的用途是將Excel中的表格中的文本框中的輸入分爲兩個選項(選項1和2)。

該代碼似乎適用於第一個Combobox,但是當我輸入第二個Combobox及其各自文本框的數據時,Excel表格上的數據被複制了幾次,我不知道爲什麼。

這是代碼:

Dim i as Integer 'row counter 
Dim j As Integer 
Dim h As Integer 
Dim ctrl As Control 
Dim num As Integer 
Dim txt As Control 


For Each ctrl In Me.custom_prices.Controls 
    If TypeName(ctrl) = "ComboBox" Then 
      If ctrl = "Choice 1" Then 
       j = i 
       For Each txt In Me.custom_prices.Controls 
       If TypeName(txt) = "TextBox" And txt.Tag = "DESCRIPTION" Then 
       For num = 1 To 20 
        If txt.Value = "" Then Exit For 
        If Controls("textbox" & num).Value = "" Then Exit For 
        If Controls("textboxprice" & num).Value = "" Then Exit For 
        If Controls("textboxno" & num).Value = "" Then Exit For 
        ActiveCell.Offset(rowOffset:=j, columnOffset:=0).Value = Controls("textbox" & num).Value 
        ActiveCell.Offset(rowOffset:=j, columnOffset:=1).Value = Controls("textboxprice" & num).Value 
        ActiveCell.Offset(rowOffset:=j, columnOffset:=2).Value = Controls("textboxno" & num).Value 
        ActiveCell.Offset(rowOffset:=j, columnOffset:=3).Value = Controls("textboxno" & num).Value * Controls("textboxprice" & num).Value 
        j = j + 1 
        sub_total = sub_total + (Controls("textboxno" & num).Value * Controls("textboxprice" & num).Value) 
       Next num 
       End If 
       Next txt 
       i = j 
       sub_total_3 = sub_total 
       sub_total = 0 


      ElseIf ctrl = "Choice 2" Then 
       h = i 
       For Each txt In Me.custom_prices.Controls 
       If TypeName(txt) = "TextBox" And txt.Tag = "DESCRIPTION" Then 
       For num = 1 To 20 
        If txt.Value = "" Then Exit For 
        If Controls("textbox" & num).Value = "" Then Exit For 
        If Controls("textboxprice" & num).Value = "" Then Exit For 
        If Controls("textboxno" & num).Value = "" Then Exit For 
        ActiveCell.Offset(rowOffset:=h, columnOffset:=0).Value = Controls("textbox" & num).Value 
        ActiveCell.Offset(rowOffset:=h, columnOffset:=1).Value = Controls("textboxprice" & num).Value 
        ActiveCell.Offset(rowOffset:=h, columnOffset:=2).Value = Controls("textboxno" & num).Value 
        ActiveCell.Offset(rowOffset:=h, columnOffset:=3).Value = Controls("textboxno" & num).Value * Controls("textboxprice" & num).Value 
       h = h + 1 
       sub_total = sub_total + (Controls("textboxno" & num).Value * Controls("textboxprice" & num).Value) 
       Next num 
       End If 
       Next txt 
       i = h 
       sub_total_4 = sub_total 
       sub_total = 0 

      Else: ctrl = "" 

       sub_total = sub_total 

     End If 
    End If 
Next ctrl 

在此先感謝。

+0

我是_guessing_這是因爲每次你通過'ComboBoxes'循環遍歷每個'TextBox'。你需要看看把兩者聯繫在一起。 – Bugs

+0

的確,我也這麼認爲。我只是不知道該怎麼做 –

+0

我提供了一些示例代碼,可以幫助您分組您的控件。我沒有深入瞭解代碼,但它可能會給你一個開始。當你有機會時讓我知道你的想法。 – Bugs

回答

0

可能存在(「textbox」& num)類型術語的問題。文本框是一個字符串,你試圖將它連接到一個整數。 嘗試轉換NUM到這樣一個串 - >

( 「文本框」 & CStr的(NUM))

測試兩者( 「文本框」 & CStr的(NUM))和( 「文本」 & NUM)由使用一個MsgBox每個看到兩個結果這樣的 - >

MSGBOX 「與CStr的=」 &( 「文本」 & CStr的(NUM))

MSGBOX 「無CStr的=」 &( 「文本」 & NUM)

如果在結果NUM轉換爲字符串試試這個當不需要的空間 - >

( 「文本框」 &修剪(CSTR(NUM)))

+0

我嘗試了兩個,他們都工作。我刪除了文本框For循環,並且該過程現在重複兩次,而不是四次。 –

0

你可以使用GroupBox對照:

enter image description here

我會然後通過每個GroupBox然後THROU環GH各ComboBoxTextBox

For Each grpb As GroupBox In Me.Controls().OfType(Of GroupBox)() 

    For Each cmb As ComboBox In grpb.Controls().OfType(Of ComboBox)() 

    Next 

    For Each txtb As TextBox In grpb.Controls().OfType(Of TextBox)() 

    Next 

Next 

注意使用OfType。當你知道你想要關注哪些控件時,這很方便。現在你不需要像If TypeName(ctrl) = "ComboBox"這樣的代碼。

您現在擁有更多控制權。通過循環訪問GroupBox上的控件,您現在知道哪個TextBox已鏈接到哪個ComboBox,並且您不會一次又一次迭代相同的TextBox