Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 如何使用按钮停止Excel中的下一步?_Vba_Excel - Fatal编程技术网

Vba 如何使用按钮停止Excel中的下一步?

Vba 如何使用按钮停止Excel中的下一步?,vba,excel,Vba,Excel,下面的代码是我根据在线找到的解决方案修改的。形式的上下文 表单有一个带有3个按钮的组合框(添加、下一步、完成) 添加将粘贴从组合框中选择的任何内容 Next应该移动到列中的下一个空单元格(下面的代码) 完成后应关闭表单,以便查看电子表格。(代码如下) 我遇到的是,当我点击“完成”按钮时,如果我运行了“下一步”,它不会关闭。For…下一个循环将一直进行,直到所有空间都被填满。我想要的是当我点击“done”时停止for循环的一种方法。我已经尝试添加一个语句来显示当前单元格是否有要退出的字符,但是

下面的代码是我根据在线找到的解决方案修改的。形式的上下文

表单有一个带有3个按钮的组合框(添加、下一步、完成)

  • 添加将粘贴从组合框中选择的任何内容
  • Next应该移动到列中的下一个空单元格(下面的代码)
  • 完成后应关闭表单,以便查看电子表格。(代码如下)
我遇到的是,当我点击“完成”按钮时,如果我运行了“下一步”,它不会关闭。For…下一个循环将一直进行,直到所有空间都被填满。我想要的是当我点击“done”时停止for循环的一种方法。我已经尝试添加一个语句来显示当前单元格是否有要退出的字符,但是它只是终止了代码,它根本不会运行。所以我现在不知所措,如果有人能帮上忙那就太好了。谢谢

Private Sub nxtBtn_Click()

Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String, cString As String


sourceCol = 7   'column G has a value of 7
rowCount = Cells(Rows.count, sourceCol).End(xlUp).Row

'for every row, find the first blank cell and select it

For currentRow = 4 To rowCount
    currentRowValue = Cells(currentRow, sourceCol).Value
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then
        Cells(currentRow, sourceCol).Select
        End If
    Next

End Sub

Private Sub done_bn_Click()


    'Refresh the workbook/pivot tables
    ThisWorkbook.RefreshAll

    'Close form
    Unload Me

End Sub

您需要暂停循环并检查发送到程序的任何事件

在VBA中,有DoEvents函数。用外行的话说,这使程序能够“赶上自己”。所以,我建议在循环中添加一个DoEvents,然后检查击键。您希望捕获Ctrl/Break键,这将暂停VBA

我是这样想的:

Private Sub nxtBtn_Click()

Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String, cString As String


sourceCol = 7   'column F has a value of 7
rowCount = Cells(Rows.count, sourceCol).End(xlUp).Row

'for every row, find the first blank cell and select it

For currentRow = 4 To rowCount
    currentRowValue = Cells(currentRow, sourceCol).Value
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then
        Cells(currentRow, sourceCol).Select
        End If

    'Allow the code to catch up, so it can determine if Ctrl/Break were sent
    DoEvents
    Next

End Sub

Private Sub done_bn_Click()

    'Send the Ctrl/Break keys to Excel
    Application.SendKeys("^{BREAK}")

    'Refresh the workbook/pivot tables
    ThisWorkbook.RefreshAll

    'Close form
    Unload Me

End Sub

不使用循环查找F列中的第一个空白单元格,只需使用该方法即可。您可以使用以下一行替换整个循环:

Columns("F").Find(vbNullString, Range("F3"), xlValues, xlWhole).Select

这样,您就不必退出代码执行,因为这将在单元格F3(我猜它是一个标题)之后的F列中找到第一个空白单元格,而不需要循环。

write
exit For
直接位于
单元格(currentRow,sourceCol)下方。选择
line一旦找到要选择的单元格,上述注释将停止代码执行。然后,当您单击“完成”时,它将工作,因为没有代码正在运行。