I am a new member to the forum. I've looked through the various threads, but can't find a solution to my current problem. I have a query in one workbook that updates from a table in another password protected workbook. A password is required both to open and modify the workbook. I need VBA code to open the password protected workbook, update the query, and then close the workbook, after the query has refreshed.
I have got as far as opening the workbook and refreshing the query. The two areas I need help with are:
1. what is the code to get past the modify password?
2. what is the code to close the workbook only after the query has refreshed? (I can close the workbook, but it does it before the refresh is complete and the refresh therefore fails)
Obviously I have access to all passwords.
Code extract
![]()
Dim cn As WorkbookConnection Dim Pass As Variant, wb1 As Workbook PathName = Workbooks("File").Sheets("Sheet").Range("name") Filename = Workbooks("File").Sheets("Sheet").Range("name") Pass = Workbooks("File").Sheets("Sheet").Range("name") On Error Resume Next Set wb1 = Application.Workbooks(Filename) If wb1 Is Nothing Then Workbooks.Open Filename:=PathName & Filename, Password:=Pass Set wb1 = Application.Workbooks(Filename) End If wb1.Activate For Each cn In ThisWorkbook.Connections If cn = "Query - DataList" Then cn.Refresh Next cn
Bookmarks