Hi Suresh, Remove the 8th line from your macro .i.e. "Next". It's not required. Here is modified code which is shorter and more readable than before.
Tip: Always indent your program, it increase readability and hence error evaluations. Private Sub ok_Click() Dim a As Integer a = Val(text1.Text) If a = bp Then Application.ActiveSheet.Unprotect ActiveWorkbook.Unprotect UserForm1.Hide End If If a >= 2 Then 'Protects ALL worksheets ActiveWorkbook.Protect For Each shts In ThisWorkbook.Worksheets shts.Protect Next End If End Sub . . . . . For more tips, tricks, downloads and problem solutions: visit --> http://www.excelitems.com ------------------------------------------------------------------------ Regards *Ashish **Jain* Analyst, CSC Australia Microsoft Certified Application (Excel) Specialist Technical Trainer - Excel and VBA Author -> www.excelitems.com +91-9999-40-48-43 ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- On Mar 24, 8:34 pm, suresh k <brave.sur...@gmail.com> wrote: > Hi All, > > Can anyone check whether below codes are correct or not. > I am getting error message while i run this code. > > Private Sub ok_Click() > Dim a As Integer > a = Val(text1.Text) > > If a = bp Then > Application.ActiveSheet.Unprotect > ActiveWorkbook.Unprotect > UserForm1.Hide > Next > End If > > If a > 2 Then > 'Protects ALL worksheets > ActiveWorkbook.Protect > For Each shts In ThisWorkbook.Worksheets > shts.Protect > Next > End If > > If a = 0 Then > ActiveWorkbook.Protect > For Each shts In ThisWorkbook.Worksheets > shts.Protect > Next > End If > > End Sub --~--~---------~--~----~------------~-------~--~----~ ------------------------------------------------------------------------------------- Some important links for excel users: 1. Excel and VBA Tutorials(Video and Text), Free add-ins downloads at http://www.excelitems.com 2. Excel tutorials at http://www.excel-macros.blogspot.com 3. Learn VBA Macros at http://www.vbamacros.blogspot.com 4. Excel Tips and Tricks at http://exceldailytip.blogspot.com To post to this group, send email to excel-macros@googlegroups.com If you find any spam message in the group, please send an email to: Ayush Jain @ jainayus...@gmail.com or Ashish Jain @ 26may.1...@gmail.com ------------------------------------------------------------------------------------- -~----------~----~----~----~------~----~------~--~---