我有一个用户窗体,其中包含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
在此先感谢。
我是_guessing_这是因为每次你通过'ComboBoxes'循环遍历每个'TextBox'。你需要看看把两者联系在一起。 – Bugs
的确,我也这么认为。我只是不知道该怎么做 –
我提供了一些示例代码,可以帮助您分组您的控件。我没有深入了解代码,但它可能会给你一个开始。当你有机会时让我知道你的想法。 – Bugs