2014-04-01 34 views
0

對不起,我以前的消息不夠清楚!識別不同的動態數組VBA Excel

這是情況 用戶可以在Excel數組中添加新行。 我想然後在宏的最後一行中存儲新的參數,以便進行其他計算。

例如:我有2列的陣列:參數和值 參數< - B1柱 參數1 參數2 參數3

價值< - C1柱 VAL1 VAL2 VAL3

此後我做了什麼,但它不起作用!

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:路徑查找器( 「worksheet1」, 「字詞1」)發送ARR(2)用細胞細節-column &行級別的 「字詞1」 中的 「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 

由於

發現
+1

我真的不能說,我明白你的問題完全,但似乎你可能能夠使用動態r anges(http://support.microsoft.com/kb/830287)和worksheet_change事件來完成您要做的事情......否則,請更新您的問題並更好地解釋您的情況... –

回答

0

嗯,如果我理解正確的話,這聽起來像你想這樣做:

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

ReDim將調整數組大小。該Preserve保持與已是陣列(否則會被重新初始化中的值

0

不知道我完全理解,但這裏是我的意見: 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 
+0

I剛剛添加了有關路徑查找器功能的詳細信息... – ThinkMAL