Worksheet Protection

From vb 6, I want to send a worksheet to the end and password protect it:

1. Line:    ws.Move after:=Sheets(Worksheets.Count)  errors: method 'Worksheets' of object '_Global' failed.     error: 1004

2. I need code to protect the same worksheet.

3. I need code to unprotect this worksheet to edit its content and then protect it again.

Thanks.




LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
 
ampapaConnect With a Mentor Commented:
Public Sub test()
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ActiveWorkbook
Set ws = wb.Worksheets("ABC")
ws.Move after:=Sheets(Worksheets.Count)

Sheets(Worksheets.Count).Protect Password:="YourPassword"


End Sub

Public Sub unprotect()
Sheets(Worksheets.Count).unprotect Password:="YourPassword"

End Sub


0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Because we are working from vb 6 not excel environmemt, I have:

Public Sub test()

Dim xlApp As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet

Set xlApp =GetObject..
Setwb=xlApp.Workbooks(strWb)
Set ws = wb.Worksheets("ABC")

ws.Move after:=Sheets(Worksheets.Count)          <-- this line still produces the error

Sheets(Worksheets.Count).Protect Password:="YourPassword"


End Sub
0
 
Erick37Commented:
Try

ws.Move after:=Sheets(xlApp.Worksheets.Count)
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Erick37,

I will try your solution and reopen the question for a proper split.

Thanks.
0
 
ampapaCommented:
Sorry, your correct I'm in Excel mode. Erick37 is correct that should fix your problem.
0
 
Erick37Connect With a Mentor Commented:
And the other...

ws.Move after:=xlApp.Sheets(xlApp.Worksheets.Count) '<-- Use xlApp to qualify your objects

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thanks Erick,

ws.Move after:=xlApp.Sheets(xlApp.Worksheets.Count)

Works.  Could you opent this question or I need to get assistance from an EE Admin.

Mike
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.