Solved

how loop over a range in Excel VBA

Posted on 2013-12-31
2
507 Views
Last Modified: 2013-12-31
Hello I need to create a function in Excel using VBA.

I need to pass a range to loop it and perform some calculations.

The idea is use a for next bucle to read all the rows, and analice the diferente columns in it.

The problem is that I don't know how to reference the cells to read it.

Any idea?

At the moment I have this...


Function rangeAnalizer(num As Integer, fecha As Date, rango As Range)
    Dim intNumFilas As Integer
    Dim intNumColumnas As Integer
    Dim celda As Range.Cells
    intNumFilas = rango.Rows.Count
    intNumColumnas = rango.Columns.Count
   
    Dim prueba As Integer
    prueba = celda(1, 1).Item ?????
0
Comment
Question by:limmontreefree
2 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39748757
The easiest way to get a value of a cell in a range passed to a function is:
Dim intNumFilas As Long      'Avoids overflow if more than 32767 rows
Dim x As Variant
For i = 1 To intNumFilas
     x = rango.Cells(i, 1).Value
     'Do stuff with x
Next

Open in new window

You can also pass all the values into a Variant array. This will be considerably faster than looping through the cells.
Dim intNumFilas As Long      'Avoids overflow if more than 32767 rows
Dim v As Variant
For i = 1 To intNumFilas
     v = rango.Value
     'Do stuff with v(i, 1), v(i, 2), etc.
Next

Open in new window

0
 

Author Closing Comment

by:limmontreefree
ID: 39749339
Thank you very much, very helpful and very fast....

I wish you the best for the new year.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question