簡體   English   中英

如何為每個VBA Excel將inputbox輸入分配給變量

[英]How do I assign inputbox input to variable for each vba excel

我在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

如果沒有其余代碼,很難說,但我認為MyQCDataRange 請嘗試以下。 我用k表示“強行使用”輸入框,即k

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的敏銳評論進行編輯

是的,使用數組:

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

但是很大程度上取決於您以后打算如何使用數組。

我將用一個獨立的代碼來處理它,您可以輕松地運行它以了解發生了什么。 必須聲明數組變量,因此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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM