VBA Excel - Variable "Double" as function

Alex C

I was given a VBA function that is working but I do not understand how it works and would appreciate your help because in the way it was written, the calculus is really long (basic arithmetic * 15 000 rows)

My problem comes from the definition of the Double variable Qty_Level which is defined as follow:

Dim Qty_Level(30) As Double

I have never seen a variable defined like this, with a couple of parenthesis like a function. Here's my code:

Sub cumul()

    Dim i As Long
    Dim j As Integer
    Dim Qty_level(30) As Double
    Dim Col_Niveau As Integer
    Dim Col_Quantite As Integer
    Dim Col_Resultat As Integer

    i = InputBox("Veuillez indiquer le numéro de la première à analyser (numéro de     ligne Excel)", "Ligne de départ")
   Col_Niveau = InputBox("Veuillez indiquer le numéro de la colonne contenant les niveaux", "Niveaux")
   Col_Quantite = InputBox("Veuillez indiquer le numéro de la colonne contenant les quantités", "Quantités")
   Col_Resultat = InputBox("Veuillez indiquer le numéro de la colonne contenant les résultats", "Résultats")

   Do While IsEmpty(Cells(i, Col_Niveau)) = False

   If IsNumeric(Cells(i, Col_Quantite)) = True Then

        Qty_level(Cells(i, Col_Niveau).Value) = Cells(i, Col_Quantite).Value
        Cells(i, Col_Resultat).Value = 1

        For j = 1 To Cells(i, Col_Niveau).Value
            Cells(i, Col_Resultat).Value = Cells(i, Col_Resultat).Value * Qty_level(j)
        Next j

    End If

    i = i + 1

Loop


End Sub

I do not understand how this works, and particularly how the For loop works with the Double(j)

Jean-François Corbett

With Dim Qty_Level(30) As Double you're declaring a static array called Qty_Level containing 31 elements of type Double (double-precision floating-point), with lower bound 0 and upper bound 30. (Unless you wrote Option Base 1 at the top of your module, in which case your lower bound is 1 and there are 30 elements total, but I doubt it.)

I see that in your current loop, you're starting your iteration at j = 1 which means you never actually access element 0. So it's better practice to explicitly specify your lower bound:

Dim QtyLevel(1 To 30) As Double

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing a Variable to an Add-in Function in Excel VBA

From Dev

Excel VBA - Find Function - Find a variable

From Dev

Declaring a variable in a public function (Excel VBA)

From Dev

Calling SQL Query with VBA Variable Function from Excel

From Dev

VBA write excel function in a cell containing a defined variable

From Dev

Excel function to VBA

From Dev

Excel not finding VBA Function

From Dev

Excel VBA and Group function

From Dev

Sub or Function in VBA Excel

From Dev

VBA Excel If statement with AND/OR function

From Dev

Find Function in Excel VBA

From Dev

Automate a Excel VBA Function

From Dev

Excel VBA hlookup function

From Dev

Excel VBA and Group function

From Dev

Round function VBA EXCEL

From Dev

How do I match a single double quote (") using excel vba's Like function?

From Dev

MS Excel 2007 VBA function error (Object variable or with block variable not set)

From Dev

Public Static variable in excel vba

From Dev

Excel VBA settings cells as a variable

From Dev

Excel VBA: Dynamic Variable Name

From Dev

Excel VBA incrementing variable in for loop

From Dev

Public Static variable in excel vba

From Dev

VBA Excel Workbooks Object Variable

From Dev

Excel VBA - check if value is type DOUBLE

From Dev

Optimizing performance of double loop in VBA Excel

From Dev

Excel formula containing double quotes through VBA

From Dev

access to excel vba double quotation issue

From Dev

autofilter using double variable criteria vba

From Dev

VBA Excel "Expected Function or variable" error when inserting form control button by macro