在继续循环之前提示用户

我正在创build一个VBmacros来遍历单元格,并将这些值复制并粘贴到记事本中。 我想要做的是提示用户,并询问他们是否愿意继续下一次迭代之前。

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 

尝试这个:

 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 

我已经使用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