Pass MsgBox answer to a userform?

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have this
VBA Code:
Dim Ans As VbMsgBoxResult
Const StrTitle As String = "Master Edit Tool"

If Sheet4.Range("BJ" & ActiveCell.Row).Value <> 0 Then
Ans = MsgBox ("The record you have selected to edit has multiple lines, any changes made here will affect all similar records!" & vbCr & vbCr & "Select Yes if you wish to do this or No if you only want to edit the one entry!", vbYesNo + vbQuestion, StrTitle)
End If

Load UserForm1
UserForm1.MultiPage1.Value = 0
UserForm1.Show

How can I pass "Ans" into the userform so that the code there knows what it is?

Many thanks.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
In the code of your userform.

VBA Code:
Public ans As VbMsgBoxResult    'At the beginning of all the code

Try this:
VBA Code:
  Dim ans As VbMsgBoxResult
  Const StrTitle As String = "Master Edit Tool"
  
  If Sheet4.Range("BJ" & ActiveCell.Row).Value <> 0 Then
    ans = MsgBox("The record you have selected to edit has multiple lines, any changes made here will affect all similar records!" & vbCr & vbCr & "Select Yes if you wish to do this or No if you only want to edit the one entry!", vbYesNo + vbQuestion, StrTitle)
  End If
  
  Load UserForm1
  With UserForm1
    .MultiPage1.Value = 0
    .ans = ans
    .Show
  End With
 
Upvote 0
Solution
Hi,
you could declare your variable as Public or maybe better to place it in the UserForms code page

In the TOP of your Userform

VBA Code:
Public Ans As VbMsgBoxResult

Updated code

VBA Code:
 Const StrTitle As String = "Master Edit Tool"
 
    Load UserForm1

    With UserForm1
    If Sheet4.Range("BJ" & ActiveCell.Row).Value <> 0 Then
        .Ans = MsgBox("The record you have selected to edit has multiple lines" & Chr(10) & _
                            "any changes made here will affect all similar records!" & _
                            vbCr & vbCr & "Select:-" & Chr(10) & _
                            "Yes -  if you wish to do this" & Chr(10) & _
                            "No - if you only want to edit the one entry!", vbYesNo + vbQuestion, StrTitle)
    End If
 
        .MultiPage1.Value = 0
        .Show
    End With

I trust you appreciate Ans will return either 6 or 7 ?

Hope Helpful

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,581
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top