Solved

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

Posted on 2015-01-30
1
200 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 22

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 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