I have a few processes that are executing in my Workbook - within Workbook_BeforeClose sub The problem is that the 1st action - "Checking on empty cells:" is not working properly with the other 2 actions - "CreatingRawData Macro:" and "Exporting Data:"
If there are empty cells in my Workbook and when I close out of Worksheet - instead of stop working, and populate error messages about an empty cells - it proceeds to the next action - and records macro and then exports it. It works perfectly well - only when I remove "CreatingRawData" and "Exporting Data" But I must have these in my vba...
What should I do to stop executing further if there empty cells in my Workbook? (I have Cancel = True within "Checking on empty cells", still not stopping)
Here is the full code, I have withing "ThisWorkbook":
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'
' Checking on empty cells:
'
Dim sh As Worksheet, lastRow As Long, lastCol As Long, emptyCells As Range
Set sh = ActiveSheet 'use here your sheet
lastRow = sh.Range("A" & Rows.Count).End(xlUp).Row
lastCol = sh.Cells(1, Columns.Count).End(xlToLeft).Column
On Error GoTo NoBlanks
Set emptyCells = sh.Range(sh.Cells(5, 1), sh.Cells(lastRow,
lastCol)).SpecialCells(xlCellTypeBlanks)
If Not emptyCells Is Nothing Then
MsgBox "There are empty cells, which must be filled: " & emptyCells.Address(0, 0)
sh.Activate: emptyCells.Select
Cancel = True
Else
NoBlanks:
Cancel = False
If Me.Saved = False Then Me.Save
'Workbook will be saved & closed if all cells in UsedRange are filled
End If
'
' CreatingRawData Macro: (manually recorded)
'
Cells.Select
Selection.Copy
Sheets(" ClientSatisfactionForm").Select
Sheets.Add
Cells.Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Rows("1:3").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Cells.Select
Cells.EntireColumn.AutoFit
Range("A:A,D:D,F:F,H:H,J:J,L:L,N:N,P:P").Select
Range("P1").Activate
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 12
ActiveWindow.ScrollColumn = 13
ActiveWindow.ScrollColumn = 14
Range("A:A,D:D,F:F,H:H,J:J,L:L,N:N,P:P,R:R,T:T,V:V,X:X").Select
Range("X1").Activate
ActiveWindow.ScrollColumn = 15
ActiveWindow.ScrollColumn = 16
ActiveWindow.ScrollColumn = 17
ActiveWindow.ScrollColumn = 18
ActiveWindow.ScrollColumn = 19
Range("A:A,D:D,F:F,H:H,J:J,L:L,N:N,P:P,R:R,T:T,V:V,X:X,Z:Z").Select
Range("Z1").Activate
Selection.Delete Shift:=xlToLeft
ActiveWindow.ScrollColumn = 18
ActiveWindow.ScrollColumn = 17
ActiveWindow.ScrollColumn = 16
ActiveWindow.ScrollColumn = 14
ActiveWindow.ScrollColumn = 13
ActiveWindow.ScrollColumn = 12
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Columns("B:B").Select
Selection.NumberFormat = "m/d/yyyy"
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
Range("O1").Select
ActiveCell.FormulaR1C1 = "SurveyCode"
Range("A1").Select
'
' Exporting Data:
'
Dim wbkExport As Workbook
Dim shtToExport As Worksheet
Set shtToExport = ThisWorkbook.Worksheets("Sheet1") 'Sheet to export as CSV
Set wbkExport = Application.Workbooks.Add
shtToExport.Copy Before:=wbkExport.Worksheets(wbkExport.Worksheets.Count)
Application.DisplayAlerts = False 'Possibly overwrite without asking
wbkExport.SaveAs Filename:="E:\Projects\Client Satisfaction Survey\ClientSatisfactionSurvey.csv",
FileFormat:=xlCSV
Application.DisplayAlerts = True
wbkExport.Close SaveChanges:=False
Application.DisplayAlerts = False
Application.Quit
End Sub
Below, please see the Worksheet image screenshot (it's too wide to fit all the fields). Data range - from A1:Z3 contains some empty cells - it's my header.
Doesn't have to be checked on empty cells...