auto_open宏生成错误代码91

时间:2016-03-16 14:12:17

标签: excel-vba excel-2010 vba excel

宏是一个auto_open(),它在打开电子表格时执行。它首先检查自上次使用以来年份是否发生了变化。如果有,则将其更改为当前年份。作为焦点的工作表被命名为“时间”。它列出了一年中和假期中的所有日子。主要功能是根据系统日期搜索当天。找到后,它指向当天,以记录当天的工作时间。

在Excel 2016中,它按预期运行。在Excel 2010中,它以错误91轰炸。

感谢所有以前看过这个问题的人。第一次堆栈溢出。你不讨厌新手吗? LOL

Public Sub Auto_Open()
Dim Today As Date
Dim Jan1 As Date
Dim Curday As Date
Dim DefYear As Long
Dim CurYear As Long
Dim JDate As Long
Dim TimeSheet As Object

Set TimeSheet = Sheets("Time")

    Today = DateSerial(Year(Now()), Month(Now()), Day(Now()))
    TimeSheet.Select
    Range("A371").Select
    CurYear = ActiveCell.Offset(0, 1).Value
    Curday = ActiveCell.Offset(0, 0).Value
    If Today > Curday Then
    ActiveCell.Offset(0, 1) = Year(Now)
    End If
    Range("A1").Select
    Jan1 = ActiveCell.Offset(1, 0).Value
    Curday = Today
    Cells.Find(What:=Curday, After:=ActiveCell, LookIn:=xlValues, LookAt _
        :=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False).Activate
    ActiveCell.Offset(0, 1).Select
    JDate = DateDiff("d", Jan1, Today)
End Sub

1 个答案:

答案 0 :(得分:0)

也许现在是时候从依靠Range .SelectRange .Activate毕业了 ActiveSheet property方法支持显式范围/单元格/工作表引用。

'Public Sub Auto_Open()
'workbook and worksheet subs are Private, not Public!
Private Sub Workbook_Open()
    'Dim Today As Date
    Dim Jan1 As Date
    Dim strDate As String, curDay As Date, curYear As Long
    'dim defYear As Long
    Dim JDate As Long
    'Dim TimeSheet As Object
    Dim fndDate As Range


    'Today = DateSerial(Year(Now()), Month(Now()), Day(Now()))
    'this can be simpler as Today = Date or Today = Int(Now)
    'or just use Date anytime you would use Today and discard the Today var altogether

    'Set TimeSheet = Sheets("Time")
    'TimeSheet.Select
    'this is better as an explicit worksheet parent reference
    With Worksheets("Time")
        .Activate   '<~~ activate the worksheet for display purposes but do not rely on the ActiveSheet reference

        'Range("A371").Select
        'curYear = ActiveCell.Offset(0, 1).Value
        'curDay = ActiveCell.Offset(0, 0).Value
        'we are working with a defined parent worksheet
        'there are only three uses of ActiveCell but I'll puit them in a With End With anyways
        With .Range("A371")
            curYear = .Offset(0, 1).Value2
            curDay = .Value2    '<~~ don't know what this does since it is reassigned a little further down

            'If Today > curDay Then
            'Date is fine for this
            If Date > curDay Then
                .Offset(0, 1) = Year(Date)
            End If
        End With


        'Range("A1").Select
        'work with A1 (actually A2 with the offset)
        Jan1 = .Range("A1").Offset(1, 0).Value

        'curDay = Today
        'this seems like a unnecessary use of two vars - just use Date
        curDay = Date

        Set fndDate = .Cells.Find(What:=CDate(Date), After:=.Cells(1, 1), MatchCase:=False, _
                                  LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=False, _
                                  SearchOrder:=xlByColumns, SearchDirection:=xlNext)
        'date formatting can foul up a .Find operation. If the above failed, try again with a CDate conversion on string date using the "Short Date" format
        If fndDate Is Nothing Then
            strDate = Format(Date, "Short Date")
            Set fndDate = .Cells.Find(What:=CDate(strDate), After:=.Cells(1, 1), MatchCase:=False, _
                                      LookIn:=xlValues, LookAt:=xlWhole, SearchFormat:=False, _
                                      SearchOrder:=xlByColumns, SearchDirection:=xlNext)
        End If

        If Not fndDate Is Nothing Then
            fndDate.Offset(0, 1).Select '<~~ selected for user interface assistance only
        Else
            Debug.Print Err.Number & ": " & Err.Description
        End If

        'JDate = DateDiff("d", Jan1, Today)
        'again, Date is the current date, Now is the current date and time and Time is the current time
        'DateDif is unnecessary since the difference in days is simple subtraction
        JDate = Date - Jan1
        'don't know what you want to do with jdate
        Debug.Print JDate
    End With
End Sub

您的代码正在运行,因此我已经执行了重写,注意要删除任何依赖于ActiveCell propertyPortableGit-2.7.4-64-bit.7z.exe的隐式引用。此外,VBA中有三个日期类型保留的变量。 日期是当前日期,现在是当前时间的当前日期,时间是没有日期的当前时间。