-1

For example code:

show_box()
Function show_box()
result = MsgBox ("Please follow steps in document"vbCrLf & vbCrLf _ 
                 & "Click OK to call sum procedure" & vbCrLf & vbCrLf _
                 & "Click No to call substraction procedure"& vbCrLf & vbCrLf _
                 & "Click cancel to print hello")
Select Case result
       case 1
            msgbox(sum(1,2))
       case 7
           msgbox(substraction(4,2))            
       Case 2
           msgbox("Hello")
       End Select
END Function


sub sum(a,b)
  sum = a+b
  msgbox(sum)
end sub

sub substraction(a,b)
  substraction = a - b  
  msgbox(substraction)
end sub

The result should be: When I click on OK, then call sum(a,b) procedure, and so on. I tried many times using different approach but I was not able to fix that. Help will be most appreciated!!

S_sauden
  • 302
  • 2
  • 10

1 Answers1

1

Try something more like this instead:

Function sum(a, b)
  sum = a + b
End Function

Function substraction(a, b)
  substraction = a - b  
End Function

Sub show_box()
  Dim result
  result = MsgBox ("Please follow steps in document" & vbCrLf & vbCrLf _ 
                 & "Click Yes to call sum procedure" & vbCrLf & vbCrLf _
                 & "Click No to call substraction procedure" & vbCrLf & vbCrLf _
                 & "Click Cancel to print hello",
                 vbYesNoCancel)
  Select Case result
    Case vbYes
      MsgBox(CStr(sum(1,2)))
    case vbNo
      MsgBox(CStr(substraction(4,2)))
    Case vbCancel
      MsgBox("Hello")
  End Select
End Sub

show_box()
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you so much for a prompt solution. But still, there is some issue while clicking the 'OK" button. If I click 'OK', nothing displays. But when I click NO and Cancel, it's working fine. What will happen If I write the procedure instead of function? @Remy – S_sauden Jun 16 '20 at 22:44
  • Sounds like `MsgBox` is not returning `vbOK` when the OK button is clicked. Did you try debugging the code to verify that? – Remy Lebeau Jun 16 '20 at 22:49
  • 2
    It's a YES/NO dialog not an OK dialog so `vbok` is never returned. –  Jun 16 '20 at 23:59
  • @Mark good catch. `MsgBox()` does not have an OK/No/Cancel dialog, that is why I changed OK to Yes, but I forgot to change `vbOK` to `vbYes` to match. I have fixed it now. – Remy Lebeau Jun 17 '20 at 00:06
  • Thank you so much @Mark . If I want to use the subprocedure instead of function what will happen? ... I tried I got type mismatch error. Do you have any solution for that? – S_sauden Jun 17 '20 at 00:20
  • A sub procedure is the same as a function EXCEPT it does not return a value. You can call functions as subs if you don't care about the retuned value. However you cannot call subs as functions. –  Jun 17 '20 at 00:23