views:

36

answers:

1

I have a function which accepts fromRange and ToRange of an Excel cell. basically i want to read cell by cell values from the range. suppose if i pass E2 and E9 i want to read in a loop something like Range(E2).value, Range(E3).value and so on till E9

How can i get the between cell addresses. Please help

A: 
Option Explicit

Private Sub calculateRangeOneByOne()
    Dim rangeIterator As Range
    Dim rangeToIterate As Range
    Dim sum As Double


    Set rangeToIterate = Range("A8", "E8")
    sum = 0#
    For Each rangeIterator In rangeToIterate
        sum = sum + rangeIterator
    Next


End Sub

You usually does not want to iterate over ranges one-by-one. there are tons of functions which work on ranges and so this example is definitly a poor one. You'd better use e.g Sum here but just to give you an idea. A range is a collection and you can iteratee over it with for each, You can also use for with index access. But this is at least a bit less "pain"

Friedrich
Thank you Freidrich you saved my time
Sathish