Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2015-01-30
1
Medium Priority
?
261 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
[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
1 Comment
 
LVL 23

Accepted Solution

by:
Ejgil Hedegaard earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

609 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