workbook question...

  • Thread starter Thread starter Waxaholic
  • Start date Start date
W

Waxaholic

I am having an issue with figuring out how to close a workbook. I know
how to close a workbook when the filename is constant/known but not
when it is dynamic. The scenario is that my Main macro reads in a text
file causing a temporary workbook to be created. It then copies data
from the temp wb into the main wb. Problem is i cannot close the temp
wb because its name constantly changes (book1, book2, etc. ). The
soulution i am looking for is to be able to specify the name of the
temp wb at creation, or capture its name for closing once the data has
been copied to the main wb successfully. I suspect the Freefile
variable is the cause of the temp wb changing from Book1 to Book2 and
so on. Is there a way to set the temp wb name somewhere?

Thanks,
 
Hi Waxaholic,

Try something like:

'=============>>
Public Sub Tester001()
Dim WB As Workbook
Const sStr As String = "C:\YourFile.txt" '<<==== CHANGE
Set WB = Workbooks.Open(sStr)

'Your processing code
WB.Close SaveChanges:=False

End Sub
'<<=============
 
I tried this but it is requiring a fixed name of the source text file
which changes periodically.
 
I figured this out. I just created a new sheet in the current workbook
and used it as a scrath pad, then deleted it.
 

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

Back
Top