View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Orion Cochrane Orion Cochrane is offline
external usenet poster
 
Posts: 119
Default Macro in UserForm

Just wondering why you would need a procedure to lock your project. Lock it
in VB under Tools VBAPriject Properties. Under the Protection tab, click
the Lock Project for Edititng checkbox and type in a password to lock your
project.
--
Please rate posts so we know when we have answered your questions. Thanks.


"K" wrote:

I have ComboBox and CommandButton on UserForm. I am trying that when
I click CommandButton on UserForm it should copy the Activesheet to
ComboBox.Value (which is Workbook.Name) and then protect its macro and
close that Workbook with saving all changes in it and then open it
again. I been struggling with the code below but for some reason i am
getting error. It copies the Activesheet to specified Workbook but
not protecting the macro of that sheet and also not opening it after
everything is done. Can please someone look my code below and tell
that what i am doing wrong.

Private Sub CommandButton1_Click()
cv = Me.ComboBox1.Value
ThisWorkbook.ActiveSheet.Copy
after:=Workbooks(cv).Sheets(Sheets.Count)
ProtectVBProject Workbooks(cv), "jack"
Workbooks(cv).Save
Unload UserForm1
Workbooks(cv).Close True
Workbooks.Open Filename:=Workbooks(cv)
End Sub


Sub ProtectVBProject(wb As Workbook, ByVal Password As String)
Dim vbProj As Object
Set vbProj = wb.VBProject
If vbProj.Protection = 1 Then Exit Sub
Set Application.VBE.ActiveVBProject = vbProj
SendKeys "+{TAB}{RIGHT}%V{+}{TAB}" & Password & "{TAB}" & Password &
"~"
Application.VBE.CommandBars(1).FindControl(ID:=257 8,
recursive:=True).Execute
wb.Save
End Sub