orsettgreenboy
New Member
- Joined
- Sep 20, 2011
- Messages
- 22
Hi there, I have the below code which does several things just fine, but now I have to change it because the destination file I'm copying to needs to be password protected by me.
I've uploaded a picture of the type of password (will be the one to modify) here:
https://docs.google.com/leaf?id=0B5...OWRlOC00M2VmLWIyODMtYzNjZDEyNjBjYjgy&hl=en_US
I don't really know anything about VBA and so I was guessing where/ how to do this, but my attempts below have failed. I have made the font red where my attempts were added below.
Hope someone can help
Thank you
Orsettgreenboy
I've uploaded a picture of the type of password (will be the one to modify) here:
https://docs.google.com/leaf?id=0B5...OWRlOC00M2VmLWIyODMtYzNjZDEyNjBjYjgy&hl=en_US
I don't really know anything about VBA and so I was guessing where/ how to do this, but my attempts below have failed. I have made the font red where my attempts were added below.
Hope someone can help
Thank you
Orsettgreenboy
Code:
Option Explicit
Sub SvMe() 'Generate next number to B5 and Save filename as value of G9 then save onto P drive and email copy to me
Sheet1.Unprotect Password:="Monkey"
Range("B5") = Range("B5") + 1
Sheet1.Protect Password:="Monkey"
ActiveWorkbook.Save
Dim newFile As String, fName As String
fName = Range("G9").Value
newFile = fName
ActiveWorkbook.SaveAs FileName:="P:\Quality\Non Conformances\" & newFile, FileFormat:=IIf(Application.Version >= "12", 56, -4143)
Dim wb As Workbook
Dim I As Long
Set wb = ActiveWorkbook
If Val(Application.Version) >= 12 Then
If wb.FileFormat = 51 And wb.HasVBProject = True Then
MsgBox "There is VBA code in this xlsx file, there will" & vbNewLine & _
"be no VBA code in the file you send. Save the" & vbNewLine & _
"file first as xlsm and then try the macro again.", vbInformation
Exit Sub
End If
End If
On Error Resume Next
For I = 1 To 3
wb.SendMail "rg@xxx", _
fName
If Err.Number = 0 Then Exit For
Next I
On Error GoTo 0
Dim wkb As Workbook, wks As Worksheet, LastRow As Long
Dim FilePath As String, FileName As String
Dim ws As Worksheet, blnOpened As Boolean
FilePath = "P:\Quality\"
FileName = "Non Conformance Log.xls"
Call ToggleEvents(False)
Set ws = ThisWorkbook.Sheets("Input")
If WbOpen(FileName) = True Then
Set wkb = Workbooks(FileName)
blnOpened = False
Else
If Right(FilePath, 1) <> Application.PathSeparator Then
FilePath = FilePath & Application.PathSeparator
End If
Set wkb = Workbooks.Open(FilePath & FileName)
blnOpened = True
End If
Set wks = wkb.Sheets("Sheet1")
[COLOR=red]Sheet1.Unprotect Password:="Monkey"[/COLOR]
LastRow = wks.Cells.Find(what:="*", after:=wks.Cells(1, 1), searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
wks.Cells(LastRow, "C").Value = ws.Cells(5, "B").Value
wks.Cells(LastRow, "D").Value = ws.Cells(7, "B").Value
wks.Cells(LastRow, "E").Value = ws.Cells(9, "B").Value
wks.Cells(LastRow, "F").Value = ws.Cells(11, "B").Value
wks.Cells(LastRow, "G").Value = ws.Cells(13, "B").Value
wks.Cells(LastRow, "H").Value = ws.Cells(15, "B").Value
wks.Cells(LastRow, "I").Value = ws.Cells(17, "B").Value
wks.Cells(LastRow, "J").Value = ws.Cells(19, "B").Value
wks.Cells(LastRow, "K").Value = ws.Cells(21, "B").Value
wks.Cells(LastRow, "L").Value = ws.Cells(23, "B").Value
wks.Cells(LastRow, "M").Value = ws.Cells(25, "B").Value
wks.Cells(LastRow, "N").Value = ws.Cells(27, "B").Value
wks.Cells(LastRow, "O").Value = ws.Cells(29, "B").Value
wks.Cells(LastRow, "P").Value = ws.Cells(31, "B").Value
wks.Cells(LastRow, "Q").Value = ws.Cells(33, "B").Value
wks.Cells(LastRow, "B").Value = ws.Cells(9, "G").Value
If blnOpened = True Then
wkb.Close SaveChanges:=True
End If
[COLOR=red]Sheet1.Protect Password:="Monkey"[/COLOR]
Call ToggleEvents(True)
End Sub
Sub ToggleEvents(blnState As Boolean)
'Originally written by firefytr
With Application
.DisplayAlerts = blnState
.EnableEvents = blnState
.ScreenUpdating = blnState
If blnState Then .CutCopyMode = False
If blnState Then .StatusBar = False
End With
End Sub
Function WbOpen(wbName As String) As Boolean
'Originally found written by Jake Marx
On Error Resume Next
WbOpen = Len(Workbooks(wbName).Name)
End Function