我有一个用于跟踪项目信息的工作簿。我一直在更新工作簿功能和宏,但目前没有办法将所有用户输入的信息传输到更新的工作簿。我正在创建一个" IMPORT"按钮,允许用户选择较旧版本的工作簿,打开该工作簿(wbout),将值复制到新工作簿(wbin),保存新工作簿,并关闭旧工作簿而不保存它。
此时我遇到了Workbooks.Open命令的麻烦。具有该命令的行打开工作簿,然后我得到一个
运行时错误' 91':对象变量或未设置块变量。
有人可以告诉我我做错了什么吗?感谢
Private Sub ImportPrj1_Click()
Dim rngin As Range
Dim rngout As Range
Dim wsin As Worksheet
Dim wsout As Worksheet
Dim wbin As Workbook
Dim wbout As Workbook
Dim namein As String
Dim folderin As Variant
Dim msg As String
Set wbin = ActiveWorkbook
folderin = Application.GetOpenFilename("Excel-files,*.xlsm", 1, "Select Older Version to transfer information from.", , False)
If folderin = False Then Exit Sub
Application.ScreenUpdating = False
wbout = Workbooks.Open(folderin) ' <--- Line executes but returns error
Set wsin = wbin.Worksheet("P3 PayOuts")
Set wsout = wbout.Worksheets("P3 PayOuts")
wsin.Range("H4").Value = wsout.Range("H4").Value
wsin.Range("C9:V10").Value = wsout.Range("C9:V10").Value
wsin.Range("K3:K5").Value = wsout.Range("K3:K5").Value
wsin.Range("L3:L5").Value = wsout.Range("L3:L5").Value
' Create File name, save new tracker, close old tracker, make summary active,screen updates = true
namein = InputBox("Enter 1-2 word project identifier to be used in the File Name", "File Name")
namein = "Project Tracker V1.2" & namein
msg = "Project Tracker with imported data will be saved in the same folder as the old version"
msg = msg & vbNewLine & vbNewLine & "File Name: " & namein & ".xlsm"
MsgBox (msg)
Application.DisplayAlerts = False
Application.EnableEvents = False
folderin = Application.wbout.Path
namein = folderin & "\" & namein
NewBook.SaveAs Filename:=namein, FileFormat:=52 ' xlOpenXMLWorkbookMacroEnabled
Workbooks(wbout).Close (False)
With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With
wbin.Worksheets("Summary").Activate
End Sub
答案 0 :(得分:3)
您收到错误91,因为您在没有SELECT VARCHAR_FORMAT(DATE(TIMESTAMP_FORMAT(CHAR(OAORDT),'YYYYMMDD')),'DD/MM/YYYY')
关键字的情况下分配对象引用,此处:
Set
但是一旦 被修复,你就会收到错误438,因为你指的是wbout = Workbooks.Open(folderin)
界面上没有定义的成员:
Excel.Workbook
那是Set wsin = wbin.Worksheet("P3 PayOuts")
,而不是Worksheets
。
这两个错误(可能还有更多 - 你在那里有未使用的变量,然后我预计Worksheet
也会爆炸)可以通过Rubberduck检查来避免我管理的-source VBIDE加载项项目。
答案 1 :(得分:0)
尝试将此行输入上面的代码&#34; Wbout = Workbooks.Open(folderin)&#34;行:
Tmp = MsgBox(folderin,vbokonly,"folderin:")
如果结果只是一个文件名(例如,不是完整路径),请尝试以下修改(您可以删除上述内容):
Set WBout = Workbooks.Open(WBin.Path & "\" & folderin)
我通常也使用Sheets集合而不是Worksheets ...不确定这是否会改变任何内容,但我过去曾遇到过Worksheets的问题。