如何为每个vba excel分配input框input到variables

我有一个VBA脚本中的每个单元格中的一个for语句(范围中的单元格的数量是可变的,基于用户input – 可能是三个单元格可能是100)。 for循环的每个实例都调用一个input框。 如何将for循环的每个实例的用户input分配给variables供以后使用? 这里是input框的代码:

 For Each cell In MyQCData text_string = cell.Value WrdArray() = split(text_string, ",") For i = LBound(WrdArray) To UBound(WrdArray) strg = strg & vbNewLine & "Part No. " & i & " - " & WrdArray(i) Next i InputBox ("This part requires a " & WrdArray(0) & " measurement of the " & _ WrdArray(1) & vbNewLine & vbNewLine _ & "The range for this is input is " & vbNewLine & vbNewLine & "Lower Control Limit " _ & WrdArray(2) & vbNewLine & "Upper Control Limit " & WrdArray(3)) Erase WrdArray() Next cell 

是的,使用一个数组:

 Dim inputBoxAnswers() As String ReDim inputBoxAnswers(1 To MyQCData.Cells.Count) Dim cellCounter As Long For Each cell In MyQCData text_string = cell.Value WrdArray() = split(text_string, ",") 'Is this loop needed??? For i = LBound(WrdArray) To UBound(WrdArray) strg = strg & vbNewLine & "Part No. " & i & " - " & WrdArray(i) Next i cellCounter = cellCounter + 1 inputBoxAnswers(cellCounter) = InputBox("This part requires a " & _ WrdArray(0) & " measurement of the " & _ WrdArray(1) & _ vbNewLine & vbNewLine & _ "The range for this is input is " & _ vbNewLine & vbNewLine & _ "Lower Control Limit " & WrdArray(2) & _ vbNewLine & _ "Upper Control Limit " & WrdArray(3)) Next cell 

如果您的MyQCData范围不是单列或单行,您可能会发现使用二维数组更容易,可能(可能)使用

 Dim inputBoxAnswers() As String ReDim inputBoxAnswers(1 To MyQCData.Rows.Count, 1 To MyQCData.Columns.Count) 

但是当你指定元素的值的时候你需要重新使用索引。 这可能需要

 inputBoxAnswers(cell.Row - MyQCData.Row + 1, cell.Column - MyQCData.Column + 1) = .... 

但很多取决于你打算如何使用数组。

如果没有其他代码,这很难说,但是我认为MyQCData是一个Range 。 试试下面。 我有点用“ k ,“FYI”“强行”input框。

 Dim k As Long k = 0 Dim inputArr() As Variant ReDim inputArr(myqcdData.Cells.Count) For Each cell In MyQCData text_string = cell.Value WrdArray() = Split(text_string, ",") For i = LBound(WrdArray) To UBound(WrdArray) strg = strg & vbNewLine & "Part No. " & i & " - " & WrdArray(i) Next i inputArr(k) = InputBox("This part requires a " & WrdArray(0) & " measurement of the " & _ WrdArray(1) & vbNewLine & vbNewLine _ & "The range for this is input is " & vbNewLine & vbNewLine & "Lower Control Limit " _ & WrdArray(2) & vbNewLine & "Upper Control Limit " & WrdArray(3)) k = k + 1 Erase WrdArray() Next cell 'Check each value in the array. This is optional and can be removed/commented out For k = LBound(inputArr) To UBound(inputArr) Debug.Print inputArr(k) Next k 

根据@ Yow精明的评论进行编辑

我将打扮成独立的代码,您可以轻松运行以了解发生了什么。 必须声明数组variables,因此Dim userInput(99)和99是上限(0-99 = 100个可能的值)。 第一个循环的第一行设置variables,即userInput(j) = InputBox("Sample InputBox", "InputBox Title", "blah" & j) "blah" & j位是默认条目,这是有用的当你写/debugging时,因为保持input虚拟数据的速度要快很多…

 Sub inputBoxEg() Dim userInput(99) Dim MyQCData As Range Set MyQCData = Range("A1:A4") 'Set InputBox inputs to a variable array called userInput: j = 0 For Each cell In MyQCData userInput(j) = InputBox("Sample InputBox", "InputBox Title", "blah" & j) If userInput(j) = "" Then Exit Sub 'if user pressed cancel or entered blank j = j + 1 Next cell 'Collate variables collected by InputBoxes in a text string called allInputs: allInputs = "" For i = 0 To j - 1 If i = 0 Then allInputs = i & ": " & userInput(i) Else allInputs = allInputs & vbNewLine & i & ": " & userInput(i) End If Next i MsgBox allInputs End Sub