【问题标题】:Input Box into a loop only insert last entry输入框进入循环仅插入最后一个条目
【发布时间】:2020-01-03 13:39:03
【问题描述】:

我正在尝试创建一个输入框循环,其中每个输入框都包含 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

【问题讨论】:

    标签: excel vba loops


    【解决方案1】:

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

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

    或者更简单地说:

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

    【讨论】:

    • 啊!这很简单 :) 非常感谢,这两种解决方案都很好用!
    • @G_TTI - 如果这回答了你的问题,你应该mark it as correct
    猜你喜欢
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 2014-01-18
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多