標題: [發問] Dim arr1(1 To 100, 1 To 2),arr = [a2:d100],其中100,如何用變數取代 [打印本頁] 作者: dou10801 時間: 2021-2-19 15:08 標題: Dim arr1(1 To 100, 1 To 2),arr = [a2:d100],其中100,如何用變數取代
請教各位前輩,Dim arr1(1 To 100, 1 To 2),arr = [a2:d100],其中100,要如何用變數取代,謝謝作者: acdx 時間: 2021-2-19 16:25
Sub 按鈕2_Click()
Dim i&, j&, k%
Dim arr As Variant
Dim MAS As Variant
MAS = Cells(Rows.Count, 1).End(xlUp).Row
'Dim arr1(1 To MAS, 1 To 2)
Dim arr1(1 To 100, 1 To 2)
arr = Range(Cells(2, 1), Cells(2, MAS))
'arr = ["A2:D" & MAS ]
For i = 1 To UBound(arr)
If arr(i, 1) <> "" Then
If arr(i, 1) = Cells(3, "e") And arr(i, 2) = Cells(3, "f") Then
k = k + 1
arr1(k, 1) = arr(i, 3)
arr1(k, 2) = arr(i, 4)
End If
End If
Next i
Cells(3, "h").Resize(UBound(arr1), 2) = arr1
End Sub作者: 軒云熊 時間: 2021-2-20 04:22
感謝兩位前輩,我的意思是Dim arr1(1 To 100, 1 To 2)中的100如何用變數活用,因為
MAS = Cells(Rows.Count, 1).End(xlUp).Row
Dim arr1(1 To MAS, 1 To 2)
會錯誤,[必需是常數運算式]作者: quickfixer 時間: 2021-2-22 08:49
Dim arr1()
mas = 999
ReDim arr1(1 To mas, 1 To 2)
Debug.Print UBound(arr1, 1), UBound(arr1, 2)作者: dou10801 時間: 2021-2-22 11:45
剛學VBA有很多要學習,感謝各位指導,經調整後以下是我要的語法,謝謝大家.
Dim i&, j&, k%
Dim arr As Variant
Dim MAS As Variant
MAS = Cells(Rows.Count, 1).End(xlUp).Row
'Dim arr1(1 To 100, 1 To 2) '原稿
ReDim arr1(1 To MAS, 1 To 2) '調整後的語法
'arr = [a2:d100] '原稿
'arr = ["a2:d" & MAS ]
arr = Range(Cells(2, 1), Cells(MAS, 4).Address) '調整後的語法
For i = 1 To UBound(arr)
If arr(i, 1) <> "" Then
If arr(i, 1) = Cells(3, "e") And arr(i, 2) = Cells(3, "f") Then
k = k + 1
arr1(k, 1) = arr(i, 3)
arr1(k, 2) = arr(i, 4)
End If
End If
Next i
Cells(3, "h").Resize(UBound(arr1), 2) = arr1