宏填充错误的值 - 当前日期

时间:2013-07-24 16:42:22

标签: excel vba excel-vba

我想请求帮助。我的宏即将填充所有填充行的当前日期。在开始它似乎工作,突然它开始改变(覆盖)标题到当前日期,并将当前日期放在下面的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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

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

debug.print "lastr set to " & lastr 

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