在继续循环之前提示用户

时间:2015-11-12 02:10:29

标签: excel vba excel-vba

我创建一个VB宏来循环遍历单元格并将值复制并粘贴到记事本中。我想做的是提示用户并询问他们是否想在继续下一次迭代之前继续。

Sub LoopTest()

Dim rng As Range, cell As Range
Set rng = Range("A1:A2")
For Each cell In rng

cell.Copy

'Worksheets("Sheet1").Range("A" + j).Copy
Shell "C:\Windows\system32\notepad.exe", vbNormalFocus
SendKeys "^V"
DoEvents
SendKeys "{ENTER}", True
Next cell


End Sub

2 个答案:

答案 0 :(得分:4)

试试这个:

Sub LoopTest()

    Dim rng As Range, cell As Range
    Set rng = Range("A1:A2")
    For Each cell In rng

        cell.Copy

        'Worksheets("Sheet1").Range("A" + j).Copy
        Shell "C:\Windows\system32\notepad.exe", vbNormalFocus
        SendKeys "^V"
        DoEvents
        SendKeys "{ENTER}", True

        If MsgBox("Continue?", vbYesNo, "Confirm") = vbNo Then

            Exit For

        End If

    Next cell


End Sub

答案 1 :(得分:0)

我使用FileSystemObject方法编写代码

Sub test1()

    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim strpath As String

    strpath = "" '~~~~> path

    Dim oFile As Object

    Set oFile = fso.CreateTextFile(strpath)

    Dim rng As Range, cell As Range
    Set rng = Range("A1:A2")
    For Each cell In rng
        If MsgBox("Continue?", vbYesNo, "Confirm") = vbYes Then

            oFile.WriteLine cell.Value
        Else
            Exit For
        End If

    Next cell

    oFile.Close

    Set fso = Nothing

    Set oFile = Nothing

    End Sub