Excel VBA - 直到最后一行的日期

标签 excel vba date autofill

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

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

关于Excel VBA - 直到最后一行的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41098031/

相关文章:

vba - 删除除前 x 个工作表之外的所有工作表

c# - 使用 ClosedXML 创建下拉列表

excel - 工作表打开错误

excel - 找出哪些细胞具有最小的 levenshtein 距离

c - 我在 main 中正确调用我的函数吗?

php - 在月初和月末获取一周的开始日期和结束日期

vba - 在 Excel for Mac 2016 中另存为 CSV

c# - 如何让 C# 连接到 excel API

vba - 将 ADODB 二进制流转换为字符串 vba

php - 如何过滤MySQL日期?