Solved

how do we get the size of input range in Excel VBA?

Posted on 2015-01-30
1
190 Views
Last Modified: 2015-01-30
Hi experts,

I try to do something and need to have a range as input in VBA function. But when I use the function, it always gives me an error, what could be wrong.

Function getSize(arr)
    rowSize = UBound(arr, 1) - LBound(arr, 1) + 1
    colSize = UBound(arr, 2) - LBound(arr, 2) + 1
    MsgBox ("input is a " & TypeName(arr) & "with size of " & rowSize & " X " & colSize & ".")    
End Function

Please take a look.
RDB
0
Comment
Question by:ResourcefulDB
1 Comment
 
LVL 21

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
ID: 40580825
Arr is not an array, but a range, so you can not use UBound and LBound.

2 options

Load the range into an array like this
Function getSize(arrIn)
    arr = arrIn
    rowSize = UBound(arr, 1) - LBound(arr, 1) + 1
    colSize = UBound(arr, 2) - LBound(arr, 2) + 1
    MsgBox ("input is a " & TypeName(arrIn) & "with size of " & rowSize & " X " & colSize & ".")
End Function

Open in new window


Or get the size directly on the range like this
Function getSize(arr As Range)
    Dim rowSize As Long, colSize As Long
    rowSize = arr.Rows.Count
    colSize = arr.Columns.Count
    MsgBox ("input is a " & TypeName(arr) & " with size of " & rowSize & " X " & colSize & ".")
End Function

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 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

13 Experts available now in Live!

Get 1:1 Help Now