如何为每个vba excel

时间:2017-05-19 22:34:33

标签: excel vba variables for-loop inputbox

我在VBA脚本中有for语句,该语句遍历某个范围内的每个单元格(范围中的单元格数量根据用户输入而变化 - 可能是三个单元格可能是100)。 for循环的每个实例都调用一个输入框。如何将for循环的每个实例的用户输入分配给变量以供以后使用? 这是带输入框的for:

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

3 个答案:

答案 0 :(得分:1)

如果没有其他代码,这很难说,但我认为callback()MyQCData。试试下面的内容。我有点强迫"粗暴强迫"输入框看起来是Range,仅供参考。

k

根据@Yow的精明评论编辑

答案 1 :(得分:1)

是的,使用数组:

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) = ....

但很大程度上取决于你打算如何使用数组。

答案 2 :(得分:0)

我要用一个独立的代码来装扮它,你可以轻松地运行它以了解正在发生的事情。因此必须声明数组变量Dim userInput(99),并且99是上限(0-99 = 100个可能的值)。第一个循环的第一行设置变量,userInput(j) = InputBox("Sample InputBox", "InputBox Title", "blah" & j) "blah" & j位是默认条目,这在您编写/调试时非常有用,因为它是'保持输入虚拟数据......要快得多......

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