Excel VBA - Date until last row

喜你入骨 提交于 2019-12-11 02:44:59

问题


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 Ranges 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!