【发布时间】:2017-08-28 10:54:50
【问题描述】:
我想创建一个汽车制造商列表及其型号。 为此,我使用字典,其中键是品牌,项目是模型的集合。例如: 字典中的key是“Volkswagen”,集合包含polo、cc、passat等…… 该代码从工作表中读取项目。问题是我不确定集合是否加载了模型类型。此外,我检查了调试选项,如何从字典中写出集合元素,但我得到了空消息。如果有人可以帮助我修复此代码,我将非常高兴。
Sub collectModels()
Dim imp_wb As Workbook, new_wb As Workbook
Dim ws_imp As Worksheet, ws_new As Worksheet, ws_stnd As Worksheet, ws_model_list As Worksheet
Dim lastRow As Long, lastCol As Long
Dim rng As Range
Dim validate As String, model_key As String, model_item As String
Dim modelCollection As Collection
Set imp_wb = ThisWorkbook
Set ws_model_list = imp_wb.Sheets("MODEL_LIST")
'Set new_wb = Workbooks.Add
'Set ws_new = new_wb.Worksheets(1)
Set rng = ws_stnd.Range("A2:A68")
'ws_imp.Activate
ws_model_list.Activate
lastRow = Last(1)
lastCol = Last(2)
Set dict_ModelMapping = CreateObject("scripting.dictionary")
Set modelCollection = New Collection
For i = 1 To lastCol
model_key = ws_model_list.Cells(1, i).Value
For j = 2 To lastRow
'add items to collection
model_item = ws_model_list.Cells(j, i).Value
If Not model_item = "" Then
modelCollection.Add model_item
Else
'add collection to dictionary
dict_ModelMapping.Add model_key, modelCollection
Set modelCollection = New Collection
GoTo nextColumn
End If
Next j
nextColumn:
'DEBUG CODE
For Each v In dict_ModelMapping.Key("SUZUKI")
Debug.Print v
Next v
Next i
'--- CHECK COLLECTIONS---
Dim tmpCollection As Collection
Dim showItem As String
For Each Key In dict_ModelMapping.Keys
MsgBox ("--------------" & Key & "---------------")
Next
End Sub
【问题讨论】:
-
什么是
Last? -
@SJR the Last 是一个函数,它返回工作表中 Last(1) 最后一行、Last(2) 最后一列、Last(3) 最后使用的单元格。这是由 ron de burin 创建的。
标签: vba excel dictionary collections