Target.Cells.Count

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

What is the function of
If Target.Cells.Count > 1 then
Exit sub
End If

and is it necessary as the first test in a Worksheet_Change(ByValue as
Range) sub?
 
This is to make sure that no action is taken if the user changes several
cells at once.
 
It is used to test whether more than one cell is being changed in one go,
and exits if so.

It isn't necessary, it is a choice. You could cater for multiple cells, and
process each one in a loop.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
What is the function of
  If Target.Cells.Count > 1 then
     Exit sub
  End If

and is it necessary as the first test in a Worksheet_Change(ByValue as
Range) sub?              

Hi
Depends what you are doing. If you want the worksheet_change to
trigger when the value in one cell changes, then you need to check
that more than one cell did not change i.e. other cells you are not
bothered about.
quite often you will see

if intersect(myRange, Target).Count>1 then
Exit Sub
End if

so you only want one cell within the range myRange to trigger the
change (note .Cells is not required)

You will also see variants of

Set Testrange = intersect(myRange, Target)
if not Testrange is nothing then
if Testrange.count = 1 then
'do something
else
Exit Sub
end If
End if

So you are checking that Target is within a certain range AND that it
is only one cell, before change is triggered.

Target can be more than one cell though (e.g. a sum of cell values
reaching a value triggers change).

regards
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top