excel - Excel 数据存储
问题描述
我有一个 VBA 代码,它将弹出一个输入框供用户输入信息,然后保存在一个单元格中(我有这部分工作),而不是每次用户单击输入框并输入新信息时,它应该将信息保存在下一行而不是擦除同一个单元格。(我需要这部分的帮助)
Private Sub NewTraining_Click()
UserValue = InputBox("What Did You Trained?")
Range("J18").Value = UserValue
UserValue = InputBox("Date?")
Range("K18").Value = UserValue
UserValue = InputBox("Location?")
Range("L18").Value = UserValue
End Sub
解决方案
您可以使用Offset
:
Sub t()
Dim trained As String, iDate As String, location As String
trained = InputBox("What Did You Trained?")
iDate = InputBox("Date?")
location = InputBox("Location?")
' What if they don't enter any value? You can exit sub at that point.
If trained = "" or iDate = "" or location = "" Then Exit Sub
Dim startCel As Range
Set startCel = Cells(Rows.Count, 10).End(xlUp).Offset(1, 0)
startCel.Value = trained
startCel.Offset(0, 1).Value = iDate
startCel.Offset(0, 2).Value = location
End Sub