標題:
[發問]
開啟資料夾檔案名稱前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 編輯
Option Explicit
Option Compare Text
#If VBA7 Then
Private Declare PtrSafe Function PathRemoveBackslashW Lib "shlwapi.dll" (ByVal pszPath As LongPtr) As Long
Private Declare PtrSafe Function PathRemoveExtension Lib "shlwapi.dll" Alias "PathRemoveExtensionW" (ByVal pszPath As LongPtr) As Long
Private Declare PtrSafe Function PathStripPath Lib "shlwapi.dll" Alias "PathStripPathW" (ByVal pszPath As LongPtr) As Long
#Else
Private Declare Function PathRemoveBackslashW Lib "shlwapi.dll" (ByVal pszPath As Long) As Long
Private Declare Function PathRemoveExtension Lib "shlwapi.dll" Alias "PathRemoveExtensionW" (ByVal pszPath As Long) As Long
Private Declare Function PathStripPath Lib "shlwapi.dll" Alias "PathStripPathW" (ByVal pszPath As Long) As Long
#End If
'使用FSO搜索文件或是文件夹
Public Function FSOFileSearch(Optional ByVal SearchPath As String, _
Optional ByVal objFolder As Object = Nothing, _
Optional ByVal SearchName As String = "*.*", _
Optional ByVal SearchSub As Boolean = True, _
Optional ByVal SearchType As Long = 1) As Collection
Dim FSO As Object
Dim Filter() As String
Dim subSearch As Collection
Dim I As Long, J As Long
Dim objSub As Object
1 Set FSOFileSearch = New Collection
2 If objFolder Is Nothing Then
3 If Len(SearchPath) = 0 Then Exit Function
4 Set FSO = CreateObject("Scripting.FileSystemObject")
5 Set objFolder = FSO.GetFolder(SearchPath)
6 Set FSO = Nothing
7 If objFolder Is Nothing Then Exit Function
8 End If
9 Filter = Split(Replace(SearchName, "|", vbNullChar), vbNullChar)
10 If Len(SearchPath) Then
11 For I = 0 To UBound(Filter)
12 Filter(I) = LCase$(Trim$(Filter(I)))
13 If Len(Filter(I)) = 0 Then
14 If I = UBound(Filter) Then Exit For
15 For J = I + 1 To UBound(Filter)
16 If Len(Filter(J)) Then
17 Filter(I) = Filter(J)
18 Filter(J) = vbNullString
19 I = J - 1
20 Exit For
21 End If
22 Next J
23 If J > UBound(Filter) Then Exit For
24 End If
25 Next I
26 ReDim Preserve Filter(I - 1)
27 SearchName = Join(Filter, vbNullChar)
28 End If
29 If SearchType And 1& Then
30 For Each objSub In objFolder.Files
31 With objSub
32 For I = 0 To UBound(Filter)
33 If LCase(.Name) Like Filter(I) Then
34 FSOFileSearch.Add .Path
35 Exit For
36 End If
37 Next I
38 End With
39 Next objSub
40 End If
41 If SearchType And 2& Then
42 For Each objSub In objFolder.SubFolders
43 With objSub
44 For I = 0 To UBound(Filter)
45 If LCase(.Name) Like Filter(I) Then
46 FSOFileSearch.Add .Path
47 Exit For
48 End If
49 Next I
50 End With
51 Next objSub
52 End If
53 If SearchSub Then
54 For Each objSub In objFolder.SubFolders
55 DoEvents
56 Set subSearch = FSOFileSearch(, objSub, SearchName, SearchSub, SearchType)
57 With subSearch
58 For J = 1 To .Count
59 FSOFileSearch.Add .Item(J)
60 Next J
61 End With
62 Set subSearch = Nothing
63 Next objSub
64 End If
End Function
Public Function ExtractFileName(ByVal strPath As String, Optional ByVal ExtensionReturn As Boolean = True) As String
Dim I As Long, J As Long
strPath = strPath & String(10, vbNullChar)
J = InStr(strPath, vbNullChar)
PathRemoveBackslashW StrPtr(strPath)
If InStr(strPath, vbNullChar) <> J Then ExtensionReturn = True
PathStripPath StrPtr(strPath)
If Not ExtensionReturn Then PathRemoveExtension StrPtr(strPath)
I = InStr(strPath, vbNullChar)
If I > 0 Then strPath = Left$(strPath, I - 1)
ExtractFileName = strPath
End Function
Sub TestOpenFiles()
Dim I As Long
Dim Search As Collection
Dim FileName As String
'搜索当前文件所在目录中及其所有子目录下的.XLSX、.XLSM、.XLS、.XLSB文件
Set Search = FSOFileSearch("\\Pcbfs02\c740\檢核表\", , "*.XLS[XMB]|*.XLS",False)
For I = 1 To Search.Count
FileName = ExtractFileName(Search(I), False)
If FileName Like ((Sheet1.Range("E3").Value) & "*") Then Workbooks.Open FileName:=Search(I)
Next I
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
試試看
Option Explicit
Sub Ex()
Dim xPath As String, xFile As String
xPath = "D:\Pcbfs02\c740\檢核表\" '請更正為你要的資料夾
xFile = Dir(xPath & [A1] & "*.XL*")
Do Until xFile = ""
Workbooks.Open xPath & xFile
xFile = Dir
Loop
'Dir 函數 傳回一個 String ,用以表示合乎條件、檔案屬性、磁碟標記的一個檔案名稱、或目錄、檔案夾名稱。
' 語法 Dir [(pathname[, attributes])]
End Sub
複製代碼
作者:
rouber590324
時間:
2016-12-27 13:19
DEAR Joforn & GBKEE 大大
感謝指導.100%符合需求 THANKS ROBERT 12/27
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)