简体   繁体   English

使用 Excel VBA 关闭变量工作簿

[英]Closing Variable Workbook with Excel VBA

I currently have a cell range with file paths that are populated using a file browser in the Userform I've created.我目前有一个单元格范围,其中包含使用我创建的用户表单中的文件浏览器填充的文件路径。 These files has rates and tables I will use in the formulas in the "Calculator" workbook.这些文件包含我将在“计算器”工作簿的公式中使用的汇率和表格。 I have this macro to open the selected workbooks:我有这个宏来打开选定的工作簿:

For i = 1 To 8
If IsEmpty(ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2)) Then
Else
Workbooks.Open (ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2).Value)

End If
Next I

Which uses the cell values to determine which Files to open.它使用单元格值来确定要打开的文件。 The cell value is something like this: U:\Rating Calculators\New2020\Rates\Rates 2018-10-01.xlsx which open fine.单元格值是这样的: U:\Rating Calculators\New2020\Rates\Rates 2018-10-01.xlsx 打开正常。 However when I am done the whole macro and go to close the workbooks that were opened, I get an error.但是,当我完成整个宏和 go 以关闭打开的工作簿时,出现错误。

Here is my code:这是我的代码:

For i = 1 To 8
If IsEmpty(ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2)) Then
Else
Workbooks(ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2).Value).Close Savechanges:=False
End If
Next I

This does not work, but if I manually type the file path (without the U:/ file path) and just do这不起作用,但是如果我手动输入文件路径(没有 U:/ 文件路径)然后执行

For i = 1 To 8
If IsEmpty(ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2)) Then
Else
Workbooks("Rates 2018-10-01.xlsx").Close Savechanges:=False
End If
Next I

The Close line works, but then it is not a variable and will not close the possibly 1 to 8 files that the macro has opened. Close 行有效,但它不是变量,不会关闭宏打开的可能 1 到 8 个文件。 I can't hard code this and do an "If this file is open, close it" because the names might change.我无法对此进行硬编码并执行“如果此文件已打开,请关闭它”,因为名称可能会更改。

Any easy solution to this?有什么简单的解决方案吗? I can't do我做不到

Workbooks(right(ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2).Value, 21)).close

because the name won't always be "Rates YYYY-MM-DD.xlsx"因为名称并不总是“Rates YYYY-MM-DD.xlsx”

ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2).Value returns the full path, like you posted. ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2).Value返回完整路径,就像您发布的那样。 It returns something like U:\Rating Calculators\New2020\Rates\Rates 2018-10-01.xlsx so when you do:它返回类似于U:\Rating Calculators\New2020\Rates\Rates 2018-10-01.xlsx ,因此当您执行以下操作时:

Workbooks(ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2).Value).Close Savechanges:=False

VBA thinks you are doing something like: Workbooks("U:\Rating Calculators\New2020\Rates\Rates 2018-10-01.xlsx").Close Savechanges:=False VBA 认为您正在做类似的事情: Workbooks("U:\Rating Calculators\New2020\Rates\Rates 2018-10-01.xlsx").Close Savechanges:=False

And that's wrong.这是错误的。 It should be only the name of opened workbook, not the full path.它应该只是打开的工作簿的名称,而不是完整路径。

You must close each workbook, one by one.您必须一一关闭每个工作簿。

When you open a workbook, you could assign it to a varible defined as Workbook .打开工作簿时,您可以将其分配给定义为Workbook的变量。 Like this:像这样:

Dim wb As Workbook
Set wb = Application.Workbooks.Open(pathtofile)

wb.Close False

The problem is that your code opens several workbooks at same time, you'll need a variable for each one of them.问题是您的代码同时打开多个工作簿,您需要为每个工作簿设置一个变量。

My suggestion:我的建议:

Do a loop to close all workbooks excep the one that holds the macro ( ThisWorkbook) .执行循环以关闭所有工作簿,但包含宏 ( ThisWorkbook)的工作簿除外。

Dim wb As Workbook

For Each wb In Application.Workbooks
    If wb.Name <> ThisWorkbook.Name Then wb.Close False
Next wb

Hope this helps希望这可以帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM