+ Reply to Thread
Results 1 to 2 of 2

If Dir(PathName & "\" & FileData) = "" Then - fails

Hybrid View

  1. #1
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    If Dir(PathName & "\" & FileData) = "" Then - fails

    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
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: If Dir(PathName & "\" & FileData) = "" Then - fails

    OK, I got confirmation from one of my co-workers. I moved the sources to where I know he can get at them and the code now works for him. So it is a matter of "Yes you can see it, but no you can't read it." So what I will have to do is trap error number 52 and issue a different warning message.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 02-05-2019, 12:03 AM
  2. [SOLVED] Column X-Ref list - Sheet1 Col A "pages", Col B:FL "Req" to Sheet2 ColA "req", ColB "page"
    By excel-card-pulled in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-07-2017, 09:30 AM
  3. [SOLVED] Why does the "CountIF" function fails on value "Monday Week 1"?
    By kashbg in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 06-02-2015, 03:24 PM
  4. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  5. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  6. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  7. Replies: 7
    Last Post: 05-13-2006, 05:02 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1