lastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row ws.Range("H20").FormulaR1C1 = "=TODAY()" ws.Range("H20:H" & lastRow).FillDown 填充数值和左列列数一致
时间: 2024-05-16 09:18:30 浏览: 199
根据你的描述,我理解你需要在 H20 单元格输入当天日期,并将该日期填充到 H 列的最后一行。可以参考下面的代码:
```
lastRow = ws.Cells(Rows.Count, "H").End(xlUp).Row
ws.Range("H20").Value = Date
ws.Range("H20:H" & lastRow).FillDown
```
第一行代码获取 H 列的最后一行,第二行代码将当天日期赋值到 H20 单元格,第三行代码将 H20 单元格中的日期填充到 H 列的最后一行。注意,使用 `Value` 属性而不是 `FormulaR1C1` 属性来设置单元格的值,这样可以避免后续计算错误。
相关问题
'获取最后一行 lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row这是获取B列有数据的最后一行行号吗
是的,您理解得很正确。这一行代码的作用是获取工作表 `ws` 中包含数据的最后一行行号,其中 `ws.Rows.Count` 表示工作表的总行数,`"B"` 表示要操作的列,`.End(xlUp)` 表示从下往上查找,直到找到第一个非空单元格,最后 `.Row` 表示返回该单元格所在的行号。
因此,`lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row` 这一行代码的作用就是获取B列中包含数据的最后一行行号,然后将其赋值给变量 `lastRow`,供后续代码使用。
lastrow = ActiveSheet.Cells(Rows.Count, "H").End(xlUp).Row For i = 1 To lastrow - 2 Cells(2, currentMonth + 15).Offset(i).FillDown Next i 這一段循環要可以不用循環實現嗎?
是的,您可以使用一行代码来代替该循环。以下是简化后的代码:
```vba
Range(Cells(2, currentMonth + 15), Cells(lastrow - 2, currentMonth + 15)).FillDown
```
这行代码将在指定范围内填充下来,而无需使用循环。请确保将 `currentMonth` 替换为您要使用的实际值。
阅读全文
相关推荐















