I have an application that reads a CSV file into an Execel table.
Here is my code. It works on my machine. With a correct file path and file name, I skip over the msgbox and compute on. With a deliberately bad file name, the code jumps into the if statement, I get the message box and the subroutine exits. Perfect execution. The offending line is highlighted in red.
The problem is that two of my co-workers are not getting this code to work. Instead of NOT finding the file (they may or may not have access to the directory) they get the error message Run-time error '52' Bad file name or number. Now, could this be that I can't even test if a file name exists if they don't have access to the server? If so, how does one test that one can't reach a file on the server?
Yes, I can attach the files, but I'll have to do a lot of work to the data to desensitize it.
Sub GetData()
Dim PathName As String ' Path name to source data
Dim FileData As String ' File name for datafile
Dim FileTran As String ' File name for transaction translations
Dim xls As Workbook ' Sourve Workbook
Dim shS As Worksheet ' Source File
Dim LRowS As Long ' Source File last row
Dim shD As Worksheet ' Destination worksheet
' initialize variables
PathName = Range("PathName")
FileData = Range("FileData")
FileTran = Range("FileTran")
' Check that the files exist
If Dir(PathName & "\" & FileData) = "" Then
MsgBox "Data source file does not exist." & Chr(10) & _
"Current data may not be accurate.", vbOKOnly, "File Does Not Exist"
Exit Sub
End If
If Dir(PathName & "\" & FileTran) = "" Then
MsgBox "Transaction lookup source file does not exist." & Chr(10) & _
"Current data may not be accurate.", vbOKOnly, "File Does Not Exist"
Exit Sub
End If
Application.ScreenUpdating = False
Application.Calculation = xlManual
Application.StatusBar = "Updating Data"
' Update the data file
ClearTable "Data", "Table_Data"
Set shD = Sheets("Data")
Set xls = Workbooks.Open(PathName & "\" & FileData)
Set shS = Sheets(1)
LRowS = shS.Range("A" & Rows.Count).End(xlUp).Row
shS.Range("$A$2:$H$" & LRowS).Copy shD.Range("A2")
xls.Close savechanges:=False
' Update the lookup file
ClearTable "Service Transactions", "Table_Transactions"
Set shD = Sheets("Service Transactions")
Set xls = Workbooks.Open(PathName & "\" & FileTran)
Set shS = Sheets(1)
LRowS = shS.Range("C" & Rows.Count).End(xlUp).Row
shS.Range("$C$2:$D$" & LRowS).Copy shD.Range("A2")
xls.Close savechanges:=False
DoEvents
'ClearPivot
Application.StatusBar = False
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
End Sub
Bookmarks