Can someone help me with my coding to return error box only one time and also to create msgbox as Critical?

KristenB

New Member
Joined
Nov 9, 2019
Messages
21
Hello, I am attempting to create an error message msgbox in an Excel workbook/worksheet. I have the coding to create the msgbox but if the error is on the worksheet multiple times, it shows the error multiple times. I have tried using alreadyprompted coding to stop this action but I'm having no luck. Additionally, I would like the error message to show a 'critical' icon on the error message and that is also not working. I would appreciate any help you can give. Here is the coding that I have so far:


Private Sub ProjNumbrReq()


Static alreadyPrompted As Boolean
If alreadyPrompted Then Exit Sub

Dim myCell As Range


With Worksheets("Travel Expense Voucher")
For Each myCell In .Range("U15:U45")
If myCell.Value > 0 And .Cells(myCell.row, "N") = "" Then

MsgBox "Project Number must be provided on each line where reimbursement is being claimed.", vbInformation, "Important:"
alreadyPrompted = True

End If
Next myCell
End With
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I just wanted to update on this thread post, I was able to get the 'critical' icon on my msgbox.. but still cannot get it to only present 1 time.
 
Upvote 0
.
Try this :



Code:
Sub ProjNumbrReq()
                                                
Static alreadyPrompted As Boolean
                                                
Dim myCell As Range
                                                
                                                
With Worksheets("Travel Expense Voucher")
    For Each myCell In .Range("U15:U45")
        If myCell.Value > 0 And .Cells(myCell.Row, "N") = "" Then
            MsgBox "Project Number must be provided on each line where reimbursement is being claimed.", vbCritical, "Important:"
            Exit Sub
        End If
    Next myCell
End With
                                                
End Sub
 
Upvote 0
It was really that easy? I have spent the last 4 hours trying to figure out where I goofed. :) Thank you! Can you tell me how I will get this to work in my document? I am currently running it in the VBA screen and it works but I want it to run whenever the user selects the 'save or save as' buttons.
 
Upvote 0
.
I want it to run whenever the user selects the 'save or save as' buttons.

Please be more specific. How is the User accessing the 'save or save as' buttons ?
 
Upvote 0
So I copied this coding into the 'Workbook' module and it worked fine the first time I selected 'run' but now it will not run again. Should it no run each time the workbook is saved as my other macro in that module does?
 
Upvote 0
The user is completing the document, they will then save the document as an excel file to send in for submission. I have moved the coding into the 'workbook' module with another macro that runs 'before save' but it will not work?
 
Upvote 0
.
The macro should be pasted into a Regular Module not in the ThisWorkbook module.

To have the macro run when the user saves the workbook paste this macro into the ThisWorkbook module :

Code:
Option Explicit


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    ProjNumbrReq
End Sub
 
Upvote 0
I'm getting the following error:

 
Upvote 0
The error didn't display correctly.

???
 
Upvote 0

Forum statistics

Threads
1,224,109
Messages
6,176,391
Members
452,726
Latest member
HaploTheGreat

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