running sub-procedure from button malfunctions but running it from vba editor works fine?

binaya shahi

New Member
Joined
Oct 28, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
running sub-procedure from button malfunctions but running it from vba editor works fine?

this problem started when i protected all worksheet.
I wrote the following code to unprotect all the sheets at the beginning of sub-procedure
and again protect all worksheet in the end of sub-procedure.

Please help!

sub name()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Unprotect

code

Dim ws As Worksheet
For Each ws In Worksheets
ws.Unprotect

end sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
That couldnt work from a button or from the vba editor. Its missing a Next for both of the For statements. You also need protect not unprotect at the bottom.

VBA Code:
For Each ws In ThisWorkbook.Worksheets
        ws.Unprotect
Next
'code
For Each ws In ThisWorkbook.Worksheets
        ws.Protect
Next
 
Upvote 0
That couldnt work from a button or from the vba editor. Its missing a Next for both of the For statements. You also need protect not unprotect at the bottom.

VBA Code:
For Each ws In ThisWorkbook.Worksheets
        ws.Unprotect
Next
'code
For Each ws In ThisWorkbook.Worksheets
        ws.Protect
Next
actually there is Next in the code. I made mistake copying my code to the forum.
so missing Next is not the issue.

"This subprocedure working from vba editor but not working from the the button i created in worksheet" is the issue. what might be the reason?
 
Upvote 0
That couldnt work from a button or from the vba editor. Its missing a Next for both of the For statements. You also need protect not unprotect at the bottom.

VBA Code:
For Each ws In ThisWorkbook.Worksheets
        ws.Unprotect
Next
'code
For Each ws In ThisWorkbook.Worksheets
        ws.Protect
Next
actually there is Next in the code and ws.unprotect at the bottom of the code. So that is not the issue. its just my copypaste mistake.
"This subprocedure working from vba editor but not working from the the button i created in worksheet" is the issue. what might be the reason?
actually if i just remove ws.protect from bottom, this code works fine both from vba editor and excel button.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,602
Members
449,089
Latest member
Motoracer88

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