无法将工作簿标识为对象变量

时间:2018-11-08 19:36:44

标签: excel vba excel-vba

我正在构建一个自动请求表格,并且遇到了麻烦,只会触发其他用户。另外3个人收到了运行时错误,我无法弄清楚是怎么回事,因为我在其他书籍中基本上使用了相同的脚本,而没有报告任何问题。

    Sub tracker_upload()

ActiveWindow.ScrollRow = 1

Run "processing" 'basic UF to display status

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

Run "archive" 'saves completed form to a SP folder

With WaitForm
    .lbStatus.Caption = "...archiving form to shared drive"
    .Repaint
End With

Application.Wait (Now + TimeValue("00:00:02"))

With Form
    If .Priority_Critical_YN = True Then
    p = "Critical"
    ElseIf .Priority_Must_Have_YN = True Then
    p = "High"
    ElseIf .Priority_Need_YN = True Then
    p = "Medium"
    ElseIf .Priority_Nice_YN = True Then
    p = "Low"
    End If
    .Shapes("upload").Visible = False
End With

With Range("tbData")
    uID = .Cells(1).Value
    .Cells(2) = "New"
    .Cells(3) = p
    .Cells(9) = Environ$("UserName")
    .Cells(10) = Date
    .Hyperlinks.Add .Cells(1), ThisWorkbook.FullName, TextToDisplay:=uID
End With


With WaitForm
    .lbStatus.Caption = "...updating tracker information"
    .Repaint
End With


Dim wb1 As Workbook, wb2 As Workbook

    On Error Resume Next
    Set wb1 = ActiveWorkbook
    Set wb2 = Workbooks("Requests Tracker")
'detect if workbook is already open and open if not
    If wb2 Is Nothing Then
    Application.Workbooks.Open ("My Shared Drive Location\Requests Tracker.xlsx"), ignorereadonlyrecommended = True
    Set wb2 = Workbooks("Requests Tracker")
    End If
  On Error GoTo 0

wb1.Sheets("data").Range("tbData").Copy

With wb2
    .Activate
    With .Sheets("Requests")
        If .Range("tbTracker").Cells(1) = "" Then
        lastrow = .Range("A" & Rows.Count).End(xlUp).Row
        Else: lastrow = .Range("A" & Rows.Count).End(xlUp).Row + 1
        End If
    .Range("A" & lastrow).PasteSpecial xlPasteAllUsingSourceTheme
    .Columns.AutoFit
    End With
    .Save
    .Close True
End With

Set wb2 = Nothing

On Error GoTo 0


With Application
    .CutCopyMode = False
    .ScreenUpdating = True
    .DisplayAlerts = True
    .Wait (Now + TimeValue("00:00:02"))
End With

Unload WaitForm

wb1.Save

mb = MsgBox("This request has been successfully recorded on the Tracker" & vbCrLf _
& vbCrLf _
& "The form will now close, would you like to open the tracker now?", vbYesNo + vbInformation, "completed")

If mb = vbYes Then
    Application.Workbooks.Open ("My Shared Drive Location\Requests Tracker.xlsx"), ignorereadonlyrecommended = True
End If

If Application.Windows.Count = 1 Then
    wb1.Saved = True
    Application.Quit
Else: wb1.Close False
End If

End Sub

最初,它挂在.Sheets("Requests")行,然后挂在它下面的行。它是每个小数点前的小数,我发现这真的很奇怪,因为我以前从未遇到过。果然,在.Save.Close True保留小数点后,它在下一个以小数点开头的函数上触发了另一个错误,如下所示。

更新:我分析了设置wb1wb2的部分代码,因为我发现是wb2给用户带来了错误。我与其他几个没有问题的用户进行了测试,这些用户都没有设置问题并将wb2标识为Workbooks("Requests Tracker")。最后,我通过在Workbook名称的末尾添加文件扩展名来使sub通过。为什么只需要此用户要求?

1 个答案:

答案 0 :(得分:0)

我必须指定set wb2 = "Requests Tracker.xlsx",以使用户不会遇到运行时错误。我不知道为什么要为整个部门的这个用户而没有其他人添加.xlsx……但这解决了头疼。