FileSearch使用set string

时间:2015-07-17 13:09:24

标签: vba excel-vba filesystems excel

我正在尝试获取VBA文件系统对象脚本来搜索设置文件路径,以根据filesearch的结果调用脚本。

所以脚本试图做的是:

  • 将搜索字符串设置为文件夹名称,文件名称(由数字单元格值设置)&扩展。
  • 运行搜索文件
  • 如果找不到结果,请调用脚本并循环直到找到匹配项
  • 找到匹配后,请调用另一个脚本

代码是序列号生成器,另一个脚本添加文本文件中的数字。然后调用此脚本以查看该数字是否存在(以具有匹配名称的excel文件的形式)。请参阅上面的步骤以查看脚本现在尝试执行的操作。

这是我到目前为止的代码。

Function CheckIFVoid()
  Dim myFile As String
  Dim fso, folder, file
  Dim folderName, searchFileName, renameFileTo

  folderName = "Z:\Delivery Notes\Delivery Notes\"
  searchFileName = Range("L5").Value
  myExtension = "*.xls"

  myFile = InStr(folderName & searchFileName, myExtension)

  ''Create FileSystem Objects
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set folder = fso.GetFolder(folderName)

  Do While myFile < 0
    Call MinusOne
    Sheets(1).Range("L5") = LowSeqNumber
  Loop
  If myFile = 1 Then
    Call PlusOne
    Sheets(1).Range("L5") = SeqNumber
  End If

ResetSettings:
  'Reset Macro Optimization Settings
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
End Function

我正在使用Excel 2013。

这就是我的旧代码看起来像我正在尝试重新创建的内容。

Sub FileSearch()

Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Dim myFileCount As Integer
Dim f  As Integer
Dim FILENAME As String
Dim FileType As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False

On Error Resume Next
    Set wbCodeBook = ThisWorkbook
        With Application.FileSearch
            .NewSearch
            .LookIn = "Z:\Delivery Notes\Delivery Notes\"
            .FileType = msoFileTypeExcelWorkbooks
            .FILENAME = Range("L5").Value

                myFileCount = 0
                MyFileName = .FoundFiles
                myFileCount = .FoundFiles.Count
                End With
End Sub
Function CheckIFVoid()

Dim wbResults As Workbook
Dim wbCodeBook As Workbook
Dim myFileCount As Integer
Dim f  As Integer
Dim FILENAME As String
Dim FileType As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False

On Error Resume Next
    Set wbCodeBook = ThisWorkbook
        With Application.FileSearch
            .NewSearch
            .LookIn = "Z:\Delivery Notes\Delivery Notes\"
            .FileType = msoFileTypeExcelWorkbooks
            .FILENAME = Range("L5").Value

               Do While .Execute < 1
                    Call MinusOne
                   Sheets(1).Range("L5") = LowSeqNumber
                  Call FileSearch
                  Loop

                If .Execute > 0 Then
                Call PlusOne
                Sheets(1).Range("L5") = SeqNumber

                End If
            End With
End Function

0 个答案:

没有答案