Input Box into a loop only insert last entry

huangapple go评论78阅读模式
英文:

Input Box into a loop only insert last entry

问题

我正在尝试创建一个输入框循环,其中每个输入框都包含列A中单元格的内容,并且要求的输入应该结束在其旁边的单元格(列B),就像图片中所示。好消息(对我来说)是循环可以工作,但在我填写每个输入框之后,宏只会记录整个范围中的最后一个条目(在这种情况下是“未批准”,而前两个条目例如是“已批准”)。你能帮我一下吗?提前感谢!(以下是代码)。

Input Box into a loop only insert last entry

Sub inputresults()

 Dim myvalue As Variant
 Dim c As Range, rngc As Range, i As Long, lrow As Long

i = 3
lrow = Cells(Rows.Count, "A").End(xlUp).Row

Set rngc = Range(Cells(lrow, "A"), Cells(i, "A"))
    For Each c In rngc
        myvalue = InputBox(c)
    Next

Dim cl As Range, clrng As Range, lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row

Set clrng = Range(Cells(lastrow, "B"), Cells(i, "B"))
    For Each cl In clrng
        cl.Value = myvalue
    Next

End Sub

<details>
<summary>英文:</summary>

I am trying to have a loop of input boxes where each input box has the content of cells in column A, and the input asked should end in the cell next to it (column B); like shown in the picture. The good news (for me) is that the loop works, but after I have filled each input box, the macro registers only the last entry in the entire range (in this case not approved, while for instance the first two entries were &quot;approved&quot;). Could you give me a hand? Thanks in advance! (code below).

[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/wUbUh.png


    
    Sub inputresults()

     Dim myvalue As Variant
     Dim c As Range, rngc As Range, i As Long, lrow As Long

    i = 3
    lrow = Cells(Rows.Count, &quot;A&quot;).End(xlUp).Row

    Set rngc = Range(Cells(lrow, &quot;A&quot;), Cells(i, &quot;A&quot;))
        For Each c In rngc
            myvalue = InputBox(c)
        Next

    Dim cl As Range, clrng As Range, lastrow As Long
    lastrow = Cells(Rows.Count, &quot;A&quot;).End(xlUp).Row
    
    Set clrng = Range(Cells(lastrow, &quot;B&quot;), Cells(i, &quot;B&quot;))
        For Each cl In clrng
            cl.Value = myvalue
        Next

    End Sub

</details>


# 答案1
**得分**: 1

你只需要一个循环。您可以使用`Offset`引用列B来写入输入的值。

```vba
对于每个 c 在 rngc 中
    myvalue = InputBox(c.Value)
    c.Offset(,1).Value = myvalue
下一个

或更简单地:

对于每个 c 在 rngc 中
    c.Offset(,1).Value = InputBox(c.Value)
下一个
英文:

You only need one loop here. You can write the inputted value by referring to column B using Offset.

For Each c In rngc
    myvalue = InputBox(c.Value)
    c.Offset(,1).Value = myvalue
Next

Or more simply:

For Each c In rngc
    c.Offset(,1).Value = InputBox(c.Value)
Next

huangapple
  • 本文由 发表于 2020年1月3日 21:39:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定