Closing a file

G

Gert-Jan

Hi,

I am using this for importing some sheets from a workbook. After importing
the file must be closed.

Sub importerendb()
Range("rekenvel!E2").Value = Range("rekenvel!b13").Value & "\" &
importeren.databasescombo.Value & ".xls"
Dim bestandsnaam
bestandsnaam = Range("rekenvel!E2").Value
Workbooks.Open Filename:=bestandsnaam, Password:="XYZ"
For Each n In ActiveWorkbook.Names
n.Delete
Next
Sheets(Array("lijstkerken", "database")).Move Before:=Workbooks( _
"myfile.xls").Sheets(1)
Workbooks(bestandsnaam).Close savechanges:=False
Unload importeren
End Sub

It goes wrong in the line "Workbooks(bestandsnaam).Close savechanges:=False"

What am I doing wrong?

Thanks for help, Gert-Jan
 
B

Bob Umlas

Change
Workbooks(bestandsnaam).Close savechanges:=False
to
Workbooks(DIR(bestandsnaam)).Close savechanges:=False
to strip the path -- workbooks(name with path).Close doesn't work. need name
without path
 
G

Guest

Sub importerendb()
Dim bk as Workbook
Dim bestandsnaam as String
Range("rekenvel!E2").Value = Range("rekenvel!b13").Value _
& "\" & importeren.databasescombo.Value & ".xls"
bestandsnaam = Range("rekenvel!E2").Value
set bk = Workbooks.Open( _
Filename:=bestandsnaam, Password:="XYZ")
For Each n In bk.Names
n.Delete
Next
Sheets(Array("lijstkerken", "database")).Move Before:=Workbooks( _
"myfile.xls").Sheets(1)
bk.Close savechanges:=False
Unload importeren
End Sub
 
G

Guest

If it worked previously, then it should work this way.

this demo from the immediate window shows that it worked fine for me:

bestandsnaam = "C:\Data\TestPassword.xls"
? bestandsnaam
C:\Data\TestPassword.xls
set bk = workbooks.Open(Filename:=bestandsnaam,Password:="ABC")
? bk.Name
TestPassword.xls
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top