我正在使用 for next 循环,其中带有 while wend 循环。问题是当我输入第一个数据然后我想通过选择 vbYes 重复插入时,循环将重复但它再次将值设置为 i=32 并继续 while 循环,即使 while {condition} 为假。让我给一些例子,
comboBox1.value = {a,b,c,d}
i={32,33,34,35} ~> i 设置为行号。
对于第一个计数,我将 c 插入到 combobox1 中,给出 combobox1.value=c 和 cells(34,2).value=c 因此 while 循环继续。但是当我选择第二次继续插入数据时,它再次返回到下一个循环,但它设置了 i=32 的值,给出了 cells(32,2).value=c 然后继续进行 while 循环但是 while循环假设不继续,因为 cells(32,2).value=a 的正确值。谁能帮忙?
Dim i As Long
Dim prod As String
Dim RowNo As Long
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Application.ScreenUpdating = False
prod = ComboBox1.Value
For i = 32 To 35
While ComboBox1.Value = Worksheets("Input").Cells(i, 2).Value
Rows(i).Select
Selection.Insert shift = xlDown
With Worksheets("Input")
'~insert data
.Range("B" & i) = ComboBox1.Text
.Range("C" & i) = TextBox1.Text
.Range("D" & i) = TextBox2.Text
.Range("E" & i) = TextBox3.Text
.Range("F" & i) = TextBox4.Text
.Range("G" & i) = TextBox5.Text
.Range("H" & i) = ComboBox2.Text
.Range("I" & i) = TextBox6.Text
.Range("J" & i) = TextBox7.Text
.Range("K" & i) = TextBox8.Text
End With
Workbooks(prod & " Input.xlsm").Activate
'~select row number from another worksheet
RowNo = Workbooks(prod & " Input.xlsm").Worksheets("Input").Cells(31, 3).Value
Set sh1 = Workbooks(prod & " Input.xlsm").Worksheets("Input")
Set sh2 = Workbooks("MasterInput.xlsm").Worksheets("Input")
'~copy inserted data from master input to product input
sh1.Range(sh1.Cells(RowNo, 2), sh1.Cells(RowNo, 11)).Value = sh2.Range(sh2.Cells((i, 2), sh2.Cells(i , 11)).Value
If MsgBox("One record written to Master Input. Do you want to continue entering data?", vbYesNo) = vbYes Then
GoTo repeat1:
Else
Unload Me
End If
Exit Sub
Wend
Next
Application.ScreenUpdating = True
repeat1:
End Sub