Finance

Charts

Statistics

Macros

Search

One-Dimensional Arrays In Excel VBA

The following procedure stores seven temperature measurement values in a one-dimensional static array of type Integer. For better visualization, the values are then output into worksheet cells:

Sub OneDimensionalArray()
    Dim T(1 To 7) As Integer
    Dim i As Integer
    ThisWorkbook.Worksheets("Sheet1").Activate
    Randomize
    For i = 1 To 7
        T(i) = Rnd * 10 + 20     ' Generates values between 20 and 30
        Cells(i, 1).Value = T(i)
    Next i
End Sub

Explanation:

  • Values are generated by a random number generator, initialized with Randomize.
  • The statement Dim T(1 To 7) As Integer declares a one-dimensional array with seven elements.
  • Each element acts like an individual Integer variable.
  • Arrays can be declared for any known data type.
  • Each element is distinguished by an index, which runs from 1 to 7 in this case.
  • Elements are accessed via the syntax T(i) inside a For loop where i is the loop variable.
  • The array elements are displayed vertically in cells.

Important Notes:

  • Using Dim T(7) As Integer would declare an array with eight elements indexed from 0 to 7 by default.
  • To start the array index at 1 by default, you can include Option Base 1 at the top of the module.
  • Then Dim T(7) As Integer declares an array with seven elements indexed 1 through 7.
0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx