我有以下代码:
Private Sub ChangeAccountNames_Click()
Dim AccountName As String
Dim Rng As Range
Dim InputQuestion As String
Dim i As Integer
Dim s As Worksheet
Set s = Sheets("Essential Info")
i = Cells(3, 9)
Do
Set Rng = Sheets("Essential Info").Range("I2:I13")
InputQuestion = "Please enter in a bank account name: " & vbNewLine & "Click Close once you are done"
AccountName = InputBox(InputQuestion)
Rng.Value = AccountName
i = i + 1
Loop Until i = 10
End Sub
我需要按以下顺序进行操作:
这里有一些未使用的变量和非常混乱的信息,我已将其添加为个人占位符。逐步操作似乎也可以满足我的需要,但我知道这完全是一团糟
答案 0 :(得分:1)
如果我正确理解了您要做什么,则应该只将Rng
设置为起始 cell 而不是整个范围。快速解决方案是将循环的初始位置设置为 外,并通过使列偏移来使其与i
保持同步。
如果在InputBox
中没有从用户那里获得值,要允许提前退出,只需检查vbNullString
的返回值即可。
我猜您需要这样的东西(未经测试):
Set Rng = Sheets("Essential Info").Range("I2")
Do
InputQuestion = "Please enter in a bank account name: " & vbNewLine & "Click Close once you are done"
AccountName = InputBox(InputQuestion)
If AccountName <> vbNullString Then
Rng.Value = AccountName
i = i + 1
'Offset by Row down
Set Rng = Rng.Offset(1, 0)
'Offset by column right
'Set Rng = Rng.Offset(0, 1)
End If
Loop Until i = 10 Or AccountName = vbNullString