使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:06:11  来源:igfitidea点击:

Use For Loop to obtain values from multiple textboxes in Excel VBA

stringexcelvbafor-loop

提问by user3347107

For a VBA code in Excel (not a userform) I have 4 textboxes named TextBox1to TextBox4.

在Excel中的VBA代码(不是用户窗体)我有4个文本框命名TextBox1TextBox4

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 (Din 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: TextBox1for example. But it starts searching column Dfor the String TextBox1instead of the value presented in this textbox. If I use Weld = Textbox1.ValueThe code works but only for Textbox1of 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