简体   繁体   English

使用Excel VBA打开工作簿,从certian单元格复制值,保存新表单,然后关闭打开的工作簿

[英]Using Excel VBA to Open a workbook, copy values from certian cells, save the new form, then close the opened workbook

I have a Workbook that is being used to track project information. 我有一个用于跟踪项目信息的工作簿。 I have been updating the workbook functionality and macros, but currently do not have means of transferring all of the user entered information to the updated workbook. 我一直在更新工作簿功能和宏,但目前没有办法将所有用户输入的信息传输到更新的工作簿。 I am creating an "IMPORT" button that will allow the user to select an older version of the workbook, open that workbook (wbout), copy over the values into the new workbook (wbin), save the new workbook, and close the old one without saving it. 我正在创建一个“IMPORT”按钮,允许用户选择较旧版本的工作簿,打开该工作簿(wbout),将值复制到新工作簿(wbin),保存新工作簿,然后关闭旧工作簿一个没有保存它。

At this point I am having troubles with the Workbooks.Open command. 此时我遇到了Workbooks.Open命令的麻烦。 The line that has the command opens the workbook, then I get a 具有该命令的行打开工作簿,然后我得到一个

Run Time Error '91': Object Variable or With Block variable not set. 运行时错误'91':对象变量或未设置块变量。

Can someone please advise as to what I am doing wrong? 有人可以告诉我做错了什么吗? Thanks 谢谢

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

You're getting error 91 because you're assigning an object reference without the Set keyword, here: 您收到错误91因为您在没有Set关键字的情况下分配对象引用,此处:

wbout = Workbooks.Open(folderin)

But once that is fixed, you're getting error 438 because you're referring to a member that isn't defined on the Excel.Workbook interface: 一旦是固定的,你得到错误438,因为你指的是未在上定义的成员Excel.Workbook接口:

Set wsin = wbin.Worksheet("P3 PayOuts")

That's Worksheets , not Worksheet . 这是Worksheets ,而不是Worksheet

Both of these bugs (and possibly many more - you have unused variables in there, and then I expect NewBook to blow up as well) could have been avoided by inspecting with Rubberduck , an open-source VBIDE add-in project I manage. 这两种错误的(也可能是更多的-你必须在有未使用的变量,然后我希望NewBook炸掉一样)本来是可以避免通过检查Rubberduck ,一个开源VBIDE外接程序项目我管理。

Try entering this line into the code above "Wbout = Workbooks.Open(folderin)" line: 尝试将此行输入上面的代码“Wbout = Workbooks.Open(folderin)”行:

Tmp = MsgBox(folderin,vbokonly,"folderin:")

If the result is just a filename (Eg not a full path), try the following amendment (you can then delete the above): 如果结果只是一个文件名(例如不是完整路径),请尝试以下修改(您可以删除上面的内容):

Set WBout = Workbooks.Open(WBin.Path & "\" & folderin)

I also typically use the Sheets collection instead of the Worksheets...not sure if that will change anything, but I have had issues with Worksheets in the past. 我通常也使用Sheets集合而不是Worksheets ...不确定这是否会改变任何东西,但我过去曾遇到过Worksheets的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA从Dropbox打开工作簿,然后保存/关闭当前工作簿 - Excel VBA Open a workbook from dropbox then Save/Close Current Workbook VBA Excel 复制单元格范围从另一个工作簿到活动工作簿 - VBA Excel copy cells range from another workbook to active workbook Excel VBA-将工作簿复制到带有宏的新工作簿中 - Excel VBA - Copy Workbook into a new Workbook with the macros Excel VBA:将列从工作簿复制到新工作簿 - Excel VBA: Copy columns from workbook to new workbook VBA EXCEL:基于部分名称从其他(开放)工作簿(XML)中获取值并将其复制到主工作簿中 - VBA EXCEL: Get values from other (open) workbook (XML) based on partial name and copy these in main workbook 如何使用 VBA 复制用 VBA 打开的 Excel 工作簿的文件名,并在当前工作簿的另存为函数中使用它? - How to use VBA to copy a filename of an Excel workbook opened with VBA and use it in a save-as function for current workbook? VBA复制到新工作簿并保存 - VBA to copy into a new workbook and save Excel VBA 不会关闭我打开的工作簿 - Excel VBA will not close my opened workbook Excel VBA打开工作簿,执行操作,另存为,关闭 - Excel VBA Open workbook, perform actions, save as, close 在VBA中使用工作簿来打开保存的工作簿,修改该预先存在的工作簿,然后保存并关闭 - Using a workbook with VBA to open a saved workbook, modify that pre-existing workbook, save and close
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM