這是一個VBA腳本。我不確定爲什麼我的收藏沒有填充「按市場」表格。爲什麼我的收藏空白?
Sub ArrayPractice()
Dim r As Integer
Dim i As Integer
Dim a As Integer
Dim numberOfRows As Integer
Dim names() As String
Dim resourceCollect As Collection
Dim Emp As Resource
Dim Count As Long
Set resourceCollect = New Collection
a = Worksheets("DATA").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
r = 2 'row that i start looping from
i = 0
For Each Emp In resourceCollect
For Count = 0 To a
Emp.Name = Cells(r, 1).Value
Emp.Title = Cells(r, 2).Value
Emp.City = Cells(r, 3).Value
resourceCollect.Add Emp
r = r + 1
Next Count
Next Emp
''''print the array!''''
Sheets.Add.Name = "By Market"
Sheets.Add.Name = "By Resource Level"
Sheets.Add.Name = "By Resource Manager"
Sheets("By Market").Select
Range("C36").Select
r = 36
For Each Emp In resourceCollect
If Emp.City = "Dallas" Then
Cells(r, 3).Select
Debug.Print Emp.Name
r = r - 1
End If
Next Emp
Range("D36:D36").Select
r = 36
For Each Emp In resourceCollect
If Emp.City = "Denver" Then
Cells(r, 4).Select
Debug.Print Emp.Name
r = r - 1
End If
Next Emp
Range("E36:E36").Select
r = 36
For Each Emp In resourceCollect
If Emp.City = "Houston" Then
Cells(r, 5).Select
Debug.Print Emp.Name
r = r - 1
End If
Next Emp
Range("F36:F36").Select
r = 36
For Each Emp In resourceCollect
If Emp.City = "Kansas City (Missouri)" Then
Cells(r, 6).Select
Debug.Print Emp.Name
r = r - 1
End If
Next Emp
End Sub
UPDATE
每約瑟夫的答案,這裏就是我試過。我還沒有工作。
這裏有幾個不同的我一直在搞的Subs。他們都試圖完成同樣的問題。
Sub stackResources()
Dim c As New Collection
Dim r1 As Excel.Range 'an object
Dim r2 As Excel.Range
Dim r3 As Excel.Range
Set r1 = Range("A1")
Set r2 = Range("B1")
Set r3 = Range("C1")
c.Add r1
c.Add r2
c.Add r3
Sheets("By Market").Select
Range("A1").Select
Dim i As Long
For i = 1 To c.Count
Debug.Print c.Item(i)
Next
End Sub
Sub collectionTest()
Dim c As New Collection
Dim emp As Resource
Sheets("DATA").Select
Range("A1").Select
Do Until Selection.Value = ""
emp.name = Selection.Value
ActiveCell.Offset(0, 1).Select
emp.Title = Selection.Value
ActiveCell.Offset(0, 1).Select
emp.city = Selection.Value
c.Add emp
Loop
Sheets("By Market").Select
Range("A1").Select
Dim i As Long
For i = 1 To c.Count
Debug.Print c.Item(i)
Next
End Sub
Sub printACollection()
Dim c As New Collection
Dim s1 As String
Dim s2 As String
Dim s3 As String
Sheets("DATA").Select
Dim r As Long
r = 1
For Each cell In Range("A1")
s1 = cell.Value
c.Add s1
ActiveCell.Offset(0, 1).Select
s2 = cell.Value
c.Add s2
ActiveCell.Offset(0, 1).Select
s3 = cell.Value
c.Add s3
Next
Sheets("By Market").Select
Dim i As Long
For i = 1 To c.Count
Debug.Print c.Item(i)
Next
End Sub
您無法遍歷空集合。您首先必須添加項目... –