简体   繁体   English

VBA Excel-变量“ Double”作为功能

[英]VBA Excel - Variable “Double” as function

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) 我获得了正在运行的VBA函数,但我不了解它的工作原理,不胜感激,因为在编写它的过程中,微积分确实很长(基本算术* 15000行)

My problem comes from the definition of the Double variable Qty_Level which is defined as follow: 我的问题来自Double变量Qty_Level的定义,其定义如下:

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) 我不明白这是如何工作的,尤其是For循环如何与Double(j)

When you define an array with parentheses and a value inside them, it creates an array with that many elements contained within it. 当您定义一个带有括号并在其中包含一个值的数组时,它将创建一个数组,其中包含许多元素。 Dimensioning it with 30 as the max element value means that for a variable between 0 and 30 (since the array will start with 0 by default) you can store up to 31 values inside of the array. 使用最大元素值30来标注尺寸意味着对于0到30之间的变量(因为数组默认情况下将从0开始),您最多可以在数组内部存储31个值。 Dimensioning it as a double (according to MSDN) allows you to store the largest and smallest numbers available in Visual Basic. 将其尺寸设置为双精度(根据MSDN)允许您存储Visual Basic中可用的最大和最小数字。

What the loop appears to do is pull the quantity value from the quantity column based off of the number in the levels column, and then multiply each stored quantity value so far, storing it into the results cell. 循环似乎要做的是从“水平”列中的数字中拉出“数量”列中的数量值,然后将每个已存储的数量值相乘至今,将其存储到结果单元格中。

So if your quantities are 2, 4, 6, 5, when you are at level 3 your result cell should show 48, and level 4 should show 240. Looping from 1 to the levels value allows it to iterate through the entire amount of stored quantities in the double array, which just contains numbers. 因此,如果您的数量是2、4、6、5,则在第3级时,结果单元格应显示48,而第4级应显示240。从1到级别值的循环使其可以迭代整个存储量双精度数组中的数量,该数组仅包含数字。 The levels column should only contain numbers between 1 and 30, otherwise it will draw an error from the qty_level array. 级别列只能包含1到30之间的数字,否则它将从qty_level数组中引发错误。

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.) Dim Qty_Level(30) As Double你声明一个称为静态数组Qty_Level包含类型双(双精度浮点数)的31个元素,具有下界0和上限30(除非你写Option Base 1在模块的顶部,在这种情况下,下限是1,总共有30个元素,但我对此表示怀疑。)

I see that in your current loop, you're starting your iteration at j = 1 which means you never actually access element 0 . 我看到在当前循环中,您从j = 1开始迭代,这意味着您实际上从未访问过元素0 So it's better practice to explicitly specify your lower bound: 因此,最好的做法是明确指定您的下限:

Dim QtyLevel(1 To 30) As Double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM