简体   繁体   English

识别不同的动态数组VBA Excel

[英]Identifying different dynamic array VBA Excel

Sorry for not been enough clear in my previous message! 很抱歉,我之前的消息不够清楚!

Here is the situation A user have the possibility to add new row in an Excel Array. 在这种情况下,用户可以在Excel数组中添加新行。 I would like then to stock the new parameter in the last line of a dynamic array on macro in order to make other calculations. 然后,我想将新参数存储在宏的动态数组的最后一行中,以便进行其他计算。

For example: I have an Array of 2 columns: Parameter and Value Parameter <-- B1 column Param1 Param2 Param3 例如:我有2列的数组:参数和值参数<-B1列Param1 Param2 Param3

Value <-- C1 column Val1 Val2 Val3 值<-C1列Val1 Val2 Val3

Hereafter what I have done but it doesn't work! 此后,我做了什么,但是没有用!

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

PS: PathFinder("worksheet1", "word1") send arr(2) with cells details -column & row- of "word1" found in "worksheet1" PS:PathFinder(“ worksheet1”,“ word1”)发送arr(2),其中包含在“ worksheet1”中找到的“ word1”的单元格详细信息-列和行-

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

Thanks 谢谢

Well, if I understand correctly, it sounds like you want to do this: 好吧,如果我理解正确,听起来您想这样做:

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

The ReDim will resize the array. ReDim将调整数组的大小。 The Preserve keeps the values that are already in the array (otherwise they will be re-initialized. Preserve保留数组中已经存在的值(否则它们将被重新初始化。

Not sure i fully understand, but here's my opinion : RowNumber is (i think) wrong ; 不确定我是否完全理解,但是我的观点是:RowNumber(我认为)是错误的; use 'With' ; 使用'With'; the second example shows how to make a very fast array. 第二个示例显示了如何制作一个非常快速的数组。

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

Ok, i don't understand all your code, still consider this: 好的,我不理解您的所有代码,仍然要考虑以下问题:

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

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

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