Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Excel Tutorial
  • Excel Formulas
  • Excel Shortcut Keys
  • Data Analysis in Excel
  • Formatting in Excel
  • Excel Workbooks
  • Statistical Functions
  • Data Visualization in Excel
  • Pivot Tables in Excel
  • MS Excel Quiz
  • Excel Interview Questions
  • Advance Excel
Open In App
Next Article:
How to Delete a Module in Excel VBA?
Next article icon

How to Get Length of Array in Excel VBA?

Last Updated : 22 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

We use UBound and LBound functions to get the length of an Array in Excel VBA. In this article, we will discuss them in detail.

Syntax: UBound() function

UBound (arrayname, [ dimension ])

Parameters:

  • arrayname: required. Array variable name
  • dimension: optional

Returns: Return upper limit of an array dimension.

Syntax: LBound() Function

LBound (arrayname, [ dimension ])

Parameters:

  • arrayname : required. Array variable name
  • dimension : optional

Returns: Return lower limit of an array dimension

Sample Data: 

VBA Code to get the length of Array (one-dimensional array):

Declare Variables:

Declaring a customer array with the size of 10.

Sub oneDimArrayLength()  ' Array variable Declaration  Dim customer (1 To 10) As String

Assign values to array elements

customer(1) = "ANTON"  customer(2) = "BERGS"  customer(3) = "BOLID"  customer(4) = "KOENE"  customer(5) = "FRANS"

Use UBound function to get the size of an array and Message box to display the result

'Message box to popup length of 1D array  MsgBox "Array has " & UBound(customer) & " element(s)."    End Sub

To Run VBA Code

Press Alt+F8 to popup macro window.  Select " oneDimArrayLength" and Click Run button.

Output

VBA Code to get the length of Array (multi-dimensional array)

Declaring variables:

Declaring ProdAndCustomer multi-dimensional array size of 10 rows and 2 columns

Sub twoDimArrayLength()    ' Array variable Declaration  Dim ProdAndCustomer(1 To 10, 1 To 2) As String, noOfRow As Integer, noOfCol As Integer, noOfElements As Integer

Assign values to array elements

ProdAndCustomer(1, 1) = "Alice Mutton"  ProdAndCustomer(2, 1) = "Boston Crab Meat"  ProdAndCustomer(3, 1) = "Camembert Pierrot"  ProdAndCustomer(4, 1) = "Alice Mutton"  ProdAndCustomer(5, 1) = "Ipoh Coffee"    ProdAndCustomer(1, 2) = "ANTON"  ProdAndCustomer(2, 2) = "BERGS"  ProdAndCustomer(3, 2) = "BOLID"  ProdAndCustomer(4, 2) = "BOTTM"  ProdAndCustomer(5, 2) = "FURIB"

Compute Number of Rows, Number of Columns using UBound and LBound function.  Multiply by noOfRow and noOfCol variable to get Number of elements in multi-dimensional array. 

noOfRow = UBound(ProdAndCustomer, 1) - LBound(ProdAndCustomer, 1) + 1  noOfCol = UBound(ProdAndCustomer, 2) - LBound(ProdAndCustomer, 2) + 1  noOfElements = noOfRow * noOfCol

Message box to popup result

'Message box to popup length of 1D array  MsgBox "Array has " & noOfElements & " element(s)."    End Sub

To Run VBA Code

Press Alt+F8 to popup macro window.  Select " twoDimArrayLength" and Click Run button.

Output:


Next Article
How to Delete a Module in Excel VBA?

S

sk123online
Improve
Article Tags :
  • Excel
  • News
  • Microsoft Office
  • Excel-VBA
  • ExcelGuide

Similar Reads

  • How to Get Value of Multidimensional Array in C?
    Prerequisite: Array in C An array is a type of data structure where we can store multiple elements of similar data types. A multidimensional array can be termed an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. Declaration
    8 min read
  • How to Use for Each Loop in Excel VBA?
    A For Each loop is used to execute a statement or a set of statements for each element in an array or collection. Syntax: For Each element In group [ statements ] [ Exit For ] [ statements ] Next [ element ] The For...Each...Next statement syntax has the following three parts: PartDescriptionelement
    3 min read
  • How to find length of data frame in R
    In this article, we will see What is a Data Frame and how to find the length of a data frame in R programming Language. Return the length (total number of rows) of the Data Frame using nrow()nrow() function is used to return the number of rows of the specified object (Matrix/DataFrame etc). we will
    3 min read
  • How to Get a List of User Defined Functions in Excel VBA?
    Microsoft provides many built-in functions to speed up your work in Excel. However, you can technically create functions using VBA coding called "user-defined functions" (UDFs). Also known as a "user-defined function" in Excel VBA. A formula that can be accessed from a worksheet using a piece of cod
    9 min read
  • How to Delete a Module in Excel VBA?
    Have you ever seen a text file in a notepad or a source code file in visual studio code? These all are just basic files where you can write some code or information. Similarly, we have modules in excel, each module has its code window, where you can write some code and allow it to automate your repe
    3 min read
  • How to Declare and Initialize String Array in Excel VBA?
    A string array is an array where we can store only string values in the array, with the help of a string array, we can store more than one string value. We can declare the string array in many ways like declaring a static string array, declaring a variant size array of string using the Array functio
    2 min read
  • How to Convert VBA Collections to Array in Excel?
    An object that can store a number of values whether it can be of string data type or integer which can be easily manipulated or iterated is called Collection. On the other hand, Array is also used to store the data but it is multidimensional but collections are single dimensions. Now, we will see ho
    1 min read
  • How to Run Code from a Module in Excel VBA
    VBA Macro is for developers. In Excel, the macro is a piece of code written in VBA and VBA is Microsoft's programming language, it stands for Visual Basic for Applications. The module is a file with a .bcf extension that stores the code written in the Visual Basic for Applications editor. Let's lear
    4 min read
  • How to Insert and Run VBA Code in Excel?
    In Excel VBA stands for (Visual Basic for Application Code) where we can automate our task with help of codes and codes that will manipulate(like inserting, creating, or deleting a row, column, or graph) the data in a worksheet or workbook. With the help of VBA, we can also automate the task in exce
    2 min read
  • How to Use For Next Loop in Excel VBA?
    If you are familiar with the programming you must have an idea of what a loop is, In computer programming, a loop is a sequence of statements that are repeated until a specific condition is satisfied. In Excel VBA the "For Next" loop is used to go through a block of code a specific number of times.
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences