问题
I am currently trying to achieve the following:
Column A has data in it (for example, Orange,Apple,Mango) and Column B is empty. I want to implement a box where upon entering a date it will autofill in a range.
Here's my code so far:
Public Sub DATERES()
Dim strUserResponse As String
strUserResponse = InputBox("Enter Date")
ActiveSheet.Range("B1:B100").Value = strUserResponse
End Sub
So far so good, however, my problem is that the data in A one can vary from 1 to 500-600 at times. As such, I want to modify it so that it first checks column A, and only enters the date in column B until the last row, instead of giving it a much bigger range (like B1:B1000).
Any help and advice is appreciated.
Have a nice day!
回答1:
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
It's safer to use referenced Sheets
and Range
s instead of 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
回答2:
FYI, if you want your ImputBox
to force the user to enter a value in a Date
format, use the lines below:
Dim strUserResponse As Date
strUserResponse = Application.InputBox("Enter Date", , FormatDateTime(Date, vbShortDate), Type:=1)
来源:https://stackoverflow.com/questions/41098031/excel-vba-date-until-last-row