如何根据单元格值删除文件? [英] How can I delete a file based on cell value?

查看:45
本文介绍了如何根据单元格值删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法根据单元格值删除文件.我在下面的Kill命令中收到一条错误消息:

I'm having trouble deleting a file based on cell value. I get an error message on the line with the Kill command below:

Kill path & r.Offset(1, -4) & "\" & r.Offset(1, -3)

有什么想法吗?

Sub INACTIVE_files()

    Const path = "C:\Users\NikolouzosD\AppData\Local\Temp\vbakillfunction\"

    Dim r As Range
    Dim x As Integer

    Set r = Cells(1, 5)
    Do Until r = ""
        If UCase(r.value) = "INACTIVE" Then

            Kill path & r.Offset(1, -4) & "\" & r.Offset(1, -3)
          End If
          Set r = r.Offset(1, 0)
    Loop

End Sub

代码从单元格E1开始,并在同一列中查找INACTIVE文件,直到没有其他文件可查找为止.然后,它检查文件夹名称(A列),并将其与多维数据集(B列)合并并将它们都放在一个路径中:

The code starts from cell E1 and looks for INACTIVE files in the same column, until there's no more files to look for. Then, it checks the folder name (Column A), combines it with the Cube (Column B) and puts both of them in a path:

path = "C:\Users\NikolouzosD\AppData\Local\Temp\vbakillfunction\"

例如:对于无效的单元格E2,路径应为:

so for example: for cell E2 which is INACTIVE, the path should be:

C:\Users\NikolouzosD\AppData\Local\Temp\vbakillfunction\WPO 17 02 04 3MMT All Periods\BG023104.txt

然后从相应的文件夹中删除不活动的文件(多维数据集).

It then deletes the INACTIVE files (Cubes) from the appropriate folder.

推荐答案

它有效!我已经注释掉了用于检查文件是否存在的部分代码.

It works! I've commented out some parts of the code that were used for checking if a file exists.

 Sub delete_INACTIVE_files()


Const path = "C:\Users\Dn\AppData\Local\Temp\vbakillfunction\"
Dim r As Range


Set r = Cells(1, 5)

Do Until r = ""


    If UCase(r.Value) = "INACTIVE" Then

        If Dir(path & r.Offset(0, -4) & "\" & r.Offset(0, -3) & ".txt") <> "" Then 'Does the file exist?

            'MsgBox "file" & path & r.Offset(0, -4) & "\" & r.Offset(0, -3) & ".txt" & " exists"

            Kill path & r.Offset(0, -4) & "\" & r.Offset(0, -3) & ".txt"


        'Else

            'MsgBox "file" & path & r.Offset(0, -4) & "\" & r.Offset(0, -3) & ".txt" & " not here"


        End If

    End If

    Set r = r.Offset(1, 0)

Loop

End Sub

这篇关于如何根据单元格值删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆