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
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