2017-02-21 85 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