Nathalii.
Nathalii.

Reputation: 126

VBA msgBox - vbYes keeps calling wrong sub

If I click on yes, VBA keeps going to the function Mandate? What did I do wrong?

Public AutoDate As Date
Public NewDate As String

Public Sub GetDate()  ' DATUM BEPALEN
    AutoDate = Date - 1
    MsgBox (AutoDate), (vbYesNo), ("Datum")
    Dim Response As VbMsgBoxResult
        If Response = vbYes Then
            NewDate = AutoDate
            Call DeleteDate
        Else    ' No
            Call ManDate
        End If
End Sub

Upvotes: 0

Views: 250

Answers (3)

MiguelH
MiguelH

Reputation: 1425

You need a variable to capture the response ...

Dim ans As Integer
ans = MsgBox("hello", vbYesNo, "Datum")
If ans = vbYes Then
   MsgBox "Yes"
Else
   MsgBox "No"
End If

Upvotes: 1

jpinto3912
jpinto3912

Reputation: 1465

You need retrieve the Response as the return of the MsgBox function:

Dim Response as Integer
Response= MsgBox( AutoDate, vbYesNo, "Datum")

Now you can if-test Response to decide what to do.

Upvotes: 1

Darren Bartrup-Cook
Darren Bartrup-Cook

Reputation: 19782

You haven't assigned the result of MsgBox to Response.
Not sure if VbMsgBoxResult is a valid data type in that instance either.

Try either of these:

Public Sub GetDate()  ' DATUM BEPALEN

    AutoDate = Date - 1

    If MsgBox(AutoDate, vbYesNo, "Data") = vbYes Then
        NewDate = AutoDate
        Call DeleteDate
    Else    ' No
        Call ManDate
    End If

End Sub

or

Public Sub GetDate()  ' DATUM BEPALEN

        Dim Response As Long

        AutoDate = Date - 1
        Response = MsgBox(AutoDate, vbYesNo, "Data")

        If Response = vbYes Then
            NewDate = AutoDate
            Call DeleteDate
        Else    ' No
            Call ManDate
        End If

    End Sub

Upvotes: 2

Related Questions