• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Ask user a Question before going to next step in macro

dk

New Member
Hi. I have a simple macro that copies a small range and pastes to a different workbook. Before pasting to the destination, I want it to ask the User if they have populated something in the Source workbook. If they have, progress to pasting, if not, go back to Source and fill in required data. How do I write that VBA code? Any help would be really great. Thank you.
 
Hi DK,


try the msgbox control..


Sub YourRoutine()


'do the codes to copy the range

Dim bFlag As Boolean


bFlag = MsgBox("Have you filled the mandatory info at source?", vbQuestion + vbYesNo, "Confirm")


If bFlag = vbYes Then

'code to paste your data


Else

' code to activate your source screen for entering the data.

'note: you may need to either above function as loop for checking again


End If


'continue with code of what you want further here


End Sub


Hope this gives you some idea.


Regards,

Prasad
 
Hi dk,


Do you want to tell them they need to fill it in, or ask them a question? You say "required" so its probably better to give a simple message box rather than a yes no choice maybe.
 
Thank you very much, that helps a lot!! It's not completely working just yet however. Whether VBAnswer is "Yes" or "No", it goes to the source data and stops. I'm not quite sure what part of the loop I am missing? Here's my code:


'do the codes to copy the range

Range("C1:C42").Select

Selection.Copy


Dim bFlag As Boolean


bFlag = MsgBox("Have you filled in the CAP-X in the Buyer's?", vbQuestion + vbYesNo, "Confirm")


If bFlag = vbYes Then

'code to paste your data

Workbooks.Open Filename:= _

"DestinationFile.xlsx"

Range("A1").Select


Else

'code to activate your source screen for entering the data.

'note: you may need to either above function as loop for checking again

Sheets("Source").Select

Range("L313").Select


End If

'continue with code of what you want further here


End Sub


'Thanks
 
Change Dim line to:

[pre]
Code:
Dim bFlag As Long

should resolve it, at first look.


Alternatively, you could do without the flag variable entirely if you want, by combining the MsgBox call and the If command (although after the End If you won't be able to refer to the user's choice again):

If MsgBox("Have you filled in the CAP-X in the Buyer's?", vbQuestion + vbYesNo, "Confirm") = vbYes Then
[/pre]
Asa
 
Finally, if you want to stick with a boolean flag, change the MsgBox line to:

[pre]
Code:
bFlag = MsgBox("Have you filled in the CAP-X in the Buyer's?", vbQuestion + vbYesNo, "Confirm") = vbYes
and change the If line to:

If bFlag Then
[/pre]
 
Back
Top