Excel VBA 代码仅适用于调试模式

lnr*_*ros 1 excel vba

我已经搜索了一堆主题,但似乎没有任何解决方案对我有用。

我有一个 Excel 宏文件,有时可以正常工作,但有时只能在步进模式下工作。

这是主子程序中的子程序,它按类别(键)将值(消息)从 Outlook 日历传递到电子表格。(对于这段代码,我从Script改编为Outlook 中按日历类别的总小时数)。该值进入与类别和列中的周值同名的行。我已经尝试过 DoEvents 并且我认为它有效,但是当我尝试在另一台计算机上运行它时,它又失败了。

有任何想法吗?

Option Explicit
Public keyArray
Sub totalCategories()


Dim app As New Outlook.Application
Dim namespace As Outlook.namespace
Dim calendar As Outlook.Folder
Dim appt As Outlook.AppointmentItem
Dim apptList As Outlook.Items
Dim apptListFiltered As Outlook.Items
Dim startDate As String
Dim endDate As String
Dim category As String
Dim duration As Integer
Dim outMsg As String
Dim firstDayOfTheYear As Date

'Going to be used to get start and end date
firstDayOfTheYear = Date
firstDayOfTheYear = "01/01/" & Right(firstDayOfTheYear, 4)

' Access appointment list
Set namespace = app.GetNamespace("MAPI")
Set calendar = namespace.GetDefaultFolder(olFolderCalendar)
Set apptList = calendar.Items

' Include recurring appointments and sort the list
apptList.IncludeRecurrences = True
apptList.Sort "[Start]"

' Get selected date
startDate = firstDayOfTheYear + 7 * (CInt(SelectWeek.week) - 1)
endDate = firstDayOfTheYear + 7 * (CInt(SelectWeek.week) - 1) + 6
startDate = Format(startDate, "dd/MM/yyyy") & " 00:01"
endDate = Format(endDate, "dd/MM/yyyy") & " 11:59 PM"

' Filter the appointment list
Dim strFilter As String
strFilter = "[Start] >= '" & startDate & "'" & " AND [End] <= '" & endDate & "'"
Set apptListFiltered = apptList.Restrict(strFilter)

' Loop through the appointments and total for each category
Dim catHours
Set catHours = CreateObject("Scripting.Dictionary")
For Each appt In apptListFiltered
    category = appt.Categories
    duration = appt.duration
    If catHours.Exists(category) Then
        catHours(category) = catHours(category) + duration
    Else
        catHours.Add category, duration
    End If
Next

' Loop through the categories
Dim key
keyArray = catHours.Keys
DoEvents 'prevents a bug from happening --> in some cases the total hours weren't divided by categories
For Each key In keyArray
    outMsg = catHours(key) / 60
    'Print in Realizado sheet --> activities must be in range (name manager) as "atividades"
    writeReport SelectWeek.week, outMsg, key
Next

' Clean up objects
Set app = Nothing
Set namespace = Nothing
Set calendar = Nothing
Set appt = Nothing
Set apptList = Nothing
Set apptListFiltered = Nothing

End Sub

Sub writeReport(week, message As String, key)

    Dim ws As Worksheet
    Dim i As Integer
    Dim Activities, nActivities As Integer

    Set ws = Sheets("5")
    Activities = Range("activities")
    nActivities = UBound(Activities)
    DoEvents
    For i = 1 To nActivities
        DoEvents 
        If key = Cells(i + 8, 2).Value Then
            ws.Cells(i + 8, week + 3).Value = CDbl(message)
            Exit For
        End If
    Next i

End Sub
Run Code Online (Sandbox Code Playgroud)

Jab*_*cky 5

你需要明确地处理错误,这样你才能确切地知道发生了什么。相信我,这将节省您对自己的代码进行故障排除的大量时间,尤其是在 VBA 中。

常见的做法类似于“尝试,抓住,最后”。

Dim position as string

Sub foo()
  position = "sub function short description"
  On Error GoTo catch
  Err.Clear

  'do stuff

finally:
  On Error Resume Next
  'do cleanup stuff

  Exit Sub

catch:
  Debug.Print Right(ThisWorkbook.FullName, Len(ThisWorkbook.FullName) - 3) & ", _
  Position: " & errorPosition & " , Error Code: [ " & Hex(Err.number) & "], _
  Description: " & Err.Description & ""
  Resume finally

End Sub
Run Code Online (Sandbox Code Playgroud)