识别不同的动态数组 VBA Excel

Posted

技术标签:

【中文标题】识别不同的动态数组 VBA Excel【英文标题】:Identifying different dynamic array VBA Excel 【发布时间】:2014-04-01 15:19:17 【问题描述】:

抱歉,我之前的留言不够清楚!

情况如下 用户可以在 Excel 数组中添加新行。 然后我想在宏的动态数组的最后一行存储新参数,以便进行其他计算。

例如:我有一个包含 2 列的数组:参数和值 参数

此后我做了什么,但它不起作用!

Dim RowCount As Integer
RowNumber = Sheets("Feuil1").Range("C1").End(xlDown).row
'MsgBox "Rows:" & RowNumber-1

Dim tb_val() As Integer
ReDim tb_val(RowNumber - 1)
Dim lc() As Integer

For i = 1 To RowNumber
    lc = PathFinder("Feuil1", Cells(i, 2).Value)
    tb_val(i - 1) = Sheets("Feuil1").Cells(lc(1), lc(2) + 1).Value 
Next i

P.S: PathFinder("worksheet1", "word1") 发送带有单元格详细信息的 arr(2) -column & row- of "word1" 在 "worksheet1" 中找到

Function PathFinder(sheet1 As String, word1 As String) As Integer()
    Dim rng As Range
    Dim rngFound As Range
    Dim temp(2) As Integer

    Set rng = Sheets(sheet1).Range("A:B")
    Set rngFound = rng.Find(word1, LookAt:=xlWhole, SearchOrder:=xlByRows)

    If rngFound Is Nothing Then
        MsgBox "not found"
    Else:
        temp(1) = rngFound.row
        temp(2) = rngFound.column
    End If
    PathFinder = temp
End Function

谢谢

【问题讨论】:

我真的不能说我完全理解您的问题,但您似乎可以使用动态范围 (support.microsoft.com/kb/830287) 和 worksheet_change 事件来完成您的任务想做...否则,请更新您的问题并更好地解释您的情况... 【参考方案1】:

好吧,如果我理解正确的话,听起来你想这样做:

ReDim Preserve arr(2)
arr(2) = val2

ReDim 将调整数组的大小。 Preserve 保留数组中已经存在的值(否则它们将被重新初始化。

【讨论】:

【参考方案2】:

不确定我是否完全理解,但这是我的看法: RowNumber 是(我认为)错误的;使用“与”;第二个例子展示了如何制作一个非常快的数组。

Dim RowCount As Long 'Long is faster , and you never know how many lines you need
Dim Sh as worksheet
set Sh = thisworkbook.Sheets("Feuil1")

with Sh
    RowNumber = .Range(.rows.count,"C").End(xlUp).Row  ' "C" can be replaced by 3, same effect.
    'MsgBox "Rows:" & RowNumber
end with

Dim tb_val() As Long
ReDim tb_val( 1 to RowNumber)
Dim lc() As Long

For i = 1 To RowNumber
    lc(i) = PathFinder("Feuil1", Cells(i, 2).Value) 'no idea what this does ???
    tb_val(i) = Sh.Cells(lc(1), lc(2) + 1).Value 'must be mistacke, no (i) in lc() ??
Next i

好吧,我没看懂你的所有代码,还是考虑一下吧:

Dim MyArray() as Variant 'must be variant for this effect
Redim MyArray (1 to RowNumber , 1 to 3) 'if only 3 columns, Rownumber is the same as above.
with sh 'same Sh as in example above
    MyArray= .Range ( .cells( 1,1 ) , .cells ( rownumber,3 ) ).value 'stocks all the cells (in 3 columns in example in a VBA Array for faster calculs later
    'this way any loop or calculation is way faster, using Myarray(y,x) instead of Sh.cells(y,x).value
end with

'Long Loop + calculs... for i=1 to Rownumber ....

Sh.range( "A1:C" & RowNumber).Value = MyArray 'Writes Back the VBA Array to the Worksheet (only needed if changes made inside the values of the array)

'range ("A1:C" & RowNumber)  has the same effect as Range(cells(1,1) , cells(rownumber,3))    from above , just wanted to say

【讨论】:

以上是关于识别不同的动态数组 VBA Excel的主要内容,如果未能解决你的问题,请参考以下文章

Excel VBA数组使用方法

vba 代码中有二维动态数组S,第1次重定义为50行,但由于只用到j行

如何在vba动态数组不清空维数前提下快速将所有元素赋值为0, erase会把动态数组维数清空

使用VBA根据逻辑动态循环数组元素

如何实现动态数据验证,例如作为 Excel VBA 函数?

使用 Excel VBA 检查字符串是不是在数组中或不包括通配符