How to check if a range of cells is empty using VBA?

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

Can someone please advise what the quickest way of checking if a range of cells eg A1:Y1 is empty, using VBA, please?

I found this code online, but I'm not sure how to modify it....?



<tbody style="box-sizing: border-box; border: 0px !important; margin: 0px !important; padding: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-size: 1em !important; direction: ltr !important;">
[TD="class: content"]<code class="keyword" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-weight: bold !important; direction: ltr !important; display: inline !important; color: rgb(0, 102, 153) !important;">If</code> <code class="plain" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; direction: ltr !important; display: inline !important;">IsEmpty(Cell) </code><code class="keyword" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-weight: bold !important; direction: ltr !important; display: inline !important; color: rgb(0, 102, 153) !important;">Then</code>[/TD]

</tbody>


<tbody style="box-sizing: border-box; border: 0px !important; margin: 0px !important; padding: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-size: 1em !important; direction: ltr !important;">
[TD="class: number"]<code style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border: 0px !important; outline: 0px !important; background: none !important; text-align: right !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 2.7em !important; line-height: 1.1em !important; direction: ltr !important; display: block !important;">2</code>[/TD]
[TD="class: content"]<code class="spaces" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; direction: ltr !important; display: inline !important;"> </code><code class="plain" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; direction: ltr !important; display: inline !important;">StatementsIfCellIsEmpty</code>[/TD]

</tbody>


<tbody style="box-sizing: border-box; border: 0px !important; margin: 0px !important; padding: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-size: 1em !important; direction: ltr !important;">
[TD="class: number"]<code style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border: 0px !important; outline: 0px !important; background: none !important; text-align: right !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 2.7em !important; line-height: 1.1em !important; direction: ltr !important; display: block !important;">3</code>[/TD]
[TD="class: content"]<code class="keyword" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-weight: bold !important; direction: ltr !important; display: inline !important; color: rgb(0, 102, 153) !important;">Else</code>[/TD]

</tbody>


<tbody style="box-sizing: border-box; border: 0px !important; margin: 0px !important; padding: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-size: 1em !important; direction: ltr !important;">
[TD="class: number"]<code style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border: 0px !important; outline: 0px !important; background: none !important; text-align: right !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 2.7em !important; line-height: 1.1em !important; direction: ltr !important; display: block !important;">4</code>[/TD]
[TD="class: content"]<code class="spaces" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; direction: ltr !important; display: inline !important;"> </code><code class="plain" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; direction: ltr !important; display: inline !important;">StatementsIfCellIsNotEmpty</code>[/TD]

</tbody>


<tbody style="box-sizing: border-box; border: 0px !important; margin: 0px !important; padding: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-size: 1em !important; direction: ltr !important;">
[TD="class: number"]<code style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border: 0px !important; outline: 0px !important; background: none !important; text-align: right !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: 2.7em !important; line-height: 1.1em !important; direction: ltr !important; display: block !important;">5</code>[/TD]
[TD="class: content"]<code class="keyword" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-weight: bold !important; direction: ltr !important; display: inline !important; color: rgb(0, 102, 153) !important;">End</code> <code class="keyword" style="box-sizing: border-box; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; background: none !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; line-height: 1.1em !important; font-weight: bold !important; direction: ltr !important; display: inline !important; color: rgb(0, 102, 153) !important;">If</code>[/TD]

</tbody>


TIA
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Formula
Code:
=If( CountA(A2:F2) = 0, "Empty", "Not Empty")
VBA
Code:
If.Application.CountA(Range("A2:F2") = 0 Then
    MsgBox "Empty"
Else
    MsgBox "Not Empty"
End If

the IsEmpty function is not intended to check if a cell contains data. It was designed to test variables to see if they were initialized.
 
Last edited:
Upvote 0
Hi JLGWhiz

Apologies for the delay in responding.

When I copy and paste the code above, as a separate Sub (to test it), I get a "Compile error" that says "Expected: Identifier..."

Do you know what is missing?

Sub If()


If.Application.CountA(Range("A2:F2") = 0 Then


MsgBox "Empty"
Else
MsgBox "Not Empty"
End If


End Sub


Thanks in advance.
 
Last edited:
Upvote 0
Actually, I found a solution online (below), so it's ok. Thank you for the prompt response, however.

Sub Empty()


If WorksheetFunction.CountA(Range("A5:ac5")) = 0 Then
MsgBox "Empty"
Else
MsgBox "Not Empty"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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