Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2002
    Location
    NJ
    Posts
    139

    Unanswered: error while trying to kill a file in excel

    Hi,

    I get an error when I run the routine bellow.

    strfpath = "\\USERSTATS\" & Range("C1").Value & ".xls"
    If Dir(strfpath) Then
    Kill strfpath
    End If

    the error is;

    Run-Time error '13'
    Type Mismatch

    When I click on debug, the
    "If Dir(strfpath) Then"
    is hilighted

    Note that the path is correct and when I bring the cursor on the highlited part, it gives me the entire path with the correct file name.

    Thank you for your anticipated help.
    Alexxx

  2. #2
    Join Date
    Feb 2004
    Posts
    533
    'Dir(strfpath)' is going to return a string value of the filename or an empty string so if true/false condition is ging to cause an error.

    Need to check for a string value.
    If Dir(strfpath) <> "" Then Kill strfpath

    or you could use this variation
    If Not Dir(strfpath) = "" Then kill strfpath


    another way is to use a function like:

    Sub killFile()
    Dim strfpath As String

    strfpath = "c:\" & Range("C1").Value & ".xls"

    If FileExists(strfpath) Then
    Kill strfpath
    Debug.Print "Killed the File " & Range("C1").Value & ".xls"
    End If
    End Sub


    Private Function FileExists(fname) As Boolean
    ' Returns TRUE if the file exists
    Dim x As String
    x = Dir(fname)
    If x <> "" Then FileExists = True _
    Else FileExists = False
    End Function
    ~

    Bill

Posting Permissions

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