Solved

how loop over a range in Excel VBA

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how can I determine if an Excel spreadsheet will fit on an 8.5 by 11 inch sheet? 3 60
Excel VBA Script 9 58
script to read text file 34 34
count number 10 30
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

752 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