Goo Morning
(Using Excel 365)
I have a piece of code that reads a path and file name in column a then goes out and opens that file and copies a specific range. Then it reads a corresponding file name in column b goes out and opens that file and pastes the info if got from the file in column A. When someone has the file open in column B it puts that file name in column a of a different worksheet. This works very nicely except if the file is not present it does not list then name of the file and it closes the file down (the one with the code) and doesn't save the file. Any thoughts on this would be greatly appreciated.
Sub Post_7Months()
'
Application.ScreenUpdating = False
Sheets("7MONTHS").Select
'
Dim columnX As Range, cell As Range
Set columnX = Range("A2:A4")
Dim path1, path2 As String
Dim FileOpen As String
Dim ifilenum As Long
For Each cell In columnX
ChDir "S:\ReportingDepartment\ReportingAnalyst\Projects\REX\Original Report Data"
'copies current cell value into path1
path1 = cell.Value
Workbooks.Open Filename:=path1
Range("A2:M13").Select
Selection.Copy
Application.DisplayAlerts = False
ActiveWindow.Close
Application.DisplayAlerts = True
ChDir "S:\ReportingDepartment\ReportingAnalyst\Projects\REX\Reports To Be Sent"
'copies the vaule of the offset to thr right cell into path2
path2 = cell.Offset(0, 1).Value
'If Not IsFileOpen = path2 Then
'Workbooks.Open "C:\MyTest\volker2.xls"
On Error Resume Next
ifilenum = FreeFile()
Open path2 For Input Lock Read As #ifilenum
Close #ifilenum
Application.CutCopyMode = False
If Err.Number <> 70 Then 'file is close
Workbooks.Open Filename:=path2
Sheets("Detail").Select
Range("A5:M16").Select
Application.DisplayAlerts = False
ActiveSheet.PasteSpecial Format:="Unicode Text", Link:=False, _
DisplayAsIcon:=False, NoHTMLFormatting:=True
Application.DisplayAlerts = True
Range("B5:B17").Select
Selection.NumberFormat = "0"
Columns("B:B").EntireColumn.AutoFit
Range("A3").Select
Sheets("Summary").Select
ActiveWorkbook.Save
ActiveWindow.Close
Else
Sheets("Dashboard").Select
Range("A2").Select
'Range("d8").Value = path2
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = path2
'ActiveCell.PasteSpecial Paste:=xlPasteValues
End If
Next cell
'
'
Exit Sub
Application.DisplayAlerts = False
Range("E3").Value = "Process Complete"
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
Range("d8").Value = path2
ActiveCell.PasteSpecial Paste:=xlPasteValues
'
'
Resume Next ' go back to the line following the error
End Sub
(Using Excel 365)
I have a piece of code that reads a path and file name in column a then goes out and opens that file and copies a specific range. Then it reads a corresponding file name in column b goes out and opens that file and pastes the info if got from the file in column A. When someone has the file open in column B it puts that file name in column a of a different worksheet. This works very nicely except if the file is not present it does not list then name of the file and it closes the file down (the one with the code) and doesn't save the file. Any thoughts on this would be greatly appreciated.
Sub Post_7Months()
'
Application.ScreenUpdating = False
Sheets("7MONTHS").Select
'
Dim columnX As Range, cell As Range
Set columnX = Range("A2:A4")
Dim path1, path2 As String
Dim FileOpen As String
Dim ifilenum As Long
For Each cell In columnX
ChDir "S:\ReportingDepartment\ReportingAnalyst\Projects\REX\Original Report Data"
'copies current cell value into path1
path1 = cell.Value
Workbooks.Open Filename:=path1
Range("A2:M13").Select
Selection.Copy
Application.DisplayAlerts = False
ActiveWindow.Close
Application.DisplayAlerts = True
ChDir "S:\ReportingDepartment\ReportingAnalyst\Projects\REX\Reports To Be Sent"
'copies the vaule of the offset to thr right cell into path2
path2 = cell.Offset(0, 1).Value
'If Not IsFileOpen = path2 Then
'Workbooks.Open "C:\MyTest\volker2.xls"
On Error Resume Next
ifilenum = FreeFile()
Open path2 For Input Lock Read As #ifilenum
Close #ifilenum
Application.CutCopyMode = False
If Err.Number <> 70 Then 'file is close
Workbooks.Open Filename:=path2
Sheets("Detail").Select
Range("A5:M16").Select
Application.DisplayAlerts = False
ActiveSheet.PasteSpecial Format:="Unicode Text", Link:=False, _
DisplayAsIcon:=False, NoHTMLFormatting:=True
Application.DisplayAlerts = True
Range("B5:B17").Select
Selection.NumberFormat = "0"
Columns("B:B").EntireColumn.AutoFit
Range("A3").Select
Sheets("Summary").Select
ActiveWorkbook.Save
ActiveWindow.Close
Else
Sheets("Dashboard").Select
Range("A2").Select
'Range("d8").Value = path2
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
ActiveCell.Value = path2
'ActiveCell.PasteSpecial Paste:=xlPasteValues
End If
Next cell
'
'
Exit Sub
Application.DisplayAlerts = False
Range("E3").Value = "Process Complete"
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
Range("d8").Value = path2
ActiveCell.PasteSpecial Paste:=xlPasteValues
'
'
Resume Next ' go back to the line following the error
End Sub