Close workbooks saved as elements in an array

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,922
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have an array of data type Variant, who's elements are workbooks opened by a user.

The array size is static, which for now isn't a concern but I can't work out how to close the workbooks in the array via a loop and the usual vba code of Workbooks("file").Close

Code I have that doesn't work is:

Sub Close_Workbooks_In_An_Array ()

Dim dFile (1 to 6) As Variant
Dim i As Integer, j As Integer

' // Some code to open files, set each dFile(i) as a file and then process
' // them. Max value for i is 6

j = 1
For j = 1 To i
MsgBox ("Closing: " & vbNewLine & vbNewLine & dFile(j))
Workbook.(dFile(j)).Close
Next j

End Sub

Any help on what the code in red should be?

Thanks in advance,
Jack
 
Hi Andrew, thanks for being the first to reply!
Unfortunately, that was a typo on my part. When I run the below (with Workbooks!) I receive following error message:

Run-time error '9':
Subscript out of range

Any thoughts?
Thanks,
Jack
 
Upvote 0
Error again:

Run-time error '13':
Type mismatch

j is an integer, dFile is the array, but I thought LBound and UBound should both be integer's themselves?
 
Upvote 0
Morning, relevent code is:

Public dFile(1 To 6) As Variant
' Array, public so I can later call it from other macros,
' should this be Global instead?

Sub Open_Selected_Files()

Dim Path1 As String
Path1 = Range("Path1").Value
' This relates to a named range (Path1) on spreadsheet with macro
' which in turn is a cell with a directory path on it

Dim i As Integer
i = 1
Dim j As Integer
j = 1
' Variables to loop with

For i = 1 to UBound(dFile)
' Other code here that opens files and places them into dFile(i)
Next i

For j = LBound(dFile) To UBound(dFile)
MsgBox ("Closing: " & vbNewLine & vbNewLine & "(" & j & ") " & dFile(j))
Workbooks(File(j)).Close
Next j

MsgBox ("Operation complete")
End Sub


Thanks, Jack
 
Upvote 0
Morning, code is:

Public dFile(1 To 6) As Variant
' Array, public so I can later call it from other macros,
' should this be Global instead?

Sub Open_Selected_Files()

Dim i As Integer
i = 1
Dim j As Integer
j = 1
' Variables to loop with

For i = 1 to UBound(dFile)
' Other code here that opens files and places them into dFile(i)
Next i

For j = LBound(dFile) To UBound(dFile)
MsgBox ("Closing: " & vbNewLine & vbNewLine & "(" & j & ") " & dFile(j))
Workbooks(dFile(j)).Close
Next j

MsgBox ("Operation complete")
End Sub


Thanks, Jack
 
Upvote 0
This simplified example worked for me:

Code:
Public dFile(1 To 6) As Variant
Sub Test()
    Dim i As Integer
    Dim j As Integer
    For i = 1 To UBound(dFile)
        dFile(i) = Chr(64 + i)
    Next i
    For j = LBound(dFile) To UBound(dFile)
        MsgBox ("Closing: " & vbNewLine & vbNewLine & "(" & j & ") " & dFile(j))
    Next j
End Sub

On which line do you get the error?
 
Upvote 0
In my example after the line, I have the closing line which your code doesn't appear to:

MsgBox ("Closing: " & vbNewLine & vbNewLine & "(" & j & ") " & dFile(j))
Workbooks(dFile(j)).Close

That's the line where it's failing!

Also, what does Chr(64 + i) mean? Is that the equivalent to populating the array with at element i with a file that I've defined as a Variant in the Public statement?
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top