关于UserForms中的循环和TextBox,我还有另一个新手场景。
这是单击“保存”命令按钮时我要完成的工作。
下面是工作表的图像,其中包含单元格范围以及用户窗体。
WS和用户窗体
我的启动代码是否正确?还是有一种更有效的方式无循环执行此操作?预先感谢您的协助。
Private Sub SandCont_Click()
Dim wb As Workbook
Dim wsJHA As Worksheet
Dim wsRange1b As Range
Dim wsRange1c As Range
Dim wsRange2 As Range
Dim wsRange3 As Range
Dim wsRange4 As Range
Dim wsRange5 As Range
Dim wsRange6 As Range
Dim wsRange7 As Range
Dim wsRange8 As Range
Dim wsRange9 As Range
Dim wsRange10 As Range
Dim c As Control
Dim i As Integer 'used to loop through cells in ranges listed below???
Dim x As String 'will be first two characters of textbox name
Dim y As String 'will be last character of textbox name
Dim z As Integer 'used to loop through textboxes on userform???
Set wb = Application.ThisWorkbook
Set wsJHA = wb.Sheets("JHA Keying")
Set wsRange1b = wsJHA.Range("B4:B11")
Set wsRange1c = wsJHA.Range("C4:C11")
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
x = Left(c.Name, 2)
y = Right(c.Name, 3)
If x = "co" And y <= "015" Then
wsRange1b.Value = c.Value
End If
End If
Next c
' Application.ThisWorkbook.Save
End Sub
答案 0 :(得分:0)
您应该这样检查文本框的名称:
If c.Name Like "co*0[0-1][0-5]" Then...