Check if a column is blank

bcmk29

Board Regular
Joined
Oct 20, 2022
Messages
55
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
The below code works fine when the selected column has more than 5 values. But, when the selected column is empty the code doesn't validate the >5 condition and throws a run time error. Can someone please help?

VBA Code:
abc:
    n = Selection.Cells.SpecialCells(xlCellTypeConstants).Count
    If n > 5 Then
    Set InputRng = Application.Selection
    Set ReplaceRng = tblAdmin.Range("M2:N90")
    For Each rng In ReplaceRng.Columns(1).Cells
    InputRng.Replace What:=rng.Value, Replacement:=rng.Offset(0, 1).Value, LookAt:=xlWhole, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    Next
    ActiveCell.Next.EntireColumn.Select
GoTo abc
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Exit Sub if n=0:

VBA Code:
abc:
    n = Selection.Cells.SpecialCells(xlCellTypeConstants).Count
    If n= 0 then Exit Sub
    If n > 5 Then
    .......
 
Upvote 0
Exit Sub if n=0:

VBA Code:
abc:
    n = Selection.Cells.SpecialCells(xlCellTypeConstants).Count
    If n= 0 then Exit Sub
    If n > 5 Then
    .......
n=0 doesn't seem to work. Because if the column is empty it doesn't give any value(0).
 
Upvote 0
Or:
VBA Code:
if n is nothing then Exit sub

Or even
VBA Code:
If Selection.Cells.SpecialCells(xlCellTypeConstants) is nothing then Exit Sub

?
 
Upvote 0
Or:
VBA Code:
if n is nothing then Exit sub

Or even
VBA Code:
If Selection.Cells.SpecialCells(xlCellTypeConstants) is nothing then Exit Sub

?
I tried both the codes, it throws me Run Time Error 1004, No cells were found.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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