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
 
Well that didn't work... this is my coding in ThisWorkbook and I am getting an AmbiguousName error on Workbook_BeforeSave

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


'When a row begins with X in Travel Expense Codes worksheet, hide the row


Const beginRow As Long = 3
Const endRow As Long = 38
Const chkCol As Long = 14


Dim rowCnt As Long
Dim ws As Worksheet


Set ws = ThisWorkbook.Worksheets("Travel Expense Codes")


For rowCnt = endRow To beginRow Step -1
With ws.Cells(rowCnt, chkCol)
.EntireRow.Hidden = (.Value = "X")
End With

Next rowCnt


End Sub




Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ProjNumbrReq
End Sub
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
.
You can only use Before Save once in ThisWorkbook module :

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)




'When a row begins with X in Travel Expense Codes worksheet, hide the row




Const beginRow As Long = 3
Const endRow As Long = 38
Const chkCol As Long = 14




Dim rowCnt As Long
Dim ws As Worksheet




Set ws = ThisWorkbook.Worksheets("Travel Expense Codes")




    For rowCnt = endRow To beginRow Step -1
        With ws.Cells(rowCnt, chkCol)
            .EntireRow.Hidden = (.Value = "X")
        End With
    
    Next rowCnt


[B][COLOR=#ff0000]ProjNumbrReq[/COLOR][/B]


End Sub
 
Upvote 0
I had a feeling that was the case but I couldn't figure out how else to put that in there. However, now I'm getting an error that 'sub or function not defined'
 
Upvote 0
.
Ok .... in a Regular Module paste this macro :

Code:
Option Explicit


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


In ThisWorkbook module paste this macro :

Code:
Option Explicit


Private Sub Workbook_BeforeClose(Cancel As Boolean)
    ProjNumbrReq
End Sub

Make certain both of these macros are not located in any other locations in the workbook modules.

In order for the macro ProjNumbrReq to run automatically, it must be located in the Workbook_BeforeClose event. Otherwise you will continue to receive the error
as before.
 
Upvote 0
.
It is very late here. Need to sleep.

I'll check this in the morning to see how it turned out.
 
Upvote 0

Forum statistics

Threads
1,215,242
Messages
6,123,827
Members
449,127
Latest member
Cyko

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