【问题标题】:vba for loop to change text box valuevba for循环更改文本框值
【发布时间】:2016-03-31 16:38:52
【问题描述】:

我正在构建一个用户表单,其中文本框应从工作表中选择值(每月 12 张)。

例如-

文本框 500 到 526 应该选择 jan-dec 表单元格 e5 到 e31 中的值。

文本框 527 到 553 应选择 jan-dec 表单元格 e38 到 e64 中的值。

文本框 554 到 580 应该选择 jan-dec 表单元格 e71 到 e97 中的值。

有人可以帮我创建一个循环来生成上述信息吗?

谢谢

Private Sub monthlist_Change()

Dim myarray As Variant
Dim X As Long

myarray = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

For X = LBound(myarray) To UBound(myarray)


    If Me.monthlist.Text = myarray(X) And Me.Teamlist.Text = "SDM" Then
    
        Me.TextBox500.Value = Worksheets("Functions - " & myarray(X)).Range("e5").Value
    
    ElseIf Me.monthlist.Text = myarray(X) And Me.Teamlist.Text = "Client accounts" Then
    
        Me.TextBox527.Value = Worksheets("Functions - " & myarray(X)).Range("e38").Value
    
    ElseIf Me.monthlist.Text = myarray(X) And Me.Teamlist.Text = "Class action" Then
    
        Me.TextBox554.Value = Worksheets("Functions - " & myarray(X)).Range("e71").Value
    
    End If

End Sub

【问题讨论】:

    标签: vba loops for-loop textbox


    【解决方案1】:

    在 OP 澄清后编辑

    你会使用

    Me.Controls("TextBox" & i).Value = ...
    

    在相应地设置 iMin 和 iMax 之后,您将在其中迭代 i = iMin 到 iMax

    但我也建议对您的代码进行以下重构

    Option Explicit
    
    Private Sub monthlist_Change()
    
    Dim myMnTBarray As Variant, myMxTBarray As Variant
    Dim i As Integer
    Dim mnTB As Integer, mxTB As Integer
    Dim rngAddress As String
    
    myMnTBarray = Array(500, 527, 554) '<== here you place the lower bounds of textbox names "numeric" part, corresponding to the 3 "teamlist" possible values
    myMxTBarray = Array(526, 553, 580) '<== here you place the upper bounds of textbox names "numeric" part, corresponding to the 3 "teamlist" possible values
    
    
    With Me
        i = .teamlist.ListIndex
        mnTB = myMnTBarray(i)
        mxTB = myMxTBarray(i)
    
        Select Case .teamlist.Text
            Case "SDM"
                rngAddress = "e5"
            Case "Client accounts"
                rngAddress = "e38"
            Case "Class action"
                rngAddress = "e71"
        End Select
    End With
    
    If rngAddress <> "" Then
        With Worksheets("Functions - " & Me.monthlist.Text).Range(rngAddress)
            For i = mnTB To mxTB
                Me.Controls("TextBox" & i).Value = .Offset(i - mnTB).Value
            Next i
        End With
    End If
    
    End Sub
    

    唯一的假设是monthlist 列表框按顺序填充了所有月份名称的数组。

    以下只是填充用户表单monthlistteamlist 列表框的可能代码

    Sub main()
    
    With UserForm1
        .monthlist.List = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
        .teamlist.List = Array("SDM", "Client accounts", "Class action")
        .Show
    End With
    Unload UserForm1
    
    End Sub
    

    【讨论】:

    • 我必须承认您的代码超出了我的想象..对不起,我是编码新手。但是,我尝试复制上述代码,但出现错误。
    • 抱歉让我提供更多信息。我的用户有 2 个列表框。 Teamlist(它有 3 个团队..sdm、客户帐户、集体诉讼)。月表(有 12 个月)。我有一个装满标签和文本框的框架(每个 27 个)。当我选择团队(ex-sdm)和月份(ex - 一月)时。文本框(500 到 526)应该选择“功能 - 一月”工作表范围 e5 到 e31 中的数据。当我选择团队客户帐户文本框(527 到 533)时,应该从“jan”表中选择 e38 到 e64。如果团队是集体诉讼 - 文本框 554 到 580 应该选择 e71 到 e97。我有 12 个工作表,每个月数据都会变化。
    • 我几乎明白了。所以我只做了一些小改动:用teamlist 替换i = .teamlist.ListIndex 中的monthlist 并从Me.Controls("TextBox" &amp; i).Value = .Offset(i - mnTB+1).Value 中删除+1。查看编辑的代码
    • 我无法理解你在那里做了什么。但它工作得非常完美。我会做一个研究并了解你做了什么。太感谢了!!! :D
    • 如果我完成了您的问题,请将我的答案标记为解决方案。您可能还想提供一些代表(请参阅stackoverflow.com/help/whats-reputation)。了解正在发生的事情的一种宝贵方法是在代码运行时单步执行代码:将光标放在主 VBE 模块中的任何有效语句上,然后按住 F8 来单步执行每个语句。将鼠标悬停在变量名称上以查询其值。也可以使用即时窗口来查询变量值和表达式。最后打开 Locals 窗口检查变量状态/值。
    猜你喜欢
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2020-09-19
    • 2017-10-21
    • 1970-01-01
    • 2014-03-26
    • 2013-10-01
    • 1970-01-01
    相关资源
    最近更新 更多