Vbscript 一个dictionary对象可以在同一个键下有多个项吗?
我正在寻找与字典对象项相关的解决方法Vbscript 一个dictionary对象可以在同一个键下有多个项吗?,vbscript,Vbscript,我正在寻找与字典对象项相关的解决方法 Dim a, d 'Create some variables Set d = CreateObject("Scripting.Dictionary") d.Add "a", "Athens" 'is possible I know d.Add "a", "Athens","India","Paris" ' Is this Possible under same Key? 描述快照:(已更新) 然后如何使用dictionary对象实现上表?
Dim a, d 'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens" 'is possible I know
d.Add "a", "Athens","India","Paris" ' Is this Possible under same Key?
描述快照:(已更新)
然后如何使用dictionary对象实现上表?给我一些想法
谢谢,否。根据定义,字典数据类型使用的键必须是唯一的。我知道在大多数实现中都是这样的,但对于VBscript的
脚本编写,我能找到的最接近权威的参考资料是:
键是唯一的条目:单个Dictionary对象中的两个键不能相同
编辑:使变量名更易于操作
编辑:包括OP的脚本。字典参考
编辑:将OP问题中的值读入字典并检索值
Sub t()
Dim d As Object
Dim values
Dim height As Long
Dim item
Dim width As Long
Dim i As Long, j As Long
Dim MANGERID
Dim EMPIDs
Dim EMPID
With ActiveSheet
height = .Cells(.Rows.Count, 1).End(xlUp).Row
If height < 2 Then
Exit Sub
End If
Set d = CreateObject("Scripting.Dictionary")
For i = 2 To height
width = .Cells(i, .Columns.Count).End(xlToLeft).Column
if width > 1 then 'make sure have EMPID
ReDim values(1 To width - 1)
Key = .Cells(i, 1).Value
For j = 2 To width
values(j - 1) = .Cells(i, j).Value
Next j
d.Add Key, values
end if
Next i
'displaying back the items in the dictionary
For Each MANGERID In d.keys
'value array
EMPIDs = d.item(MANGERID)
If TypeName(EMPIDs) = "Variant()" Then
For Each EMPID In EMPIDs
Debug.Print MANGERID & ":" & EMPID
Next EMPID
End If
Next MANGERID
Set d = Nothing
End With
End Sub
subt()
将d作为对象
尺寸值
低矮如长
暗项
宽度与长度相同
我和我一样长,我和我一样长
暗马格里德
暗EMPIDs
暗EMPID
使用ActiveSheet
高度=.Cells(.Rows.Count,1).End(xlUp).Row
如果高度<2,则
出口接头
如果结束
Set d=CreateObject(“Scripting.Dictionary”)
对于i=2至高度
宽度=.Cells(i、.Columns.Count).End(xlToLeft).Column
如果宽度>1,则“确保有EMPID
ReDim值(1到宽度-1)
键=.Cells(i,1).Value
对于j=2至宽度
值(j-1)=.单元格(i,j).值
下一个j
d、 添加键、值
如果结束
接下来我
'显示回字典中的项目
对于d键中的每个MANGERID
'值数组
EMPIDs=d.item(MANGERID)
如果TypeName(EMPIDs)=“Variant()”则
对于EMPID中的每个EMPID
Debug.Print管理器ID&“:”&EMPID
下一个EMPID
如果结束
下一个马格里德
设置d=无
以
端接头
这是另一个例子,供任何其他看到这一点并需要另一种解决方案的人使用。我觉得这有帮助
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
dicKey = 69
dicValues = ""
dicVal = "val1"
dicVal2 = "val2"
dicVal3 = "val3"
objDictionary.add dicKey, DicValues1 & DicValues2
chang = -1
if chang = -1 then
objDictionary.item(dicKey) = dicVal & "," & dicVal2
chang = -69
end if
if chang = -69 then
objDictionary.item(dicKey) = dicVal3 & ", " & dicVal & ", " & dicVal2
chang = -1
end if
for each objDictionary_key in objDictionary.keys
msgbox "Key: " & objDictionary_Key & " Value: " & objDictionary.item(objDictionary_Key)
next
我希望这对你有所帮助 如果您试图在一个键中添加多个项,它们将是键的重复,Vb脚本不允许这样做。
要了解有关dictionary对象的更多信息,请参阅以下链接。
我在寻找同一个键是否可以包含多个值?我想在这种情况下,您应该有一个数组字典。我更新了主题描述,您可以查看相同的吗?还有建议。你能为我更新的描述编写相同的代码吗?这本质上是动态的?你是说你的“描述快照:”部分是输入excel部分,你想将它们存储在字典中吗?@TukaiRakshit一篇关于字典的非常好的文章你想一次一个经理ID还是一个EMPID?在我的代码中,d.keys中每个键的都是循环管理器ID值是存储EMPID的数组元素,您可以将值作为数组进行循环。对于这类问题,您应该学会隔离问题。。写一个vbs仅包含固定a到b的lune。。
Sub t()
Dim d As Object
Dim values
Dim height As Long
Dim item
Dim width As Long
Dim i As Long, j As Long
Dim MANGERID
Dim EMPIDs
Dim EMPID
With ActiveSheet
height = .Cells(.Rows.Count, 1).End(xlUp).Row
If height < 2 Then
Exit Sub
End If
Set d = CreateObject("Scripting.Dictionary")
For i = 2 To height
width = .Cells(i, .Columns.Count).End(xlToLeft).Column
if width > 1 then 'make sure have EMPID
ReDim values(1 To width - 1)
Key = .Cells(i, 1).Value
For j = 2 To width
values(j - 1) = .Cells(i, j).Value
Next j
d.Add Key, values
end if
Next i
'displaying back the items in the dictionary
For Each MANGERID In d.keys
'value array
EMPIDs = d.item(MANGERID)
If TypeName(EMPIDs) = "Variant()" Then
For Each EMPID In EMPIDs
Debug.Print MANGERID & ":" & EMPID
Next EMPID
End If
Next MANGERID
Set d = Nothing
End With
End Sub
Dim objDictionary
Set objDictionary = CreateObject("Scripting.Dictionary")
dicKey = 69
dicValues = ""
dicVal = "val1"
dicVal2 = "val2"
dicVal3 = "val3"
objDictionary.add dicKey, DicValues1 & DicValues2
chang = -1
if chang = -1 then
objDictionary.item(dicKey) = dicVal & "," & dicVal2
chang = -69
end if
if chang = -69 then
objDictionary.item(dicKey) = dicVal3 & ", " & dicVal & ", " & dicVal2
chang = -1
end if
for each objDictionary_key in objDictionary.keys
msgbox "Key: " & objDictionary_Key & " Value: " & objDictionary.item(objDictionary_Key)
next