I could not get the code from gyclone to work
What do you mean; what happens when you try?
If you mean that it runs, but didn't do what you want, that's because what you asked for the first time is not actually what you wanted, based on your last post. If you get an error, what is the error?
As for your new code, look at this (same code with comments added telling you what is actually happening). See if that helps you see the problems.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim nRws As Long, nFilCnt As Long, rMyRng As Range
With Sheets("DOMESTIC")
' set nRws to last row of data in column D
nRws = .Cells(Rows.Count, "D").End(xlUp).Row
Set rMyRng = Range("a5494:d" & nRws)
' Count number of cells that are NOT empty
nFilCnt = WorksheetFunction.CountA(rMyRng)
' if total number of rows in worksheet, minus one, times 3, does not equal the total numer of non-blank cells in range
' it never will, it's impossible
' number of rows in worksheet is at least 5494 rows more than in range
' nRws needs to be the number of rows in range, not in worksheet, for this to work
If (nRws - 1) * 3 <> nFilCnt Then
' Will throw error if no blank cells in range
' Correcting the calculation for nRws will prevent getting here if no blank cells
' But, right now, the code will always get here, whether there are blanks or not
rMyRng.SpecialCells(xlCellTypeBlanks).Select
sgBox "Fill The Selected Blank Cells to Exit This File", vbCritical, "Action Required"
Cancel = True
End If
End With
End Sub
Bookmarks