我目前正在努力实现以下目标:
A 列中有数据(例如,Orange、Apple、Mango),B 列为空。我想实现一个框,在输入日期时它会自动填充一个范围。
这是我到目前为止的代码:
Public Sub DATERES()
Dim strUserResponse As String
strUserResponse = InputBox("Enter Date")
ActiveSheet.Range("B1:B100").Value = strUserResponse
End Sub
到目前为止一切都很好,但是,我的问题是 A 中的数据有时会在 1 到 500-600 之间变化。因此,我想修改它,以便它首先检查 A 列,然后只在 B 列中输入日期,直到最后一行,而不是给它一个更大的范围(如 B1:B1000)。
感谢任何帮助和建议。
祝你有美好的一天!
最佳答案
Public Sub DATERES()
Dim strUserResponse As String
dim lastrow as long
strUserResponse = InputBox("Enter Date")
lastrow = Cells(Rows.Count,1).end(xlup).row 'the 1 will get the last row for column A. Change if needed
ActiveSheet.Range("B1:B"& lastrow).Value = strUserResponse
End Sub
使用引用的 Sheets
和 Range
比使用 ActiveSheet
更安全。
' modify "Sheet1" to your sheet's name
With Sheets("Sheet1")
lastrow = .Cells(.Rows.count, 1).End(xlUp).Row ' the 1 will get the last row for column A. Change if needed
.Range("B1:B" & lastrow).Value = strUserResponse
End With
关于Excel VBA - 直到最后一行的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41098031/