Macro / VBA

Hoube78

New Member
Joined
Mar 27, 2014
Messages
43
Hi,

I have the below macro in my workbook however due to increased number of SQL queries and the fact I need to enter the password for them all it appears the macro locks the sheets before the data is refreshed.

Excel pops up with a message that says the data can not be refreshed as the sheet is protected.

Sub UnprotectRefreshAll()
Dim ws As Worksheet
On Error Resume Next


For Each ws In ActiveWorkbook.Worksheets
ws.Unprotect Password:="MM@123.reg.com"
Next ws




ActiveWorkbook.RefreshAll




For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:="MM@123.reg.com", _
DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFiltering:=True, AllowUsingPivotTables:=True
Next ws

End Sub

Does any one know away to not run the part in blue until I have entered the password for all the SQL connections / data refreshes?

Thank you in advance.

John
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
See if the time delay will help.
Code:
Sub UnprotectRefreshAll()
 Dim ws As Worksheet, p As Double
 On Error Resume Next
 For Each ws In ActiveWorkbook.Worksheets
 ws.Unprotect Password:="MM@123.reg.com"
 Next ws
 ActiveWorkbook.RefreshAll
p = Timer + 2
Do While Timer < p
	DoEvents
Loop
For Each ws In ActiveWorkbook.Worksheets
 ws.Protect Password:="MM@123.reg.com", _
 DrawingObjects:=True, Contents:=True, Scenarios:=True _
 , AllowFiltering:=True, AllowUsingPivotTables:=True
 Next ws
 End Sub
You can increase or decrease the time span by changing the value of the 2. Whole numers are in seconds, but you can use .1 to .9 or .01 to .99 for tenths and hundreths of a second if needed.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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