为什么For循环复制过程?

时间:2017-02-21 12:06:03

标签: excel vba combobox

我有一个UserForm包含20个Comboboxes,然后每个Combobox旁边有3个相应的文本框(总共60个文本框)。每个Combobox显示相同的两个选项(选择1和选择2)。 Combobox旁边的3个文本框分别用于描述,项目数和每个项目的价格。

我使用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

提前致谢。

2 个答案:

答案 0 :(得分:0)

(“textbox”& num)类型术语可能存在问题。 Textbox是一个字符串,您尝试将其连接到整数。 尝试将num转换为这样的字符串 - >

(“Textbox”& CStr(num))

测试两者(“Textbox”和& CStr(num))和(“textbox”& num),每个使用一个Msgbox来查看这样的结果 - >

Msgbox“with CStr =”& (“textbox”& CStr(num))

Msgbox“no CStr =”& (“textbox”& num)

如果将num转换为字符串时结果中有不需要的空格,请尝试此选项 - >

(“textbox”& Trim(CStr(num)))

答案 1 :(得分:0)

您可以使用GroupBox 控件:

enter image description here

然后我会遍历每个GroupBox,然后遍历每个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上的控件,您现在知道哪个TextBoxComboBox相关联,并且您不会一次又一次地迭代TextBox。< / p>