Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Open and Close another Workbook from VBA

Greetings.

I wrote the following subroutine to open, do some formatting,
and then close a workbook prior to importing its data into my
current workbook:

Sub CleanSrc(src As String)
Set srcApp = New Excel.Application
srcApp.Visible = False
Set srcWkBk = srcApp.Workbooks.Open(myFile)
Set srcSht = srcWkBk.Sheets(1)
srcLast = srcSht.Range("A1").End(xlDown).Row
srcSht.Rows((srcLast + 1) & ":" & (srcLast + 5)).Delete
srcWkBk.Close (True)
Set srcWkBk = Nothing
Set srcApp = Nothing
End Sub

My problem is that later when I attempt to import data from
this workbook, I get complaints it being locked (by myself!)
and read only.

It appears that somehow this workbook is never really closed
and/or the application I spun up wasn't exited properly.

Can somebody please tell me what I can do to fix this problem?

Regards,

Charles
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Open and Close another Workbook from VBA

- Do you need another instance of Excel ?
Set srcApp = New Excel.Application
Just open all in your current instance.

- Also, it would us (and you) if you declared your variables
Dim srcWkBk as workbook
Check out "Option Explicit" in the VBA help.

- Also, I doubt you are opening the file you think. You pass "src" as an
argument, but open "myFile", whatever that is.
Sub CleanSrc(src As String)
.......
Set srcWkBk = srcApp.Workbooks.Open(myFile)

- Remove the ( ) from
srcWkBk.Close (True)

NickHK

"Charles in Iraq" wrote in message
...
Greetings.

I wrote the following subroutine to open, do some formatting,
and then close a workbook prior to importing its data into my
current workbook:

Sub CleanSrc(src As String)
Set srcApp = New Excel.Application
srcApp.Visible = False
Set srcWkBk = srcApp.Workbooks.Open(myFile)
Set srcSht = srcWkBk.Sheets(1)
srcLast = srcSht.Range("A1").End(xlDown).Row
srcSht.Rows((srcLast + 1) & ":" & (srcLast + 5)).Delete
srcWkBk.Close (True)
Set srcWkBk = Nothing
Set srcApp = Nothing
End Sub

My problem is that later when I attempt to import data from
this workbook, I get complaints it being locked (by myself!)
and read only.

It appears that somehow this workbook is never really closed
and/or the application I spun up wasn't exited properly.

Can somebody please tell me what I can do to fix this problem?

Regards,

Charles



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Open and Close another Workbook from VBA

Thanks Nick. This advice helped a lot.

I rewrote my subroutine as follows:

Sub CleanSrc(src As String)
Dim srcWkBk As Excel.Workbook
Dim srcSht As Excel.Worksheet
Dim srcLast As Integer

Set srcWkBk = Application.Workbooks.Open(src)
Set srcSht = srcWkBk.Sheets(1)
srcLast = srcSht.Range("A1").End(xlDown).Row
srcSht.Rows((srcLast + 1) & ":" & (srcLast + 5)).Delete
srcWkBk.Close True
End Sub

However, now I notice another strange error.

If I double click on the source workbook while still inside my
current workbook, a dialog window pops up with the following
error message:

Cannot access myFile.xls

Do you know how I can fix this annoyting error?

Regards,

Charles
"NickHK" wrote:

- Do you need another instance of Excel ?
Set srcApp = New Excel.Application
Just open all in your current instance.

- Also, it would us (and you) if you declared your variables
Dim srcWkBk as workbook
Check out "Option Explicit" in the VBA help.

- Also, I doubt you are opening the file you think. You pass "src" as an
argument, but open "myFile", whatever that is.
Sub CleanSrc(src As String)
.......
Set srcWkBk = srcApp.Workbooks.Open(myFile)

- Remove the ( ) from
srcWkBk.Close (True)

NickHK

"Charles in Iraq" wrote in message
...
Greetings.

I wrote the following subroutine to open, do some formatting,
and then close a workbook prior to importing its data into my
current workbook:

Sub CleanSrc(src As String)
Set srcApp = New Excel.Application
srcApp.Visible = False
Set srcWkBk = srcApp.Workbooks.Open(myFile)
Set srcSht = srcWkBk.Sheets(1)
srcLast = srcSht.Range("A1").End(xlDown).Row
srcSht.Rows((srcLast + 1) & ":" & (srcLast + 5)).Delete
srcWkBk.Close (True)
Set srcWkBk = Nothing
Set srcApp = Nothing
End Sub

My problem is that later when I attempt to import data from
this workbook, I get complaints it being locked (by myself!)
and read only.

It appears that somehow this workbook is never really closed
and/or the application I spun up wasn't exited properly.

Can somebody please tell me what I can do to fix this problem?

Regards,

Charles




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Open and Close another Workbook from VBA

I assume you mean that sourceWB is not open, but the code has run
successfully before on that WB. If so, not sure if this applicable...

I have just upgraded to XL2002 and noticed that I receive that error
occasionally on files that I have not opened recently, certainly not
involved in any coding.
Click OK and open again results in success.
Not sure what the cause is but seems to happen most often on XL files with
Chinese characters in the name and/or when I am working fast between
Explorer and Excel.

NickHK

"Charles in Iraq" wrote in message
...
Thanks Nick. This advice helped a lot.

I rewrote my subroutine as follows:

Sub CleanSrc(src As String)
Dim srcWkBk As Excel.Workbook
Dim srcSht As Excel.Worksheet
Dim srcLast As Integer

Set srcWkBk = Application.Workbooks.Open(src)
Set srcSht = srcWkBk.Sheets(1)
srcLast = srcSht.Range("A1").End(xlDown).Row
srcSht.Rows((srcLast + 1) & ":" & (srcLast + 5)).Delete
srcWkBk.Close True
End Sub

However, now I notice another strange error.

If I double click on the source workbook while still inside my
current workbook, a dialog window pops up with the following
error message:

Cannot access myFile.xls

Do you know how I can fix this annoyting error?

Regards,

Charles
"NickHK" wrote:

- Do you need another instance of Excel ?
Set srcApp = New Excel.Application
Just open all in your current instance.

- Also, it would us (and you) if you declared your variables
Dim srcWkBk as workbook
Check out "Option Explicit" in the VBA help.

- Also, I doubt you are opening the file you think. You pass "src" as an
argument, but open "myFile", whatever that is.
Sub CleanSrc(src As String)
.......
Set srcWkBk = srcApp.Workbooks.Open(myFile)

- Remove the ( ) from
srcWkBk.Close (True)

NickHK

"Charles in Iraq" wrote in

message
...
Greetings.

I wrote the following subroutine to open, do some formatting,
and then close a workbook prior to importing its data into my
current workbook:

Sub CleanSrc(src As String)
Set srcApp = New Excel.Application
srcApp.Visible = False
Set srcWkBk = srcApp.Workbooks.Open(myFile)
Set srcSht = srcWkBk.Sheets(1)
srcLast = srcSht.Range("A1").End(xlDown).Row
srcSht.Rows((srcLast + 1) & ":" & (srcLast + 5)).Delete
srcWkBk.Close (True)
Set srcWkBk = Nothing
Set srcApp = Nothing
End Sub

My problem is that later when I attempt to import data from
this workbook, I get complaints it being locked (by myself!)
and read only.

It appears that somehow this workbook is never really closed
and/or the application I spun up wasn't exited properly.

Can somebody please tell me what I can do to fix this problem?

Regards,

Charles






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Before close and workbook open! Marc[_20_] Excel Programming 1 March 17th 05 01:06 PM
How can I close only 1 workbook when I have many open? jpt consulting Excel Discussion (Misc queries) 3 November 30th 04 11:58 PM
Open, print and close workbook despistado[_3_] Excel Programming 0 November 11th 04 03:02 PM
Close Open Workbook David Coleman Excel Programming 1 December 30th 03 05:53 PM
Close Open Workbook Bob Umlas[_3_] Excel Programming 1 December 30th 03 05:50 PM


All times are GMT +1. The time now is 04:43 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"