VBA array function

In VBA, you can create a collection of values or array by using Array function. To create an array by using array function, you need to write its elements in parentheses and separate each element by comma. The lowest index of the array is 0 and the highest index is equal to the number of its elements minus one.
You can check the lowest index and highest index of the array by using LBound(array) and UBound(array) functions.

Example:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim mons As Variant
Dim i As Integer
mons = Array("January", "Frebruary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
For i = LBound(mons) To UBound(mons)
   Cells(i + 1, 1) = mons(i) 'dispaly months of the year in the current worksheet
   If Month(Now) = i + 1 Then
     Cells(i + 1, 1).Interior.Color = RGB(16, 100, 200) 'highlight the current month
   End If
  
Next

End Sub




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.