vba for循环改变文本框的值
vba for loop to change text box value
我正在构建一个用户表单,其中的文本框应从工作表中选择值(每月 12 张)。
例如-
textbox 500 till 526 should pick the value in jan-dec sheet cell e5 to e31.
textbox 527 till 553 should pick the value in jan-dec sheet cell e38 to e64.
textbox 554 till 580 should pick the value in jan-dec sheet cell e71 to 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
编辑 在 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
列表框按顺序填充所有月份名称的数组。
以下只是填充用户表单 monthlist
和 teamlist
列表框的可能代码
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
我正在构建一个用户表单,其中的文本框应从工作表中选择值(每月 12 张)。
例如-
textbox 500 till 526 should pick the value in jan-dec sheet cell e5 to e31.
textbox 527 till 553 should pick the value in jan-dec sheet cell e38 to e64.
textbox 554 till 580 should pick the value in jan-dec sheet cell e71 to 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
编辑 在 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
列表框按顺序填充所有月份名称的数组。
以下只是填充用户表单 monthlist
和 teamlist
列表框的可能代码
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