我已经为CorelDraw编写了一个VBA宏,该宏循环遍历所选对象并以字符串形式返回值。我希望它能够更好地处理重复数据,例如,如果两个对象的大小相同,则应返回
"2 of 10 x 10"
代替
"1 of 10 x 10"
"1 of 10 x 10"
来自Ruby(专门考虑哈希),我认为宏应该遍历所选范围,将(object.sizeWidth,object.sizeHeight)数据作为字符串添加到应检查数组/字典中重复并计数。我不知道什么是最好的,或者如何设置/检查它们的值。
到目前为止,这是我的代码
Sub objectsToString()
Dim str As String
Dim v As Shape, vr As ShapeRange
Dim xSize#, ySize#
Dim dupCount As Integer
str = ""
Set vr = ActiveSelectionRange
For Each v In vr
dupCount = 'value assigned via iteration
xSize = v.SizeWidth
ySize = v.SizeHeight
str = str & dupCount & " of " & xSize & " x " & ySize & vbNewLine
Next v
End Sub
答案 0 :(得分:1)
例如,使用使用形状名称作为键的scripting.dictionary,并使用它的.exists方法,然后将该项目作为计数,并使用具有相同键名的单独词典,然后使用类/类型/数组/尺寸在的馆藏/词典。
这样的事情将是一个好的开始。
Sub x()
Dim a() As Variant
Dim d As Scripting.Dictionary
Dim v As Variant
a = Array("10 x 10", "20 x 20", "30 x 30", "10 x 10")
Set d = New Scripting.Dictionary
For Each v In a
If Not d.Exists(v) Then
d.Add v, 1
Else
d(v) = d(v) + 1
End If
Next v
For Each v In d.Keys
Debug.Print d(v) & " of " & v
Next v
End Sub
给出结果
2 x 10 x 10
1 x 20 x 20
1 x 30 x 30