exit message box if press close or cancel

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,483
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
I create code to show message based on condition . my formula is based on msgbox =IF(F5<0,MSG(),"")
VBA Code:
Private Sub MSG()
Dim Action As VbMsgBoxResult
Action = MsgBox("the date has been expired ", vbOKCancel + vbExclamation + vbDefaultButton2, "warning!")
If Action = vbOK Then
End If

If Action = vbCancel Then Exit Sub

End Sub
so what I want if I press cancel or close button when show the message box based on formula is existed in some cells ,then should the message should close
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I'm not sure what you ask for. Hope the following can help you.

VBA Code:
Private Sub MSG()
  Dim Action As VbMsgBoxResult
  If Range("F5").Value < 0 Then
    Action = MsgBox("the date has been expired ", vbOKCancel + vbExclamation + vbDefaultButton2, "warning!")
    Select Case Action
      Case vbOK
        'here actions
      Case vbCancel
        Exit Sub
    End Select
  End If
End Sub
 
Upvote 0
in column g I have many formula
1.xlsm
FG
4-70
5-60
6-50
7-40
8-30
9-20
10-10
110 
1223 
1323 
1423 
15
1
Cell Formulas
RangeFormula
F4:F14F4=D4-TODAY()
G4:G14G4=IF(F4<0,MSG(),"")
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G4:G14Cell Value=0textNO
F4:F14Cell Value>0textNO
F4:F14Cell Value<0textNO

your code works when run macro , but I want run the macro based on column G which contain the formula . if the formula in column G achieve the condition then should show message
 
Upvote 0
You want the macro to loop through column G and send a message every time the condition is met.
If the cell meets the condition 10 times, then you want the message 10 times?
 
Upvote 0
You want the macro to loop through column G and send a message every time the condition is met.
If the cell meets the condition 10 times, then you want the message 10 times?
yes but if I press close (x) or cancel the message should be gone
 
Upvote 0
How about

VBA Code:
Private Sub MSG()
  Dim c As Range
  '
  For Each c In Range("F4", Range("F" & Rows.Count).End(3))
    If c.Value < 0 Then
      If MsgBox("the date has been expired ", vbOKCancel + vbExclamation, "warning!") = vbCancel Then Exit Sub
    End If
  Next
End Sub
 
Upvote 0
Solution
unfortunately doesn't work well . when I press ok continues showing the message and if I press cancel or close reaches for 8 times . the right way when press ok should shows 8 times based on condition in 8 cells and when close or cancel should be the message is closed . should not repeat show for 8 times .
 
Upvote 0
I would add this information . the macro in post# 6 works as what I want . just in one case if I run the macro by button . but if I depends on formula in column G
VBA Code:
=IF(F4<0,MSG(),"")

it will occurs the problems as I have ever mentioned in post#7
 
Upvote 0
the macro in post# 6 works as what I want . just in one case if I run the macro by button
That's what you asked for. A macro that runs through the entire column F. And the message appears in each cell when the cell meets the condition.
I don't see any sense in your formula. All formulas are executed. They do not depend on a button to cancel. You cannot stop the execution of the formulas, unless you turn off all the formulas.
 
Upvote 0
based on this
All formulas are executed. They do not depend on a button to cancel. You cannot stop the execution of the formulas, unless you turn off all the formulas.
I decided put the macro in workbook open event and delete the column G instead of put the formula and show problem by repeat showing the message.
many thanks for this macro .it helps me so much ;)
 
Upvote 0

Forum statistics

Threads
1,222,620
Messages
6,167,086
Members
452,094
Latest member
Roberto Saveru

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