Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how loop over a range in Excel VBA

Posted on 2013-12-31
2
Medium Priority
?
530 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

722 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