0

我想寻求帮助。我的宏即将将当前日期填充到所有填充的行。一开始它似乎工作,突然它开始将标题更改(覆盖)为当前日期并将当前日期放在下面的 1 个单元格中。谁能帮助我我应该修改什么以使其正常工作?

这是宏代码(重新格式化):

Sub DateVerified()

    Sheets("Data").Activate
    Dim rngAddress As Range
    Set rngAddress = Range("A1:ZZ1").Find("Verified Date").Offset(1, 0)
    rngAddress.Select

    Dim ac As Integer
    Dim lastr As Long
    Dim sh As Worksheet


    Set sh = ActiveSheet
    ac = ActiveCell.Column
    lastr = sh.Cells(Rows.Count, ac).End(xlUp).Row
    Range(Cells(2, ac), Cells(lastr, ac)).Select
    Selection.ClearContents
    Selection.Formula = "=TEXT(Now(),""dd/mm/yyyy"")"
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
End Sub
4

2 回答 2

1

以下可能适合:

Sub DateVerified()
    Dim rngAddress As Range
    Dim ac As Integer
    Dim lastr As Long
    Dim sh As Worksheet

    Sheets("Data").Activate
    Set rngAddress = Range("A1:ZZ1").Find("Verified Date").Offset(1, 0)
    rngAddress.Select
    Set sh = ActiveSheet
    lastr = sh.Cells(Rows.Count, 1).End(xlUp).Row
    ac = ActiveCell.Column
    Range(Cells(2, ac), Cells(lastr, ac)).Select
    Selection.ClearContents
    Selection.Formula = "=TEXT(Now(),""dd/mm/yyyy"")"
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues
End Sub
于 2013-07-24T17:10:04.410 回答
0

我认为这很可能在下面的行中出错:

lastr = sh.Cells(Rows.Count, ac).End(xlUp).Row

这会尝试转到活动列的电子表格的最后一行,然后在 excel 中执行相当于 ctrl+up 的操作,以查找该列中填充的最后一行。为了测试这个理论,在这之后直接添加一行代码:

debug.print "lastr set to " & lastr 

并查看此值设置为查看即时窗口(使用 Ctrl+G 从 VBA 编辑器打开)。

于 2013-07-24T17:01:49.160 回答