VBA用户function检查一个目录
以下是目前的代码
- 我经常需要检查一个采购订单是否已被保存在一个目录中,Excel中可能有数百个采购订单。
- 随着工作簿的变化,文件path也经常变化。
- 因此,我想做一个函数,要求一个单元格的值包含一个string的文件path,然后一个单元格的PO#。
我有点难以理解如何从Excel工作表中获取信息。 我需要一个到该目录的文件path的单元格引用,以及PO#的单元格引用。 我已经能够使这个工作与一个子程序,这是在下面发布。 这是我工作过的第三个VBA计划,请在发布之前告诉我是否应该做更多的工作:
Dim directory As String Dim TempfileName As String Dim i As Long Dim x As Long Sub Check_PO() x = 2 Application.ScreenUpdating = False For x = 2 To 673 While Cells(x, 14) = 0 x = x + 1 Wend i = Cells(x, 14) TempfileName = "\\network\file\name\here\" & "*" & i & "*.pdf" directory = Dir(TempfileName, vbNormal) While directory <> "" Cells(x, 18) = "Matched" directory = Dir Wend Next x End Sub
这是一个简单的UDF:
Public Function HaveReport(fPath As String, fileName As String) HaveReport = IIf(Dir(fPath & fileName, vbNormal) <> "", _ "Matched", "Not Matched") End Function
用法: