orsettgreenboy
New Member
- Joined
- Sep 20, 2011
- Messages
- 22
Hi there
I'm getting an error when running a macro that says "Microsoft Visual Basic for Appli... 400."
The macro I'm running should generate the next sequential number and then the save the file into a shared folder on our network with a new file name based on certain cell criteria and also send me an email with a copy of the file. I have altered the email address in the code below for confidentiality reasons only.
There is a hidden list & some hidden & some locked cells (but unhiding these don't seem to make any difference). These are protected by a sheet password.
This has worked earlier (i can't see what has changed) and then today it only worked the first time i used the macro, and now it doesn't work at all giving the above mentioned error.
There is nothing highlighted in debug.
The VB Code is as below
Sub SvMe() 'Generate next number to B5 and Save filename as value of B5 plus the current date etc etc
Sheet1.Unprotect Password:="Monkey"
Range("B5") = Range("B5") + 1
Sheet1.Protect Password:="Monkey"
Dim newFile As String, fName As String, fName3 As Integer, fName2 As String, fName4 As String, fName5 As String, fName6 As String
' Don't use "/" in date, invalid syntax
fName = Range("G3").Text
fName2 = Range("G4").Text
fName3 = Range("B5").Value
fName4 = Range("G17").Text
fName5 = Range("I17").Text
fName6 = Range("G15").Text
newFile = fName & " " & Format(Now, "yyyymmdd") & " " & fName2 & fName3 & fName4 & " " & fName5 & " " & fName6
' Change directory to suit
ChDir _
"P:\Quality\Non Conformances" 'YOU MUST Change USER NAME to suit
ActiveWorkbook.SaveAs Filename:=newFile
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 "xx@xxxx.co.uk", _
"Non Conformance"
If Err.Number = 0 Then Exit For
Next I
On Error GoTo 0
End Sub
Really appreciate any help you can offer, but please note i'm a newbie to VB and the code above was compiled from various internet sources (so as it is not my own work so I can't really understand what each part is doing where the error might be)
Thanks
I'm getting an error when running a macro that says "Microsoft Visual Basic for Appli... 400."
The macro I'm running should generate the next sequential number and then the save the file into a shared folder on our network with a new file name based on certain cell criteria and also send me an email with a copy of the file. I have altered the email address in the code below for confidentiality reasons only.
There is a hidden list & some hidden & some locked cells (but unhiding these don't seem to make any difference). These are protected by a sheet password.
This has worked earlier (i can't see what has changed) and then today it only worked the first time i used the macro, and now it doesn't work at all giving the above mentioned error.
There is nothing highlighted in debug.
The VB Code is as below
Sub SvMe() 'Generate next number to B5 and Save filename as value of B5 plus the current date etc etc
Sheet1.Unprotect Password:="Monkey"
Range("B5") = Range("B5") + 1
Sheet1.Protect Password:="Monkey"
Dim newFile As String, fName As String, fName3 As Integer, fName2 As String, fName4 As String, fName5 As String, fName6 As String
' Don't use "/" in date, invalid syntax
fName = Range("G3").Text
fName2 = Range("G4").Text
fName3 = Range("B5").Value
fName4 = Range("G17").Text
fName5 = Range("I17").Text
fName6 = Range("G15").Text
newFile = fName & " " & Format(Now, "yyyymmdd") & " " & fName2 & fName3 & fName4 & " " & fName5 & " " & fName6
' Change directory to suit
ChDir _
"P:\Quality\Non Conformances" 'YOU MUST Change USER NAME to suit
ActiveWorkbook.SaveAs Filename:=newFile
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 "xx@xxxx.co.uk", _
"Non Conformance"
If Err.Number = 0 Then Exit For
Next I
On Error GoTo 0
End Sub
Really appreciate any help you can offer, but please note i'm a newbie to VB and the code above was compiled from various internet sources (so as it is not my own work so I can't really understand what each part is doing where the error might be)
Thanks