增加值变量数组VBA [英] adding values to variable array VBA

查看:291
本文介绍了增加值变量数组VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过具有客户试图循环和金额。如果我的循环找到一个客户名为格雷格或亨利我想增加他的金额一个未知大小的数组。

I am trying to loop through a table that has a column for "customers" and "dollar amount". If my loop finds a customer called "greg" or "henry" I want to add his "dollar amount" to an array of an unknown size.

有人能帮帮我吗?

推荐答案

如果按大小未知,你的意思的元素,这个数字是未知的,你可以使用一个动态数组。

If by unknown size, you mean that number of elements is unknown, you could use a dynamic array.

Dim aArray() As Single ' or whatever data type you wish to use
ReDim aArray(1 To 1) As Single
If strFirstName = "henry" Then
    aArray(UBound(aArray)) = 123.45
    ReDim Preserve aArray(1 To UBound(aArray) + 1) As Single
End If

如果该数组尚未尺寸UBOUND(aArray)抛出一个错误,所以我们通过添加元素它启动。这使得我们有必要在文本的末尾空元素,所以你的code应该考虑这一点。 aArray(UBOUND(aArray)-1)会给你的最后一个有效的元素数组中的

Ubound(aArray) throws an error if the array hasn't been dimensioned, so we start by adding an element to it. That leaves us with an empty element at the end of the text, so your code should account for that. aArray(Ubound(aArray)-1) will give you the last valid element in the array.

这篇关于增加值变量数组VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆