Board logo

標題: [發問] 開啟資料夾檔案名稱前7碼相同之檔案 [打印本頁]

作者: rouber590324    時間: 2016-12-23 11:26     標題: 開啟資料夾檔案名稱前7碼相同之檔案

DEAR ALL 大大
1.如下程式為開啟\\Pcbfs02\c740\檢核表\下之檔案名稱= Sheet1.Range("E3")之程式碼
ChDir "\\Pcbfs02\c740\檢核表"
Workbooks.Open Filename:="\\Pcbfs02\c740\檢核表\" & Sheet1.Range("E3") & ".xls"
2.今需求為開啟於\\Pcbfs02\c740\檢核表\下之檔案名稱前7碼=Sheet1.Range("E3")之檔案
2.1例 : Sheet1.Range("E3")=S16021A 則開啟於\\Pcbfs02\c740\檢核表\下之檔案名稱前7碼=S16021A之檔案
  2.1.1 \\Pcbfs02\c740\檢核表\下之檔案名稱前7碼=S16021A之檔案只有唯一.但可能為 XXXXXXX-AA.XLS OR XXXXXXX-BBD.XLS
           第8碼後之名稱不一定
3.煩不吝賜教  THANKS*10000
作者: Joforn    時間: 2016-12-23 21:55

本帖最後由 Joforn 於 2016-12-23 21:58 編輯
  1. Option Explicit
  2. Option Compare Text

  3. #If VBA7 Then
  4.   Private Declare PtrSafe Function PathRemoveBackslashW Lib "shlwapi.dll" (ByVal pszPath As LongPtr) As Long
  5.   Private Declare PtrSafe Function PathRemoveExtension Lib "shlwapi.dll" Alias "PathRemoveExtensionW" (ByVal pszPath As LongPtr) As Long
  6.   Private Declare PtrSafe Function PathStripPath Lib "shlwapi.dll" Alias "PathStripPathW" (ByVal pszPath As LongPtr) As Long
  7. #Else
  8.   Private Declare Function PathRemoveBackslashW Lib "shlwapi.dll" (ByVal pszPath As Long) As Long
  9.   Private Declare Function PathRemoveExtension Lib "shlwapi.dll" Alias "PathRemoveExtensionW" (ByVal pszPath As Long) As Long
  10.   Private Declare Function PathStripPath Lib "shlwapi.dll" Alias "PathStripPathW" (ByVal pszPath As Long) As Long
  11. #End If

  12. '使用FSO搜索文件或是文件夹
  13. Public Function FSOFileSearch(Optional ByVal SearchPath As String, _
  14.          Optional ByVal objFolder As Object = Nothing, _
  15.          Optional ByVal SearchName As String = "*.*", _
  16.          Optional ByVal SearchSub As Boolean = True, _
  17.          Optional ByVal SearchType As Long = 1) As Collection
  18.         Dim FSO       As Object
  19.         Dim Filter()  As String
  20.         Dim subSearch As Collection
  21.         Dim I As Long, J As Long
  22.         Dim objSub    As Object
  23.         
  24. 1       Set FSOFileSearch = New Collection
  25. 2       If objFolder Is Nothing Then
  26. 3         If Len(SearchPath) = 0 Then Exit Function
  27. 4         Set FSO = CreateObject("Scripting.FileSystemObject")
  28. 5         Set objFolder = FSO.GetFolder(SearchPath)
  29. 6         Set FSO = Nothing
  30. 7         If objFolder Is Nothing Then Exit Function
  31. 8       End If
  32.         
  33. 9       Filter = Split(Replace(SearchName, "|", vbNullChar), vbNullChar)
  34. 10      If Len(SearchPath) Then
  35. 11        For I = 0 To UBound(Filter)
  36. 12          Filter(I) = LCase$(Trim$(Filter(I)))
  37. 13          If Len(Filter(I)) = 0 Then
  38. 14            If I = UBound(Filter) Then Exit For
  39. 15            For J = I + 1 To UBound(Filter)
  40. 16              If Len(Filter(J)) Then
  41. 17                Filter(I) = Filter(J)
  42. 18                Filter(J) = vbNullString
  43. 19                I = J - 1
  44. 20                Exit For
  45. 21              End If
  46. 22            Next J
  47. 23            If J > UBound(Filter) Then Exit For
  48. 24          End If
  49. 25        Next I
  50. 26        ReDim Preserve Filter(I - 1)
  51. 27        SearchName = Join(Filter, vbNullChar)
  52. 28      End If
  53.         
  54. 29      If SearchType And 1& Then
  55. 30        For Each objSub In objFolder.Files
  56. 31          With objSub
  57. 32            For I = 0 To UBound(Filter)
  58. 33              If LCase(.Name) Like Filter(I) Then
  59. 34                FSOFileSearch.Add .Path
  60. 35                Exit For
  61. 36              End If
  62. 37            Next I
  63. 38          End With
  64. 39        Next objSub
  65. 40      End If
  66.         
  67. 41      If SearchType And 2& Then
  68. 42        For Each objSub In objFolder.SubFolders
  69. 43          With objSub
  70. 44            For I = 0 To UBound(Filter)
  71. 45              If LCase(.Name) Like Filter(I) Then
  72. 46                FSOFileSearch.Add .Path
  73. 47                Exit For
  74. 48              End If
  75. 49            Next I
  76. 50          End With
  77. 51        Next objSub
  78. 52      End If
  79.         
  80. 53      If SearchSub Then
  81. 54        For Each objSub In objFolder.SubFolders
  82. 55          DoEvents
  83. 56          Set subSearch = FSOFileSearch(, objSub, SearchName, SearchSub, SearchType)
  84. 57          With subSearch
  85. 58            For J = 1 To .Count
  86. 59              FSOFileSearch.Add .Item(J)
  87. 60            Next J
  88. 61          End With
  89. 62          Set subSearch = Nothing
  90. 63        Next objSub
  91. 64      End If
  92. End Function

  93. Public Function ExtractFileName(ByVal strPath As String, Optional ByVal ExtensionReturn As Boolean = True) As String
  94.   Dim I As Long, J As Long
  95.   
  96.   strPath = strPath & String(10, vbNullChar)
  97.   J = InStr(strPath, vbNullChar)
  98.   PathRemoveBackslashW StrPtr(strPath)
  99.   If InStr(strPath, vbNullChar) <> J Then ExtensionReturn = True
  100.   PathStripPath StrPtr(strPath)
  101.   If Not ExtensionReturn Then PathRemoveExtension StrPtr(strPath)
  102.   I = InStr(strPath, vbNullChar)
  103.   If I > 0 Then strPath = Left$(strPath, I - 1)
  104.   ExtractFileName = strPath
  105. End Function

  106. Sub TestOpenFiles()
  107.   Dim I         As Long
  108.   Dim Search    As Collection
  109.   Dim FileName  As String
  110.   '搜索当前文件所在目录中及其所有子目录下的.XLSX、.XLSM、.XLS、.XLSB文件
  111.   Set Search = FSOFileSearch("\\Pcbfs02\c740\檢核表\", , "*.XLS[XMB]|*.XLS",False)
  112.   For I = 1 To Search.Count
  113.     FileName = ExtractFileName(Search(I), False)
  114.     If FileName Like ((Sheet1.Range("E3").Value) & "*") Then Workbooks.Open FileName:=Search(I)
  115.   Next I
  116. End Sub
複製代碼

作者: Joforn    時間: 2016-12-23 22:01

注意:
一、如果需要搜索所有的子目录,请将Set Search = FSOFileSearch("\\Pcbfs02\c740\檢核表\", , "*.XLS[XMB]|*.XLS",False)最后一个参数去掉或使用True,即:Set Search = FSOFileSearch("\\Pcbfs02\c740\檢核表\", , "*.XLS[XMB]|*.XLS")
二、示例中代码只搜索xls、xlsm、xlsx、xlsb类型文档,如果有其它的类型请自己添加相应的后缀。
三、代码未测试,如果有错误,请自行处理。
作者: GBKEE    時間: 2016-12-24 08:34

回復 1# rouber590324
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim xPath As String, xFile As String
  4.     xPath = "D:\Pcbfs02\c740\檢核表\"  '請更正為你要的資料夾
  5.     xFile = Dir(xPath & [A1] & "*.XL*")
  6.     Do Until xFile = ""
  7.         Workbooks.Open xPath & xFile
  8.         xFile = Dir
  9.     Loop
  10.     'Dir 函數 傳回一個 String ,用以表示合乎條件、檔案屬性、磁碟標記的一個檔案名稱、或目錄、檔案夾名稱。
  11.    ' 語法 Dir [(pathname[, attributes])]
  12. End Sub
複製代碼

作者: rouber590324    時間: 2016-12-27 13:19

DEAR  Joforn & GBKEE  大大
感謝指導.100%符合需求  THANKS   ROBERT 12/27




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)