我正在编写一个小的Excel宏,我有一个名为EventClass的类。
我遍历工作表中的每一行,并在EventClass对象的Collection中将每一行添加为EventClass对象 - 该行中的每个单元格都是该类的属性。
以下是代码:
Dim iRow As Integer
For iRow = 2 To oSourceWorksheet.UsedRange.Rows.Count
Set evEvent = New EventClass
evEvent.OwnerConsolidated = oSourceWorksheet.Rows(iRow, 1)
evEvent.OwnerUser = oSourceWorksheet.Rows(iRow, 2)
evEvent.ProductsAF = oSourceWorksheet.Rows(iRow, 3)
evEvent.ProductsGL = oSourceWorksheet.Rows(iRow, 4)
evEvent.ProductsMZ = oSourceWorksheet.Rows(iRow, 5)
evEvent.Quarter = oSourceWorksheet.Rows(iRow, 6)
evEvent.Attendees = oSourceWorksheet.Rows(iRow, 7)
evEvent.CampaignName = oSourceWorksheet.Rows(iRow, 8)
colEvents.Add evEvent
Next iRow
第一个属性集上抛出了错误,我不明白为什么。
如果您需要查看任何其他代码或任何内容,请告诉我们。
谢谢你们!
答案 0 :(得分:3)
尝试使用带有两个参数的范围对象的.Rows
属性时出现此错误,即使intellisense告诉我您可以提供两个参数:
尝试这样做:
With oSourceWorksheet.UsedRange
evEvent.OwnerConsolidated = .Cells(iRow, 1)
evEvent.OwnerUser = .Cells(iRow, 2)
evEvent.ProductsAF = .Cells(iRow, 3)
evEvent.ProductsGL = .Cells(iRow, 4)
evEvent.ProductsMZ = .Cells(iRow, 5)
evEvent.Quarter = .Cells(iRow, 6)
evEvent.Attendees = .Cells(iRow, 7)
evEvent.CampaignName = .Cells(iRow, 8)
End With