dincigneri
New Member
- Joined
- Jun 14, 2021
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
Hi all, I was wondering if anyone can help me jump this last hurdle in my first VBA project? As a complete newbie I have made good progress browsing this site and watching videos however cannot get this final bug fixed.
I have a workbook where all sheets are protected. On sheet13 however, is a table from an external data source that needs to be refreshed on when opening the file.
I have created a macro to unlock, refresh and lock (with password) that sheet only, and I have created another sub to trigger this macro when opening the file. I recorded the macro and then edited it - I typed in the password into the code as the password was not being recorded, and I believe I also changed 'ActiveSheet' to 'Sheet13' as I want to make sure the right sheet is updated (not knowing which 'activesheet' a user may open this shared file on). This worked for a single test on Friday arvo and I went to the weekend quite satisfied...
However today I and other users receive a '91' error on opening the file. In turn the macro doesn't run automatically. It will then execute fine when I hit F5 - which makes my testing very tricky - why is it only failing when opening the file (more to the point, when i send it to another user to test it for me - it always fails!).
The line of code that is the issue is marked below. Searching online, the fix for this error has led me on a path of defining and setting variables in my code, though I am not sure I need to do that? Will very much appreciate any insight anyone can provide here. Thanks
Error line:
Whole query:
I have a workbook where all sheets are protected. On sheet13 however, is a table from an external data source that needs to be refreshed on when opening the file.
I have created a macro to unlock, refresh and lock (with password) that sheet only, and I have created another sub to trigger this macro when opening the file. I recorded the macro and then edited it - I typed in the password into the code as the password was not being recorded, and I believe I also changed 'ActiveSheet' to 'Sheet13' as I want to make sure the right sheet is updated (not knowing which 'activesheet' a user may open this shared file on). This worked for a single test on Friday arvo and I went to the weekend quite satisfied...
However today I and other users receive a '91' error on opening the file. In turn the macro doesn't run automatically. It will then execute fine when I hit F5 - which makes my testing very tricky - why is it only failing when opening the file (more to the point, when i send it to another user to test it for me - it always fails!).
The line of code that is the issue is marked below. Searching online, the fix for this error has led me on a path of defining and setting variables in my code, though I am not sure I need to do that? Will very much appreciate any insight anyone can provide here. Thanks
Error line:
VBA Code:
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Whole query:
VBA Code:
Sub Unlock_Refresh_Lock()
Sheet13.Unprotect "password"
Range("G6").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Sheet13.Protect "password", DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowUsingPivotTables:=True
End Sub