這使我變得瘋狂。我試圖從用戶選擇的行中提取符合我的條件(TRUE或1)的列的值,僅將這些值保存到數組中,然後將數組寫入一個範圍。就這樣。誰能告訴我我做錯了什麼?Excel VBA數組
Sub DevNeeds()
Dim x(), y(), needs() As Variant
Dim counter As Integer
columns_in_range = Range("dev_needs_hdrs").Columns.Count
counter = 1
Debug.Print "i", "counter", "y(counter)"
For i = 1 To columns_in_range
ReDim x(columns_in_range), needs(columns_in_range)
x(i) = Application.Index(Range("dev_needs"), Range("selected_row").Value, i)
needs(i) = Application.Index(Range("dev_needs_hdrs"), 1, i)
If (x(i) = True Or x(i) = 1) Then
ReDim y(counter)
y(counter) = needs(i)
counter = counter + 1
End If
Next i
counter = counter - 1
With Range("selected_rep_needs")
.ClearContents
.Resize(1, counter) = y
End With
End Sub
什麼行接收錯誤? – 2014-09-02 19:42:42
其實,我沒有收到錯誤。我只是得到意想不到的結果,即範圍「selected_rep_needs」沒有得到任何寫入的結果。另外,名爲「y」的數組除了最後一個下標(最後一個值爲「counter」)以外不包含任何值 – SteveS 2014-09-02 19:56:39
我不假設'Application.Index'中的範圍名稱參數將值賦給'x(我)'應該是「dev_needs_hdrs」,而不僅僅是「dev_needs」(?)。 – 2014-09-02 20:04:40