ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

[µo°Ý] ¶}±Ò¸ê®Æ§¨ÀɮצWºÙ«e7½X¬Û¦P¤§ÀÉ®×

[µo°Ý] ¶}±Ò¸ê®Æ§¨ÀɮצWºÙ«e7½X¬Û¦P¤§ÀÉ®×

DEAR ALL ¤j¤j
1.¦p¤Uµ{¦¡¬°¶}±Ò\\Pcbfs02\c740\Àˮ֪í\¤U¤§ÀɮצWºÙ= Sheet1.Range("E3")¤§µ{¦¡½X
ChDir "\\Pcbfs02\c740\Àˮ֪í"
Workbooks.Open Filename:="\\Pcbfs02\c740\Àˮ֪í\" & Sheet1.Range("E3") & ".xls"
2.¤µ»Ý¨D¬°¶}±Ò©ó\\Pcbfs02\c740\Àˮ֪í\¤U¤§ÀɮצWºÙ«e7½X=Sheet1.Range("E3")¤§ÀÉ®×
2.1¨Ò : Sheet1.Range("E3")=S16021A «h¶}±Ò©ó\\Pcbfs02\c740\Àˮ֪í\¤U¤§ÀɮצWºÙ«e7½X=S16021A¤§ÀÉ®×
  2.1.1 \\Pcbfs02\c740\Àˮ֪í\¤U¤§ÀɮצWºÙ«e7½X=S16021A¤§ÀÉ®×¥u¦³°ß¤@.¦ý¥i¯à¬° XXXXXXX-AA.XLS OR XXXXXXX-BBD.XLS
           ²Ä8½X«á¤§¦WºÙ¤£¤@©w
3.·Ð¤£§[½ç±Ð  THANKS*10000
ù

¥»©«³Ì«á¥Ñ Joforn ©ó 2016-12-23 21:58 ½s¿è
  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·j¯Á¤å¥ó©Î¬O¤å¥ó夹
  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.   '·j¯Á当«e¤å¥ó©Ò¦b¥Ø录¤¤¤Î¨ä©Ò¦³¤l¥Ø录¤Uªº.XLSX¡B.XLSM¡B.XLS¡B.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
½Æ»s¥N½X
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

ª`·N¡G
¤@¡B¦pªG»Ý­n·j¯Á©Ò¦³ªº¤l¥Ø录¡A请将Set Search = FSOFileSearch("\\Pcbfs02\c740\Àˮ֪í\", , "*.XLS[XMB]|*.XLS",False)³Ì¦Z¤@个参数¥h±¼©Î¨Ï¥ÎTrue¡A§Y¡GSet Search = FSOFileSearch("\\Pcbfs02\c740\Àˮ֪í\", , "*.XLS[XMB]|*.XLS")
¤G¡B¥Ü¨Ò¤¤¥N码¥u·j¯Áxls¡Bxlsm¡Bxlsx¡Bxlsb类«¬¤å档¡A¦pªG¦³¨ä¥¦ªº类«¬请¦Û¤v²K¥[¬Û应ªº¦Z缀¡C
¤T¡B¥N码¥¼测试¡A¦pªG¦³错误¡A请¦Û¦æ处²z¡C
¥@¬É¨º»ò¤j¡A¥i§Ú·Q¥h­þ¡H

TOP

¦^´_ 1# rouber590324
¸Õ¸Õ¬Ý
  1. Option Explicit
  2. Sub Ex()
  3.     Dim xPath As String, xFile As String
  4.     xPath = "D:\Pcbfs02\c740\Àˮ֪í\"  '½Ð§ó¥¿¬°§A­nªº¸ê®Æ§¨
  5.     xFile = Dir(xPath & [A1] & "*.XL*")
  6.     Do Until xFile = ""
  7.         Workbooks.Open xPath & xFile
  8.         xFile = Dir
  9.     Loop
  10.     'Dir ¨ç¼Æ ¶Ç¦^¤@­Ó String ¡A¥Î¥Hªí¥Ü¦X¥G±ø¥ó¡BÀÉ®×ÄÝ©Ê¡BºÏºÐ¼Ð°Oªº¤@­ÓÀɮצWºÙ¡B©Î¥Ø¿ý¡BÀɮק¨¦WºÙ¡C
  11.    ' »yªk Dir [(pathname[, attributes])]
  12. End Sub
½Æ»s¥N½X
·P®¦ªº¤ß......(¦b³Â»¶®a±Ú°Q½×°Ï.¥Î¤ß¾Ç²ß·|¦³¶i¨Bªº)
¦ý¸ê·½µL­­,«á´©¦³­­,  ¤@¤Ñ1¤¸ªºÃÙ§U,¤H¤H¦³¯à¤O.

TOP

DEAR  Joforn & GBKEE  ¤j¤j
·PÁ«ü¾É.100%²Å¦X»Ý¨D  THANKS   ROBERT 12/27
ù

TOP

        ÀR«ä¦Û¦b : ­ì½Ì§O¤H´N¬Oµ½«Ý¦Û¤v¡C
ªð¦^¦Cªí ¤W¤@¥DÃD