londoneye001
Board Regular
- Joined
- Sep 12, 2014
- Messages
- 56
All,
I'm currently running an existing macro and getting this error "Run-time error '52' Bad file name or number". When i'm trying to debug the error, arrow is pointing to this statement "Open PageName For Output As #1" I'm not sure how to fix this? Appreciate help here! Below please see a full coding behind Macro if it helps!
Thanks,
--------------------------------------------
Sub Parklane()
'
'
PageName = "\\hr\Parklane\HS\PARKLANE" & ".DAT"
FirstRow = Range("C1").Value
LastRow = Range("C2").Value
Open PageName For Output As #1
For MyRow = FirstRow To LastRow
MyStr = "" 'clear valiable
If Val(Cells(MyRow, 1).Value) > 99999999 And Val(Cells(MyRow, 1).Value) < 999999999 Then 'SIN
MyStr = MyStr & Val(Cells(MyRow, 1).Value)
Else
MyStr = MyStr & Left(Trim(Cells(MyRow, 1).Value) & String(9, " "), 9)
End If
MyStr = MyStr & "#" 'Record Identifier
Dim Dept1 As String, Dept2 As String
Dept1 = Cells(MyRow, 4).Value
Dept2 = Cells(MyRow, 5).Value
Do Until Len(Dept1) = 3
Dept1 = "0" & Dept1
Loop
Do Until Len(Dept2) = 3
Dept2 = "0" & Dept2
Loop
MyStr = MyStr & Left(Dept1 & Dept2 & String(10, " "), 10)
MyStr = MyStr & Left(Cells(MyRow, 6).Value & String(25, " "), 25) 'LastName
MyStr = MyStr & Left(Cells(MyRow, 7).Value & String(20, " "), 20) 'FirstName
MyStr = MyStr & Left(Cells(MyRow, 8).Value & String(30, " "), 30) 'Address
MyStr = MyStr & Left(Cells(MyRow, 9).Value & ", " & Cells(MyRow, 10).Value & String(30, " "), 30) 'City
MyStr = MyStr & Left(Cells(MyRow, 11).Value & String(7, " "), 7) 'PostalCode
If Val(Mid(Cells(MyRow, 12).Value, 2, 3) & Right(Cells(MyRow, 12).Value, 8)) > 999999999 Then 'AreaCode & Phone
MyStr = MyStr & Val(Mid(Cells(MyRow, 12).Value, 2, 3) & Right(Cells(MyRow, 12).Value, 8))
Else
MyStr = MyStr & String(10, " ")
End If
MyStr = MyStr & Left(Format(Cells(MyRow, 13).Value, "ddmmyyyy") & String(8, " "), 8) 'BirthDate
If Cells(MyRow, 14).Value = "Single" Then 'MaritalStatus
MyStr = MyStr & "S"
ElseIf Cells(MyRow, 14).Value = "Married" Then
MyStr = MyStr & "M"
ElseIf Cells(MyRow, 14).Value = "Divorced" Then
MyStr = MyStr & "D"
ElseIf Cells(MyRow, 14).Value = "Widowed" Then
MyStr = MyStr & "W"
ElseIf Cells(MyRow, 14).Value = "Separated" Then
MyStr = MyStr & "S"
ElseIf Cells(MyRow, 14).Value = "Common Law" Then
MyStr = MyStr & "C"
ElseIf Cells(MyRow, 14).Value = "Other" Then
MyStr = MyStr & "X"
Else
MyStr = MyStr & " "
End If
If Cells(MyRow, 15).Value = "Male" Then 'Gender
MyStr = MyStr & "M"
ElseIf Cells(MyRow, 15).Value = "Female" Then
MyStr = MyStr & "F"
End If
MyStr = MyStr & Left(Cells(MyRow, 16).Value & String(24, " "), 24) 'JobTitle
If Cells(MyRow, 17).Value = "Working" Then 'Status
Select Case Cells(MyRow, 65).Value
Case "FT"
MyStr = MyStr & "F"
Case "SS"
MyStr = MyStr & "S"
Case "PT"
MyStr = MyStr & "P"
Case "CO"
MyStr = MyStr & "C"
End Select
ElseIf Cells(MyRow, 17).Value = "Terminated - Voluntary" Then
MyStr = MyStr & "L"
ElseIf Cells(MyRow, 16).Value = "Terminated - Involuntary" Then
MyStr = MyStr & "T"
End If
MyStr = MyStr & String(296, " ")
Print #1, MyStr
Next
Close #1
'
End Sub
I'm currently running an existing macro and getting this error "Run-time error '52' Bad file name or number". When i'm trying to debug the error, arrow is pointing to this statement "Open PageName For Output As #1" I'm not sure how to fix this? Appreciate help here! Below please see a full coding behind Macro if it helps!
Thanks,
--------------------------------------------
Sub Parklane()
'
'
PageName = "\\hr\Parklane\HS\PARKLANE" & ".DAT"
FirstRow = Range("C1").Value
LastRow = Range("C2").Value
Open PageName For Output As #1
For MyRow = FirstRow To LastRow
MyStr = "" 'clear valiable
If Val(Cells(MyRow, 1).Value) > 99999999 And Val(Cells(MyRow, 1).Value) < 999999999 Then 'SIN
MyStr = MyStr & Val(Cells(MyRow, 1).Value)
Else
MyStr = MyStr & Left(Trim(Cells(MyRow, 1).Value) & String(9, " "), 9)
End If
MyStr = MyStr & "#" 'Record Identifier
Dim Dept1 As String, Dept2 As String
Dept1 = Cells(MyRow, 4).Value
Dept2 = Cells(MyRow, 5).Value
Do Until Len(Dept1) = 3
Dept1 = "0" & Dept1
Loop
Do Until Len(Dept2) = 3
Dept2 = "0" & Dept2
Loop
MyStr = MyStr & Left(Dept1 & Dept2 & String(10, " "), 10)
MyStr = MyStr & Left(Cells(MyRow, 6).Value & String(25, " "), 25) 'LastName
MyStr = MyStr & Left(Cells(MyRow, 7).Value & String(20, " "), 20) 'FirstName
MyStr = MyStr & Left(Cells(MyRow, 8).Value & String(30, " "), 30) 'Address
MyStr = MyStr & Left(Cells(MyRow, 9).Value & ", " & Cells(MyRow, 10).Value & String(30, " "), 30) 'City
MyStr = MyStr & Left(Cells(MyRow, 11).Value & String(7, " "), 7) 'PostalCode
If Val(Mid(Cells(MyRow, 12).Value, 2, 3) & Right(Cells(MyRow, 12).Value, 8)) > 999999999 Then 'AreaCode & Phone
MyStr = MyStr & Val(Mid(Cells(MyRow, 12).Value, 2, 3) & Right(Cells(MyRow, 12).Value, 8))
Else
MyStr = MyStr & String(10, " ")
End If
MyStr = MyStr & Left(Format(Cells(MyRow, 13).Value, "ddmmyyyy") & String(8, " "), 8) 'BirthDate
If Cells(MyRow, 14).Value = "Single" Then 'MaritalStatus
MyStr = MyStr & "S"
ElseIf Cells(MyRow, 14).Value = "Married" Then
MyStr = MyStr & "M"
ElseIf Cells(MyRow, 14).Value = "Divorced" Then
MyStr = MyStr & "D"
ElseIf Cells(MyRow, 14).Value = "Widowed" Then
MyStr = MyStr & "W"
ElseIf Cells(MyRow, 14).Value = "Separated" Then
MyStr = MyStr & "S"
ElseIf Cells(MyRow, 14).Value = "Common Law" Then
MyStr = MyStr & "C"
ElseIf Cells(MyRow, 14).Value = "Other" Then
MyStr = MyStr & "X"
Else
MyStr = MyStr & " "
End If
If Cells(MyRow, 15).Value = "Male" Then 'Gender
MyStr = MyStr & "M"
ElseIf Cells(MyRow, 15).Value = "Female" Then
MyStr = MyStr & "F"
End If
MyStr = MyStr & Left(Cells(MyRow, 16).Value & String(24, " "), 24) 'JobTitle
If Cells(MyRow, 17).Value = "Working" Then 'Status
Select Case Cells(MyRow, 65).Value
Case "FT"
MyStr = MyStr & "F"
Case "SS"
MyStr = MyStr & "S"
Case "PT"
MyStr = MyStr & "P"
Case "CO"
MyStr = MyStr & "C"
End Select
ElseIf Cells(MyRow, 17).Value = "Terminated - Voluntary" Then
MyStr = MyStr & "L"
ElseIf Cells(MyRow, 16).Value = "Terminated - Involuntary" Then
MyStr = MyStr & "T"
End If
MyStr = MyStr & String(296, " ")
Print #1, MyStr
Next
Close #1
'
End Sub