如何移动(替换)在运行时创建的TextBox,并按名称引用它们? VBA

时间:2013-08-23 18:15:21

标签: vba textbox runtime

因此,我创建了一个用户表单,该表单将从用户那里获取输入,以创建一个有助于评估我的工作的财务投资的工具。我的用户表格考虑了投资产生的任何可能的收入和支出,投资将有用多年。为此,我添加了3个按钮,一个创建新的收入行,另一个创建新的费用行,最后一个新的年份列,给我两个转换年x和费用x年的矩阵。如果您需要更多详细信息,请告诉我们!

看到的小“+”按钮会增加收入或费用或年份列数。我的问题是,当我添加一行收入时,我必须取消费用,甚至是那些在运行时创建的费用。我的方法是使用控件集合和文本框的名称(我已经制作成矩阵:e1y1,e2y2,e1y2,e2y2,......等等)使用一些用于每个语句和一对循环。该程序运行和编译正确,但唯一向下移动的是标签和框架,它们在每个循环代码之外。我附上了相关的代码,但是如果还有其他任何需要让我知道的话,那么你的帮助就会受到青睐!

Private Sub moveControls()

    Dim ctl As Control
    Dim cell As TextBox
    Let y = 0
    Let e = 0

    For Each ctl In Me.Controls
        If TypeOf ctl Is TextBox Then
        cell = ctl
            Do While y < years
                Do While e < expenses
                    'Names of the small cells that are in a grid
                    ' which are the expense values
                    If cell.name = "e" & e & "y" & y Then
                        cell.top = cell.top + 19
                    End If
                    'Names of the larger cells on the left under
                    ' "Expenses" which are the expense names
                    If cell.Name = "expenses" & e Then
                        cell.top = cell.top + 19
                    End If
                    e = e + 1
                Loop
                y = y + 1
            Loop
        End If
    Next ctl

    'This part of the code executes perfectly
    revenuesframe.top = revenuesframe.top + 19
    expenseslbl.top = expenseslbl.top + 19
    addxpsbutton.top = addxpsbutton.top + 19

End Sub

0 个答案:

没有答案