Macro fails to close open workbook.

wwbwb

Well-known Member
Joined
Oct 20, 2003
Messages
513
Greetings,

I have a workbook that pulls information from one workbook and places it in another for production auditing.
I have an XP machine running Office 2003 and the code works perfectly.
My boss's machine is an XP running Office 2007 and it works perfectly.
Two techs have XP machines running Office 2007 and both of them fail at the same line.

Workbooks(importbook).Close

I can not figure out why this is occuring. Ideas?

Here is the full code:

<code>
Sub Import()

'This code will pull the PR of the indiviual and save a copy of it for use in this workbook.
'Created Mar 2013, Will Bland
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual

Dim mypath As String
Dim importbook As String

'Warning to user about possible length of time calculations may take.
MsgBox "The import may take a couple minutes.", vbExclamation, "Import Warning"

'Gets and sets PR name and location
importbook = Range("B2")
mypath = Range("l1").Value & Range("B2") & ".xls"

If Len(Dir(mypath)) = 0 Then GoTo error01:

'Preps worksheet for use
Workbooks("Roll Up.xls").Sheets("Previous").Range("A1:e65000").ClearContents

Workbooks.Open mypath

'Copies relevant data and copies to this workbook
Sheets("Previous").Select
Range("A1:E65000").Select
Selection.Copy
Workbooks("Roll Up.xls").Activate
Sheets("Previous").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Dim rng As Range
Dim rng2 As Range

Set rng = Range("d1:d" & Range("d" & Rows.Count).End(xlUp).Row)

For Each rng2 In rng
If rng2 = "x" Then
rng2.Offset(, -1).Value = "x"
End If
If rng2 = "X" Then
rng2.Offset(, -1).Value = "x"
End If

Next rng2

Workbooks(importbook).Close

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Sheets("Report").Select
Range("A1").Select

'Lets user know import and calculations are complete.
MsgBox "The import has finished.", vbInformation, "Import Complete"
Exit Sub
error01:
MsgBox "The PR you are trying to import from is missing.", vbCritical, "PR Missing"
Range("B2").Select
End Sub
</code>
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Immediately after:

Code:
[COLOR=#333333][FONT=monospace]Workbooks.Open mypath

insert:

Code:
Set WB1=ActiveWorkbook

then replace:

[/FONT][/COLOR]
Code:
[COLOR=#333333][FONT=monospace]Workbooks(importbook).Close

with:

Code:
WB1.Close[/FONT][/COLOR]
 
Upvote 0
That works. Thanks. Is there a reason why this works as opposed to the other method?
 
Upvote 0
Credit where it is due.

I just wanted to thank you Gary's Student for getting my brain working again.
I have been going rounds with a problem and your answer here got me kick started.
[h=3]Run-time error 32809 when workbook opens[/h]
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,375
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top