我目前正在尝试实现以下目标:

列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




使用引用的SheetsRange而不是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

09-20 10:03