通过一个集合的成员循环,我怎样才能得到该成员的关键?

所以我必须将一堆微调对象移动到一组单元格的旁边。

对于每个微调我需要运行这些语句

Worksheets("Serial").spnAspect.Left = Worksheets("Serial").Range("I12").Left - Worksheets("Serial").spnAspect.Width Worksheets("Serial").spnAspect.Top = Worksheets("Serial").Range("I12").Top + Worksheets("Serial").Range("I12").Height / 2 _ - Worksheets("Serial").spnAspect.Height / 2 

所以我想让这个更加整洁,所以我做了以下的小节。 我在这里唯一的问题是如何告诉它哪个单元排队每个微调。 这是每个迭代中的“I12”应该是thisControl的集合中的关键。

 Sub MoveSpinners() Dim myControls As New Collection Dim thisControl As Object Dim mySheet As Worksheet myControls.Add Worksheets("Serial").spnHeight, "I11" myControls.Add Worksheets("Serial").spnAspect, "I12" myControls.Add Worksheets("Serial").spnCropleft, "I13" myControls.Add Worksheets("Serial").spnCropRight, "I14" myControls.Add Worksheets("Serial").spnCropTop, "I15" myControls.Add Worksheets("Serial").spnCropBottom, "I16" Set mySheet = Worksheets("Serial") For Each thisControl In myControls thisControl.Left = mySheet.Range("I12").Left - thisControl.Width thisControl.Top = mySheet.Range("I12").Top + thisControl.Height / 2 _ - thisControl.Height / 2 Next End Sub 

这与获取一个Collection对象上的一个项目的键的问题是一样的,在这种情况下,通过“使用字典对象”来解决这个问题,但是在我的情况下,它不起作用,因为它比刚刚复制粘贴相同的线路很多次

我正在考虑的其他select是一个fMoveSpinner(thisSpinner作为对象,myDestination作为范围),但我希望保持这个小的MoveSpinner子程序独立。

如果你有一个更好的主意,我会很高兴听到它!

我重构了您的代码使用脚本字典。 我看起来很整洁!

脚本字典的键和项都可以是对象。 在这里,我将控件存储为一个键,并将范围存储为该项目。 通过这种方式,当您访问某个按键控件时,您可以轻松获取该项目范围作为参考。

之前和之后

在这里输入图像说明

 Sub MoveSpinners2() Dim myControls As Object Dim thisControl As Object Dim mySheet As Worksheet Dim x As Long Set myControls = CreateObject("Scripting.Dictionary") With Worksheets("Serial") myControls.Add .spnHeight, .Range("I11") myControls.Add .spnAspect, .Range("I12") myControls.Add .spnCropleft, .Range("I13") myControls.Add .spnCropRight, .Range("I14") myControls.Add .spnCropTop, .Range("I15") myControls.Add .spnCropBottom, .Range("I16") End With For Each thisControl In myControls thisControl.Left = myControls(thisControl).Left - thisControl.Width thisControl.Top = myControls(thisControl).Top + thisControl.Height / 2 _ - thisControl.Height / 2 Next End Sub 

下面是一个关于如何迭代集合键的例子,但是,我认为使用字典实际上会更清晰,因为你正在尝试做什么。

无论如何,这里是一个例子:

 Sub Iterate_Keys_Collection() Dim myControls As New Collection Dim thisControl As Variant myControls.Add Array("first key", "I11"), "I11" myControls.Add Array("second key", "I12"), "I12" myControls.Add Array("third key", "I13"), "I13" For Each thisControl In myControls If thisControl(0) = "first key" Then MsgBox (thisControl(0)) ' Print the 'first key' ' The Value is in thisControl(1) End If Next End Sub 

我会用一个Array方法添加我的0.02美分,因此不依赖DictionaryCollection对象:

 Option Explicit Sub MoveSpinners3() Dim iSpn As Long Dim spnArr As Variant Dim shp As Shape spnArr = Array("I11", "spnHeight", "I12", "spnAspect", "I13", "spnCropleft", "I14", "spnCropRight", "I15", "spnCropTop", "I16", "spnCropBottom") With Worksheets("Serial") For iSpn = LBound(spnArr) To UBound(spnArr) Step 2 Set shp = .Shapes(spnArr(iSpn + 1)) shp.Left = .Range(spnArr(iSpn)).Left - shp.Width shp.Top = .Range(spnArr(iSpn)).Top + shp.Height / 2 - shp.Height / 2 Next End With End Sub 

在范围和旋钮的名称之间保持一个(相当)有用的视觉配对,上面的代码可以利用VBA延续字符重写:

 Option Explicit Sub MoveSpinners3() Dim iSpn As Long Dim spnArr As Variant Dim shp As Shape spnArr = Array("I11", "spnHeight", _ "I12", "spnAspect", _ "I13", "spnCropleft", _ "I14", "spnCropRight", _ "I15", "spnCropTop", _ "I16", "spnCropBottom") With Worksheets("Serial") For iSpn = LBound(spnArr) To UBound(spnArr) Step 2 Set shp = .Shapes(spnArr(iSpn + 1)) shp.Left = .Range(spnArr(iSpn)).Left - shp.Width shp.Top = .Range(spnArr(iSpn)).Top + shp.Height / 2 - shp.Height / 2 Next End With End Sub 

而一个(最终?)重构来分离纯alignment规则代码从select一个可能如下:

 Option Explicit Sub MoveSpinners3() Dim iSpn As Long Dim spnArr As Variant Dim shp As Shape spnArr = Array("I11", "spnHeight", _ "I12", "spnAspect", _ "I13", "spnCropleft", _ "I14", "spnCropRight", _ "I15", "spnCropTop", _ "I16", "spnCropBottom") With Worksheets("Serial") For iSpn = LBound(spnArr) To UBound(spnArr) Step 2 MoveSpin .Shapes(spnArr(iSpn + 1)), .Range(spnArr(iSpn)) Next End With End Sub Sub MoveSpin(shp As Shape, rng As Range) shp.Left = rng.Left - shp.Width shp.Top = rng.Top + shp.Height / 2 - shp.Height / 2 End Sub 

可以立即获得优势,以增强OP的代码alignment规则,并避免由于行高不足而可能出现的旋钮的重叠,如下所示:

 Sub MoveSpin(shp As Shape, rng As Range) rng.RowHeight = shp.Height '<--| make rows height match spinbutton one shp.Left = rng.Left - shp.Width shp.Top = rng.Top + shp.Height / 2 - shp.Height / 2 End Sub