#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 9,101
Default Macro Help

In the visual basic window there is a help feature. I typed loop and go the
following

Looping Through a Range of Cells
See AlsoSpecificsWhen using Visual Basic, you often need to run the same
block of statements on each cell in a range of cells. To do this, you combine
a looping statement and one or more methods to identify each cell, one at a
time, and run the operation.

One way to loop through a range is to use the For...Next loop with the Cells
property. Using the Cells property, you can substitute the loop counter (or
other variables or expressions) for the cell index numbers. In the following
example, the variable counter is substituted for the row index. The procedure
loops through the range C1:C20, setting to 0 (zero) any number whose absolute
value is less than 0.01.

Sub RoundToZero1()
For Counter = 1 To 20
Set curCell = Worksheets("Sheet1").Cells(Counter, 3)
If Abs(curCell.Value) < 0.01 Then curCell.Value = 0
Next Counter
End Sub

Another easy way to loop through a range is to use a For Each...Next loop
with the collection of cells specified in the Range property. Visual Basic
automatically sets an object variable for the next cell each time the loop
runs. The following procedure loops through the range A1:D10, setting to 0
(zero) any number whose absolute value is less than 0.01.

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

If you don't know the boundaries of the range you want to loop through, you
can use the CurrentRegion property to return the range that surrounds the
active cell. For example, the following procedure, when run from a worksheet,
loops through the range that surrounds the active cell, setting to 0 (zero)
any number whose absolute value is less than 0.01.

Sub RoundToZero3()
For Each c In ActiveCell.CurrentRegion.Cells
If Abs(c.Value) < 0.01 Then c.Value = 0
Next
End Sub



"Ldyflyr" wrote:

Hi:

I have 10 rows of data. I want to cretae a macro and have it loop through
the 10 lines. I think I have to go into Visual Basic and tell it something
like:

for x=10
{macro code}
Next x

But I am not sure of the syntax, or where exatcly it goes in the program.
Can someone help?

Thanks

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 1 February 5th 07 09:31 PM
My excel macro recorder no longer shows up when recording macro jack Excel Discussion (Misc queries) 3 February 5th 07 08:22 PM
using a cell value to control a counter inside a macro and displaying macro value ocset Excel Worksheet Functions 1 September 10th 06 05:32 AM
Macro needed to Paste Values and prevent Macro operation thunderfoot Excel Discussion (Misc queries) 1 June 11th 05 12:44 AM
Macro needed to Paste Values and prevent Macro operation thunderfoot Excel Discussion (Misc queries) 0 June 10th 05 03:38 PM


All times are GMT +1. The time now is 11:01 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"