workbook.close ERROR please help as pulling my hair out!! One for true Guru's!!

chopperj

New Member
Joined
May 5, 2010
Messages
11
Hi gang,

I have an error that i am at a loss how to fix / work around.
I have the following code that facilitates a user to select a folder, then reads the .xls file and writes them to a column then loops through each one and inserts some values. All quite striaght forward(!) then it closes the workbook and this is where the issue is.
As you can see i have tried different routes but get the same response which is either a 'object required' (for activeworkbook.close) or subscript out of range (for workbooks("filename").close)
i am really stuck, how difficult should closing a workbook be!!

note; that the files i am opening have code in them (including on_open and on_close)

i look forward to your responses





Sub clickfolderbutton()
Dim wbfp As String
Dim jmcpath As String


jmcpath = "" 'set varible

Call BrowseFolders("Please select the folder where the MM files are saved.") 'call windows API to browse folders

If jmcpath = "" Then End 'if canceled at API level then exit

If Right(jmcpath, 1) <> "\" Then jmcpath = jmcpath & "\" '

File_Search (jmcpath) ' this code goes to folder selected and finds all xls files then saves their name and path to column E


thiswb = ActiveWorkbook.Name

'below i loop for opening file and inserting values into set cells then save and close
For doctor = 1 To Range("e65536").End(xlUp).Row

wbfp = Workbooks(thiswb).Sheets("Administrator").Cells(doctor, 5).Value

Workbooks.Open wbfp

Sheets("index").Unprotect ("xxxxxxx")

Sheets("index").Range("d4").Select

ActiveCell.FormulaR1C1 = Workbooks(thiswb).Sheets("Administrator").Range("c1").Value

Sheets("index").Protect ("xxxxxxx"), DrawingObjects:=True, Contents:=True, Scenarios:=True

ActiveWorkbook.Unprotect ("zzzzzzzzzz")

Sheets("Admin").Visible = True
Sheets("Admin").Select
Sheets("Admin").Range("D55").Value = Workbooks(thiswb).Sheets("Administrator").Range("c3").Value

Sheets("Admin").Range("c55").Value = Workbooks(thiswb).Sheets("Administrator").Range("c5").Value

Sheets("Admin").Range("c56").Value = Workbooks(thiswb).Sheets("Administrator").Range("c6").Value
Sheets("Admin").Visible = xlVeryHidden

ActiveWorkbook.Protect ("zzzzzzzzzz")


'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Sheets("index").Select

currentf = ActiveWorkbook.Name

'Workbooks(currentf).Activate
'Set wkbk = ActiveWorkbook
'activeWorkbooks.Close

'Windows(currentf).Activate
'ActiveWindow.Close True

'closeallWB


Next


Workbooks(thiswb).Sheets("Administrator").Range("e:e").Value = ""

MsgBox "all completed!", vbOKOnly, "All done"


'Application.Quit
End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You had

'activeWorkbooks.Close

yes sorry was just an example, i have just found the issue! it was the before close code in the file i was opening but i was unaware as this file had its code password protected !! its still strange why it was erroring but now i have put Application.enableevents = false just before closing and the before close does not run and all works!! thank you for your help
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,256
Members
452,901
Latest member
LisaGo

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