简体   繁体   English

Excel宏/ Userform:“For Each”不以TabIndex顺序循环控件

[英]Excel Macro/Userform: “For Each” not looping through controls in TabIndex order

Could someone help me understand how to adjust the order that a "For Each" loop will cycle through a UserForm's controls? 有人可以帮我理解如何调整“For Each”循环在UserForm控件中循环的顺序吗? (I know it's not cycling via "TabIndex" order, and I understand it doesn't care about the "TabIndex" when looping programatically in "For Each".) (我知道它不是通过“TabIndex”顺序循环,我理解它在“For Each”中以编程方式循环时不关心“TabIndex”。)

I added controls to my current UserForm (screenshot below) by copying a single controls for a couple and then a group of controls (Ctrl+click, copy/paste) from a previous form, and adding controls after that. 我将控件添加到我当前的UserForm(下面的屏幕截图),方法是复制一对控件,然后从前一个表单复制一组控件(Ctrl +单击,复制/粘贴),然后添加控件。 The group of controls ("Site Name" to "City,St,Zip") cycles in reverse-order (bottom to top). 控件组(“站点名称”到“City,St,Zip”)以相反顺序(从下到上)循环。 Ideally, I want to cycle from the top to bottom continuously. 理想情况下,我想连续从上到下循环。

I tried to re-order by right-clicking and "Send Backward/Forward" but that does nothing for my "For Each" loop. 我试图通过右键单击和“发送后退/前进”来重新排序,但这对我的“For Each”循环没有任何作用。

For Each txtBox In ADSheaderForm.Controls
    If TypeName(txtBox) = "TextBox" Then
        If txtBox.Name = "stNmTxtBx" Then
            ReDim Preserve txtArr(txtNum)
            txtArr(txtNum) = txtBox.Text
            txtNum = txtNum + 1
        Else
            If txtBox.Text = "" Or txtBox.Text = "Optional" Then
                ReDim Preserve txtArr(txtNum)
                txtArr(txtNum) = "Not Entered"
                txtNum = txtNum + 1
            Else
                ReDim Preserve txtArr(txtNum)
                txtArr(txtNum) = txtBox.Text
                txtNum = txtNum + 1
            End If
        End If
    End If
Next

在此输入图像描述

The controls seem to be added into the form by copy/paste order(a group often has last as first), and you cannot change that order of controls. 控件似乎通过复制/粘贴顺序添加到表单中(一个组通常最后作为第一个),并且您无法更改控件的顺序。

You could look at the following code and see how you might use the tabIndex to reorder the controls for your purpose. 您可以查看以下代码,看看如何使用tabIndex为您的目的重新排序控件。

  Dim ctrl As Control
  Dim str As String
  Dim v() As Control
  Dim i As Long
  Dim arrItem As Variant

  i = ADSheaderForm.Controls.count
  ReDim v(1 To i)

  For Each ctrl In ADSheaderForm.Controls
        Set v(ctrl.TabIndex + 1) = ctrl
  Next ctrl

  For Each arrItem In v
        str = str & arrItem.Name & vbNewLine
        'Actually do your code as above within this loop e.g. if typeName = blabla
  Next arrItem

  MsgBox str

If you set the tabIndexes to run as you do, then this will push them into an array by tabIndex position. 如果将tabIndexs设置为像您一样运行,那么这将通过tabIndex位置将它们推送到数组中。 Then you cycle through the array. 然后循环遍历数组。

I had a similar problem working with an Access form and was looking for solutions, so I stumbled upon this post. 我在使用Access表单时遇到了类似的问题并且正在寻找解决方案,所以我偶然发现了这篇文章。 Because of my very limited knowledge of VBA syntax, I had to turn MarcoMarc's idea (thanks for that, btw) into something I would know how to write. 由于我对VBA语法的了解非常有限,我不得不将MarcoMarc的想法(感谢btw)转变为我知道如何编写的东西。

Here's what I came up with... It worked for me: 这就是我想出来的......它对我有用:

Dim i as Integer
Dim ctl as Control

For i = 1 To ADSheaderForm.Controls.Count
    For Each ctl In ADSheaderForm.Controls
        If ctl.TabIndex = i then
            'do something 
            Exit For
        End If
    Next ctl
Next i

You can reorder them all at once, as MacroMarc touched on, using the copy/paste function but the controls must be selected REVERSE order from what you want them to cycle. 您可以使用复制/粘贴功能在MacroMarc触及时立即对它们进行重新排序,但必须根据您希望它们循环的顺序选择REVERSE顺序。

For your example, in the userform you would select the textbox next to "Scope of Work", hold CTRL and click on each subsequent control, again in reverse order. 对于您的示例,在userform中,您将选择“工作范围”旁边的文本框,按住CTRL并再次按相反的顺序单击每个后续控件。 Cut, paste and voilà, they will cycle in the correct order. 剪切,粘贴和vo,它们将以正确的顺序循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM