使用 For 循环从 Excel VBA 中的多个文本框中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21991222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use For Loop to obtain values from multiple textboxes in Excel VBA
提问by user3347107
For a VBA code in Excel (not a userform) I have 4 textboxes named TextBox1
to TextBox4
.
在Excel中的VBA代码(不是用户窗体)我有4个文本框命名TextBox1
来TextBox4
。
I want to obtain the values from these textboxes using a for loop. The goal is to search for a value in a corresponding column (D
in this case). Therefore I search in the selected sheet (named after the selected value in the UpdateComboBox
). To be able to search I first select the rownumber corresponding to the Weld showed in RelatedWeldTextBox_i
. This value differs following other input boxes.
我想使用 for 循环从这些文本框中获取值。目标是在相应的列中搜索值(D
在本例中)。因此,我在选定的工作表中进行搜索(以 中选定的值命名UpdateComboBox
)。为了能够进行搜索,我首先选择与RelatedWeldTextBox_i
. 此值在其他输入框后有所不同。
The code below is wrong. It creates a String with the for loop: TextBox1
for example. But it starts searching column D
for the String TextBox1
instead of the value presented in this textbox. If I use Weld = Textbox1.Value
The code works but only for Textbox1
of course...
下面的代码是错误的。它使用 for 循环创建一个字符串:TextBox1
例如。但它开始搜索列D
中的字符串TextBox1
而不是此文本框中显示的值。如果我使用Weld = Textbox1.Value
代码有效,但仅适用Textbox1
于...
Sub RelatedWeldNegative ()
Dim Weld As String
Dim Bmin As Integer
Dim Bmax As Integer
Dim i As Integer
For i = 1 To 4`
Weld = "Textbox" & i & ".Value"
For Each Cell In ActiveWorkbook.Sheets(UpdateComboBox.Value).Range("C2:C320")
If Cell.Text = Weld Then
SelecRow = Cell.Row
End If
Next
Next i
Bmax = 6
Bmin = ActiveWorkbook.Sheets(UpdateComboBox.Value).Cell(SelecRow, "D")
'follow up with code
End Sub
Thanks in advance,
提前致谢,
Miriam
米丽亚姆
回答by Gary's Student
Here is how to loop over TextBoxes:
下面是如何遍历 TextBoxes:
Sub dural()
For i = 1 To 4
ActiveSheet.Shapes("TextBox " & i).Select
MsgBox Selection.Characters.Text
Next i
End Sub
Another Way
其它的办法
Sub Sample()
Dim shp As Shape
Dim i As Long
For i = 1 To 4
Set shp = ActiveSheet.Shapes("TextBox" & i)
Debug.Print shp.OLEFormat.Object.Object.Text
Next i
End Sub