只是关于VBA的一个简短问题。我的代码VBA阵列等于范围
Dim colEmployees As New Collection
Dim recEmployee As New clsEmployee
Dim LastRow As Integer, myCount As Integer
Dim EmpArray As Variant
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
EmpArray = ActiveSheet.Range(Cells(1, 1), Cells(LastRow, 4))
该块,这在电子表格上...
现在我已经做过的阅读掌握如何阵列的工作很多,我已经看到exaples像
Dim myArray As Variant
Dim myArray (1 to 10, 1 to 20)
myArray = Array(“Name”, “Address”, “Phone”, “Email”)
和我完全理解他们,但是当你有一个数组等于这样
EmpArray = ActiveSheet.Range(Cells(1, 1), Cells(LastRow, 4))
如何将数据实际保存在EmpArray等一维数组中?你不需要一维的行和一列的列?我的意思是数组将如何实际存储数据 - 按什么顺序(“Tracy”,“Bill”,“1651”,“1509”,....)或(“Tracy”,“1651”,“25” “45”)?通常将数组中的范围存储在数组中对于我来说看起来很奇怪,尤其是一维数组。和我的VBA书3没有深入到更深的这一点......
如果它是一个二维数组是怎么回事更多钞票,代码继续,如:
Sub EmpPayCollection()
Dim colEmployees As New Collection
Dim recEmployee As New clsEmployee
Dim LastRow As Integer, myCount As Integer
Dim EmpArray As Variant
LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
EmpArray = ActiveSheet.Range(Cells(1, 1), Cells(LastRow, 4))
For myCount = 1 To **UBound(EmpArray)**
Set recEmployee = New clsEmployee
With recEmployee
.EmpName = EmpArray(myCount, 1)
.EmpID = EmpArray(myCount, 2)
.EmpRate = EmpArray(myCount, 3)
.EmpWeeklyHrs = EmpArray(myCount, 4)
colEmployees.Add recEmployee, .EmpID
End With
Next myCount
MsgBox “Number of Employees: “ & colEmployees.Count & Chr(10) & _
“Employee(2) Name: “ & colEmployees(2).EmpName
MsgBox “Tracy’s Weekly Pay: $” & colEmployees(“1651”).EmpWeeklyPay
Set recEmployee = Nothing
End Sub
为什么它使用UBound时未明确说明所选维度?我知道这是可选的......但是你能否将这个因素考虑在内?
先生你是说什么意思... 2维1基于...? – ExoticBirdsMerchant
我的意思是与从1开始阵列的下限为2维阵列,而不是0 – SWa
爵士,对不起dealying该长我几乎拿到它,我只是做了一个小的调整和重新编辑一点点发布,以便我可以让我的问题得到全面解决。 – ExoticBirdsMerchant