View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Closing and reopening the same file

Check VBA's help for the With statement.

It makes coding easier to type and easier to read.

with workbooks("someworkbookname.xls").worksheets("some name")
.range("a1").value = "hi"
.range("b1:b99").clearcontents
.protect
end with

is easier to type and read than:


workbooks("someworkbookname.xls").worksheets("some name").range("a1").value _
= "hi"

workbooks("someworkbookname.xls").worksheets("some name").range("b1:b99") _
.clearcontents

workbooks("someworkbookname.xls").worksheets("some name").protect

The leading dot means that that property or method (like an adjective and or
verb) belongs to the object that was used in the preceding With statement. In
this case a range object on a certain worksheet in a specific workbook.

And so .fullname belongs to the object Activeworkbook.

..fullname will return the drive\path\filename.ext

..name will return only the filename.ext portion.




Tom wrote:

Thank you Dave. That's wonderful. It did exactly what I hoped it would do.
Just a couple of questions:
1. Does Excel capture the name of the active document with the line,
wkbkfullname = .fullname ?
2. The use of dot (.) fascintes me. Where can I find out how else it can be
used? Am not a programmer.

TIA
Tom

"Dave Peterson" wrote in message
...
It won't change the way the code works, but it looks more natural:

Dim wkbkFullName as string
dim wkbk as workbook
with activeworkbook
wkbkfullname = .fullname
.close savechanges:=false
end with
set wkbk = workbooks.open(filename:=wkbkfullname)




Dave Peterson wrote:

If you're running this from a different workbook, but want to close the
activeworkbook.

Dim wkbkFullName as string
dim wkbk as workbook
with activeworkbook
wkbkfullname = .fullname
.close savechanges:=false
set wkbk = workbooks.open(filename:=wkbkfullname)
end with



Tom wrote:

I need a macro to close an open file but without saving its changed
content.
Then reopen it again. Hence, the macro will have to be able to read the
filename first and use that name to reopen it. Any help is much
appreciated.

TIA
Tom

--

Dave Peterson


--

Dave Peterson


--

Dave Peterson