英文:
Input Box into a loop only insert last entry
问题
我正在尝试创建一个输入框循环,其中每个输入框都包含列A中单元格的内容,并且要求的输入应该结束在其旁边的单元格(列B),就像图片中所示。好消息(对我来说)是循环可以工作,但在我填写每个输入框之后,宏只会记录整个范围中的最后一个条目(在这种情况下是“未批准”,而前两个条目例如是“已批准”)。你能帮我一下吗?提前感谢!(以下是代码)。
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 "approved"). 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, "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>
# 答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论