Looping question

J

Jase

I have a looping function that copies from a range and then pastes into
another. However I can't seem to get the function to end looping when I would
like. This is what I have to exit the loop and it does not work.

Do
If IsEmpty(RngToCopy) Then
Exit Sub
 
T

Tom Hutchins

IsEmpty returns a True/False value indicating whether a variable has been
initialized. If your code is assigning a range to RngToCopy, then it has been
initialized and will return TRUE. If you want to test whether RngToCopy
contains anything, try

If Len(RngToCopy.Value) = 0 Then Exit Sub

Hope this helps,

Hutch
 
D

Dave Peterson

If rngtocopy is a single cell, then it should work.

But isempty() really checks to see if the cell is empty--no formula that
evaluates to "".

If you have a formula that evaluates to "", you could use:

if rngtocopy.value = "" then
exit sub

=======
If rngtocopy is multiple cells, then you might be able to use:

if application.counta(rngtocopy) = 0 then
'all are empty
exit sub

Again, if you have formulas that evaluate to "", then this won't work.
=Counta() will include the cells with formulas.
 
Top