Excel VBA – 直到最后一行的date
我目前正在努力实现以下内容:
A列中有数据(如Orange,Apple,Mango),B列为空。 我想实现一个框,在inputdate后,它将在一个范围内自动填充。
这是我的代码到目前为止:
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中inputdate,直到最后一行,而不是给它一个更大的范围(如B1:B1000)。
任何帮助和build议表示赞赏。
祝你今天愉快!
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
仅供参考,如果您希望ImputBox
强制用户以Date
格式input值,请使用以下ImputBox
行:
Dim strUserResponse As Date strUserResponse = Application.InputBox("Enter Date", , FormatDateTime(Date, vbShortDate), Type:=1)