for ....在重复用户表单后,下一个循环失败

时间:2013-03-01 04:28:11

标签: excel-vba vba excel

我正在使用下一个循环,同时在它内部循环。问题是,当我输入第一个数据然后我想通过选择vbYes重复插入时,循环将重复,但它再次将值设置为i = 32并继续循环,尽管while {condition}为false.let我给出一些例如,

comboBox1.value = {a,b,c,d}

i = {32,33,34,35}〜>我被设置为行号。

对于第一次计数,我将c插入到combobox1中,给出了combobox1.value = c和单元格(34,2).value = c,因此循环继续。但是当我选择继续第二次插入数据时,它会再次返回到下一个循环但是它设置了i = 32的值,给出了单元格(32,2).value = c然后继续进行while循环但是循环假设不继续,因为单元格的正确值(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

2 个答案:

答案 0 :(得分:0)

您正在从内存中卸载表单。当您不想继续时,请卸载表单。

如果msgbox(“你想继续”,vbyesNo)= vbyes那么

 goto repeat1

其他

 unload me

结束如果

答案 1 :(得分:0)

最后!只是一个我忘了放的简单的一行命令。

在上面插入for循环

Workbooks("MasterInput.xlsm").Activate
For i = 32 To 35

立即行动! i值取自第二个工作簿,它假定从第一个工作簿中获取。