Looping through a range of cells

C

Conan Kelly

Hello all,

I've been looking through the
Help files for this and this
is what they have
listed:

Sub RoundToZero2()
For Each c In
Worksheets("Sheet1").Range("A1:D10").Cells
If Abs(c.Value) < 0.01
Then c.Value = 0
Next
End Sub


The range I want to use in
this loop is already selected
(after pasting, the pasted
data is selected). So I tried
changing
"Worksheets("Sheet1").Range("A1:D10").Cells"
to
"ActiveWindow.RangeSelection.Cells".
No workie...


The other thing I'm concerned
about is the "For Each c":
Does "c" need to be set to an
object variable (like a "Cell"
or "Cells" object or something
like that), or will Excel/VBA
automatically know that "c"
means each cell in the range.

thanks for any help anyone can
provide,

Conan Kelly
 
B

Bob Phillips

Try

Sub RoundToZero2()
For Each c In Selection
If Abs(c.Value) < 0.01 Then c.Value = 0
Next c
End Sub

If c is not declared, VBA will assume type variant and sub-type as
appropriate. But it i9s always better to declare it, and c should be
declared as type Range.


--

HTH

RP
(remove nothere from the email address if mailing direct)
 

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

Top