Solved

how loop over a range in Excel VBA

Posted on 2013-12-31
2
472 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 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
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
Comment Utility
Thank you very much, very helpful and very fast....

I wish you the best for the new year.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now