Home > Mobile >  VBA MsgBox - How to display multiple messages
VBA MsgBox - How to display multiple messages

Time:06-08

Would someone be able to advise on the below

Is it possible for a MsgBox text to display multiple messages dependent on the criteria met.

I am looking to get the Msgbox "Not enough Monitors in stock!" to display a message on its own if the Boolean is true, and the same for MsgBox "Not enough Laptops in stock!".

But if both booleans are true then Msgbox will display "Not enough Laptops in stock! & Not enough Monitors in stock!"

Dim aRow As Long
aRow = Worksheets("MFB Stock").Cells.Find(What:="*", SearchOrder:=xlRows, _
    SearchDirection:=xlPrevious, LookIn:=xlValues).Row   1

Dim Test As Boolean
Test = Val(TextBox4.Text) > Worksheets("MFB Stock").Cells(aRow, 3).Value

If Test = True Then
    MsgBox "Not enough Monitors in stock!"
    Exit Sub
End If

'compares cells A2 to textbox 21 - Laptop
Dim Test1 As Boolean
Test1 = Val(TextBox12.Text) > Worksheets("MFB Stock").Cells(aRow, 1).Value

If Test1 = True Then
    MsgBox "Not enough Laptops in stock!"
    Exit Sub
End If

CodePudding user response:

Use an intermediate variable to collect the message(s) and separate them with a newline:

Dim msg As String

Dim TestMonitor As Boolean
TestMonitor = Val(TextBox4.Text) > Worksheets("MFB Stock").Cells(aRow, 3).Value
If TestMonitor Then msg = msg & IIf(msg = "", "", vbCrLf) & "Not enough Monitors in stock!"

Dim TestLaptop As Boolean
TestLaptop = Val(TextBox12.Text) > Worksheets("MFB Stock").Cells(aRow, 1).Value
If TestLaptop Then msg = msg & IIf(msg = "", "", vbCrLf) & "Not enough Laptops in stock!" 

If msg <> "" Then
   MsgBox msg, vbExclamation
   Exit Sub
End If
  • Related