Delete WB

G

Guest

I have two WorkBooks (in the same directory) open Test and TestX. From Test
I want to close and delete TestX. The code I am ussing is;

Sub Delete WB()
sName = ThisWorkbook.FullName
sName = Left(sName, Len(sName) - 4) & "X.xls"
Workbooks("sName").Close SaveChanges:=False
Kill "sName"
End Sub

It errors on the close statement. What am I doing wrong?

Thanks
 
B

Bob Phillips

I think you want

Sub Delete WB()
sName = ThisWorkbook.FullName
sName = Left(sName, Len(sName) - 4) & "X.xls"
Workbooks(sName).Close SaveChanges:=False
Kill "sName"
End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
D

Dave Peterson

..fullname includes the drive and path.

Workbooks(sName) <-- no quotes around the variable
wants just the name (no drive, no path)

sname = thisworkbook.name
sName = Left(sName, Len(sName) - 4) & "X.xls"
workbooks(sName).close savechanges:=false
kill thisworkbook.path & "\" & sname

(You'd still want to specify the full path for the kill statement.)
 
M

michael fuller

Hi there,

I am not sure why your code is not working.

but here is one that does

Sub Delete_WB()
sName = ThisWorkbook.Name
sName = Left(sName, Len(sName) - 4) & "X.xls"
Windows(sName).Activate
ActiveWindow.Close

fName = ThisWorkbook.FullName
pName = Left(fName, Len(fName) - 4) & "X.xls"

Kill pName
End Sub
 

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