K
K
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:=2578,
recursive:=True).Execute
wb.Save
End Sub
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:=2578,
recursive:=True).Execute
wb.Save
End Sub