1

I'm creating a VB macro to loops through cells and copy and paste the values into notepad. What I'd like to do is prompt the user and ask if they'd like to continue before doing the next iteration.

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 Answers 2

5

Try this:

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
Sign up to request clarification or add additional context in comments.

2 Comments

I have taken few line from you. Is it fine right for you?
Yes of course. The code doesn't belong to me and the purpose of this website is to provide free help. So feel free to use it.
0

I have used FileSystemObject method to write a code

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.