Elena Margulis
New Member
- Joined
- Aug 21, 2020
- Messages
- 25
- Office Version
- 365
- Platform
- Windows
I have a macro enabled excel sheet - Sheet_Change.xlsm
My goal is to check if a columns A and C only - are empty or not within a table.
If cells are empty, then a message should appear and user shouldn't be able to enter a new record (row).
It is currently works on any "Change" event.
But I need it to work only after entering a row or - even better - after activating a new worksheet - after switching from sheet 1 to sheet 2 (if that's easier to achieve)
Also, it's now working for ANY empty cells, while I need it to work for the columns A & C only.
Please, see the vba code below:
My goal is to check if a columns A and C only - are empty or not within a table.
If cells are empty, then a message should appear and user shouldn't be able to enter a new record (row).
It is currently works on any "Change" event.
But I need it to work only after entering a row or - even better - after activating a new worksheet - after switching from sheet 1 to sheet 2 (if that's easier to achieve)
Also, it's now working for ANY empty cells, while I need it to work for the columns A & C only.
Please, see the vba code below:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'
' 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(2, Columns.Count).End(xlToLeft).Column
On Error GoTo NoBlanks
Set emptyCells = Sh.Range(Sh.Cells(2, 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 wb.Sheets("Sheet1").Saved = False Then wb.Sheets("Sheet1").Save
'Workbook will be saved & closed if all cells in UsedRange are filled
End If
End Sub