这是一个 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
更新
根据约瑟夫的回答,这就是我尝试过的。我还没有让它工作。
这是我一直在搞砸的几个不同的潜艇。他们都试图解决同样的问题。
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