Excel 无法将工作簿标识为对象变量
我正在构建一个自动化的请求表单,遇到了一个只会触发其他用户的头痛问题。3其他人收到运行时错误,我无法弄清楚发生了什么,因为我在其他书籍中基本上使用了相同的脚本,但从未报告过问题Excel 无法将工作簿标识为对象变量,excel,vba,Excel,Vba,我正在构建一个自动化的请求表单,遇到了一个只会触发其他用户的头痛问题。3其他人收到运行时错误,我无法弄清楚发生了什么,因为我在其他书籍中基本上使用了相同的脚本,但从未报告过问题 Sub tracker_upload() ActiveWindow.ScrollRow = 1 Run "processing" 'basic UF to display status With Application .ScreenUpdating = False .DisplayAler
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
后,它在下一个函数上触发了另一个错误,前面有一个小数,如下所示
更新:我解析出了设置
wb1
和wb2
的代码位,因为我确定是wb2
给了用户一个错误。我与其他几个没有问题设置的用户一起做了一些测试&将wb2
识别为工作簿(“请求跟踪器”)
。通过在工作簿
名称的末尾添加文件扩展名,我终于让sub通过了。为什么仅此用户需要此选项?我必须指定set wb2=“Requests Tracker.xlsx”
,以便用户不会遇到运行时错误。我不知道为什么必须为这个用户而不是整个部门的其他人添加.xlsx
,但这解决了头痛的问题。请包含您的代码,而不是代码的屏幕截图。如果您不知道您不应该发布代码的照片,我猜你也没有尝试浏览这个网站寻找一个潜在的解决方案…@TylerH code添加。你有wb2
设置吗?每个设置的前面都是小数点,我觉得很奇怪,因为我以前从未遇到过这个问题
-所以你“修复了”使代码调用当前工作表/工作表的Activate
而不是wb2.Activate
,然后您不知道如何修复。是否保存?请将
放回原处,阅读有关的内容,并确保wb2
已初始化。