VBA isblank function

chenchanjuan

New Member
Joined
Apr 15, 2004
Messages
20
Hello,

I have a question about the isblank function.

I have code like this:

Sub checkempty()

dim row as integer
For row=1 to 10
if [and(isblank(range(cells(row,1),cells(row,5)))] = T then
msgbox "all empty"
else msgbox "not all empty"
end if

next
End sub

If I use the isblank function without loop, say
if [and(isblank(A1:A5))] = T then blahblah

it works. Do you know how to add a loop into the isblank function?

Thank you so much
Cathy
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hello,

You can't use brackets on variable ranges. Try the following:

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> checkempty()
<SPAN style="color:darkblue">Dim</SPAN> cl <SPAN style="color:darkblue">As</SPAN> Range
<SPAN style="color:darkblue">For</SPAN> <SPAN style="color:darkblue">Each</SPAN> cl <SPAN style="color:darkblue">In</SPAN> [a1:a10]
    <SPAN style="color:darkblue">If</SPAN> Evaluate("SUM(LEN(" & cl.Resize(, 5).Address _
        & "))") <SPAN style="color:darkblue">Then</SPAN>
        MsgBox "Not All Empty"
        Else: MsgBox "All Empty"
    <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">Next</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0
As an option, and if NateO could confirm this (please? :) ) that you may be able to use the IsEmpty instead of an isblank type of test.
 
Upvote 0
Hello Zack,

Not on a multi-cell range (which Cathy is asking about). Her functional code ([and(isblank(A1:A5))]) is an array function as is the one I posted.

IsEmpty() won't evaluate all of the cells in one pass, you'd need a second loop.
 
Upvote 0
True, but a neat little trick that bypasses the idiosyncracies of the Evaluate function would be to check if the result of the COUNTA function is >0.
NateO said:
Hello Zack,

Not on a multi-cell range (which Cathy is asking about). Her functional code ([and(isblank(A1:A5))]) is an array function as is the one I posted.

IsEmpty() won't evaluate all of the cells in one pass, you'd need a second loop.
 
Upvote 0
tusharm said:
True, but a neat little trick that bypasses the idiosyncracies of the Evaluate function would be to check if the result of the COUNTA function is >0.

That's a good call. :) Do either of you guys know a strictly VBA call that will discern the presence of a value in a cell w/o looping or reverting to native Excel functions? Just curious.
 
Upvote 0
Tushar said:
True, but a neat little trick that bypasses the idiosyncracies of the Evaluate function would be to check if the result of the COUNTA function is >0.
Which in turn is true, and a better way to proceed, that's what it's there for eh. It might look like:

<font face=Courier New><SPAN style="color:darkblue">Sub</SPAN> checkempty()
<SPAN style="color:darkblue">Dim</SPAN> cl <SPAN style="color:darkblue">As</SPAN> Range
<SPAN style="color:darkblue">For</SPAN> <SPAN style="color:darkblue">Each</SPAN> cl <SPAN style="color:darkblue">In</SPAN> [a1:a10]
    <SPAN style="color:darkblue">If</SPAN> WorksheetFunction.CountA(cl.Resize(, 5)) <SPAN style="color:darkblue">Then</SPAN>
        MsgBox "Not All Empty"
        Else: MsgBox "All Empty"
    <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">Next</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>

Apparently I wasn't seeing the forest through the trees! Maybe I should stop breaking the board and think more about my Excel posts! :LOL:
 
Upvote 0
Hello again Zack,
firefytr said:
Do either of you guys know a strictly VBA call that will discern the presence of a value in a cell w/o looping or reverting to native Excel functions? Just curious.
A single cell, isEmpty(). E.g., MsgBox IsEmpty([b2])

You also have SpecialCells(xlblanks) at your disposal and Autofilter capabilities. What's the issue with using Excel functions in Excel?

Did you have a more specific example in mind?
 
Upvote 0
NateO said:
Did you have a more specific example in mind?

Actually no. Just sheer curiosity. I thought I'd ask the guru's while I had ya on the line. ;) Looking at it, I'm not sure there would really be a better way than to use the CountA function. That's pretty fast.

Thanks! TTYL
 
Upvote 0
Hope you guys don't mind me throwing in another suggestion....

Sub CheckForBlanks()
For Rw = 1 To 10
BLankCellCount = 0
On Error Resume Next
BLankCellCount = Range("A" & Rw & ":E" & Rw).SpecialCells(xlConstants, 3).Count
If BLankCellCount > 0 Then
MsgBox "Row " & Rw & " Not Blank"
Else
MsgBox "Row " & Rw & " is Blank"
End If
Next Rw
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,573
Messages
6,120,310
Members
448,955
Latest member
Dreamz high

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