Checking if cells are empty on "Change" event in Excel vba

Elena Margulis

New Member
Joined
Aug 21, 2020
Messages
25
Office Version
  1. 365
Platform
  1. 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:

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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
" If Not emptyCells Is Nothing Then"

may be

If Not intersect(sh.range("A:A"),emptyCells) or Not intersect(sh.range("C:C"),emptyCells) Is Nothing Then
 
Upvote 0
" If Not emptyCells Is Nothing Then"

may be

If Not intersect(sh.range("A:A"),emptyCells) or Not intersect(sh.range("C:C"),emptyCells) Is Nothing Then
Unfortunately it creates Runtime error 424 and points to the last row of this code:
If wb.Sheets("Sheet1").Saved = False Then wb.Sheets("Sheet1").Save
 
Upvote 0
Try again:

If Not intersect(sh.range("A:A"),emptyCells) is nothing or Not intersect(sh.range("C:C"),emptyCells) Is Nothing Then
 
Upvote 0
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:

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
I have this code to find first empty row (found on internet, and use it now- works for me).
Maybe you can use it?

VBA Code:
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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