Solved

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

Posted on 2015-01-30
1
187 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 20

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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,…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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…

763 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