If Nothing Then VBA

kmmsquared

New Member
Joined
Jan 7, 2011
Messages
33
Hi!

I am running into a problem with my code below. When Range("AB2:AB" & lngLastRow) is nothing, Column AB doesn't delete. How should I fix this? Thanks!

Code:
Sub Check()
 
Dim lngLastRow As Long
 
With Sheets("B")
lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set Check = Range("AB2:AB" & lngLastRow)

Columns("AB:AB").Copy
Columns("AB:AB").PasteSpecial Paste:=xlPasteValues
Columns("AB:AB").AutoFilter Field:=1, Criteria1:="=UD", Operator:=xlOr, Criteria2:="="
If Not Check Is Nothing Then Check.SpecialCells(xlCellTypeVisible).ClearContents
Columns("AB:AB").AutoFilter
        
[COLOR=red]If Check Is Nothing Then
[/COLOR][COLOR=red]Columns("AB:AB").Delete[/COLOR]
[COLOR=red]Else
[/COLOR]    Columns("AB:AB").AutoFilter Field:=1, Criteria1:="<>"
    On Error Resume Next
    Range("AB2:AB" & lngLastRow).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 22
 
    Columns("AB:AB").AutoFilter
End If
End With
 
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
A few problems.

How can it be nothing? It looks like it is a range. Do you mean no data in the range?

Also, it is not a good idea to use a variable in your sub procedure that has the same name as the sub procedure. That could confuse VBA, because then anytime you use "Check" in your code, it might have a hard time trying to figure out if you are calling the "Check" procedure, or the "Check" range variable.
 
Upvote 0
I do mean no data in range...that is what I was going for, how do I do that?

I will fix the variable/name of sub too!
 
Upvote 0
It cannot be Nothing - you have just assigned a range to it. Maybe

Code:
If WorksheetFunction.CountA(check) = 0 Then
 
Upvote 0
Here is an example of how you can check to see if your range is empty:
Code:
    Dim myRange As Range
    Dim myRangeEmpty As Boolean

'   Set your range here    
    Set myRange = Range("A1:A4")
 
'   Check to see if range is empty (set myRangeEmpty variable equal to true/false)
    If Application.WorksheetFunction.CountBlank(myRange) = myRange.Cells.Count Then
        myRangeEmpty = True
    Else
        myRangeEmpty = False
    End If
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,248
Members
452,900
Latest member
LisaGo

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