ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping through a range of cells (https://www.excelbanter.com/excel-programming/337669-looping-through-range-cells.html)

Conan Kelly

Looping through a range of cells
 
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



Conan Kelly

Looping through a range of cells
 
Never mind. I got it to work.
Thanks anyways.

Conan


"Conan Kelly" <CTBarbarin at
msn dot com wrote in message
...
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




Bob Phillips[_6_]

Looping through a range of cells
 
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)


"Conan Kelly" <CTBarbarin at msn dot com wrote in message
...
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






All times are GMT +1. The time now is 03:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com