Excel VBA-截止到最后一行的日期

Excel VBA-截止到最后一行的日期,excel,vba,date,autofill,Excel,Vba,Date,Autofill,我目前正在努力实现以下目标: 列A中包含数据(例如,橙色、苹果、芒果),列B为空。我想实现一个框,输入日期后,它将自动填充一个范围 以下是我目前的代码: Public Sub DATERES() Dim strUserResponse As String strUserResponse = InputBox("Enter Date") ActiveSheet.Range("B1:B100").Value = strUserResponse End Sub 然而,到目前为止还不错,我的问题

我目前正在努力实现以下目标:

列A中包含数据(例如,橙色、苹果、芒果),列B为空。我想实现一个框,输入日期后,它将自动填充一个范围

以下是我目前的代码:

Public Sub DATERES()
Dim strUserResponse As String

strUserResponse = InputBox("Enter Date")

ActiveSheet.Range("B1:B100").Value = strUserResponse

End Sub
然而,到目前为止还不错,我的问题是1中的数据有时会从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

使用引用的
工作表
范围
s而不是
活动工作表
更安全

' 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
格式输入值,请使用以下行:

Dim strUserResponse As Date

strUserResponse = Application.InputBox("Enter Date", , FormatDateTime(Date, vbShortDate), Type:=1)

您的最后一行看到的是B列,它是空的(阅读文章),您需要用日期填充它(在A列中)。所以您需要搜索A列的最后一行。感谢您的快速响应!!:)很好@MaCIR只是为了确保您没有在代码中间弄乱引用,我会在LASTROW之前使用ActheGeTe<代码> <代码>。或者更好的做法是将引用对象
用于图纸(“yoursheetname”)
。您的意思是像
lastrow=ActiveSheet.Cells(Rows.Count,1)。end(xlup)。row
?或者使用ActiveSheet lastrow=Cells(Rows.Count,1).end(xlup).row Range(“B1:B”&lastrow).Value=strUserResponse end With@llorcs阅读下面我的答案中的提示,它将帮助您节省用户在
输入框中输入错误数据的时间