简体   繁体   中英

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. However when I am done the whole macro and go to close the workbooks that were opened, I get an error.

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

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. 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"

ThisWorkbook.Sheets("Info & Inputs").Cells(i + 2, 2).Value returns the full path, like you posted. It returns something like U:\Rating Calculators\New2020\Rates\Rates 2018-10-01.xlsx so when you do:

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

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 . 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) .

Dim wb As Workbook

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

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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