Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how loop over a range in Excel VBA

Posted on 2013-12-31
2
Medium Priority
?
534 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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

783 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