⑴ 大神《同一文件夾使用VBA遍歷所有excel表格並引用數據》 能否提供下實現的方法和步驟 教育用 感謝
用dir 和do loop 遍歷同一文件夾下的excel文件,在for next 循環中open 打開excel文件,for each next 循環工作簿中的工作表, usedrange.select 選擇使用區域,selection. 復制,activesheet.paste粘貼。
⑵ 如何用python遍歷文件夾下的所有excel文件
import os
for i in os.listdir("."):
if os.path.splitext(i)[1] == ".xlsx":
print i
⑶ excel 遍歷文件夾 宏命令
VBA遍歷文件夾常用有三種方法,這三種方法中,filesearch不適合2007和2010版本,而且速度比較慢,遞歸法速度也慢。只有用DIR加循環的方法,速度飛快。下面是三種方法的代碼:
1、filesearch法
Sub test3()
Dim wb As Workbook
Dim i As Long
Dim t
t = Timer
With Application.FileSearch '調用fileserch對象
.NewSearch '開始新的搜索
.LookIn = ThisWorkbook.path '設置搜索的路徑
.SearchSubFolders = True '搜索范圍包括 LookIn 屬性指定的文件夾中的所有子文件夾
.Filename = "*.xls" '設置搜索的文件類型
' .FileType = msoFileTypeExcelWorkbooks
If .Execute() > 0 Then '如果找到文件
For i = 1 To .FoundFiles.Count
'On Error Resume Next
Cells(i, 1) = .FoundFiles(i) '把找到的文件放在單元格里
Next i
Else
MsgBox "沒找到文件"
End If
End With
MsgBox Timer - t
End Sub
2、遞歸法
Sub Test()
Dim iPath As String, i As Long
Dim t
t = Timer
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "請選擇要查找的文件夾"
If .Show Then
iPath = .SelectedItems(1)
End If
End With
If iPath = "False" Or Len(iPath) = 0 Then Exit Sub
i = 1
Call GetFolderFile(iPath, i)
MsgBox Timer - t
MsgBox "文件名鏈接獲取完畢。", vbOKOnly, "提示"
End Sub
Private Sub GetFolderFile(ByVal nPath As String, ByRef iCount As Long)
Dim iFileSys
'Dim iFile As Files, gFile As File
'Dim iFolder As Folder, sFolder As Folders, nFolder As Folder
Set iFileSys = CreateObject("Scripting.FileSystemObject")
Set iFolder = iFileSys.GetFolder(nPath)
Set sFolder = iFolder.SubFolders
Set iFile = iFolder.Files
With ActiveSheet
For Each gFile In iFile
' .Hyperlinks.Add anchor:=.Cells(iCount, 1), Address:=gFile.path, TextToDisplay:=gFile.Name
iCount = iCount + 1
Next
End With
'遞歸遍歷所有子文件夾
For Each nFolder In sFolder
Call GetFolderFile(nFolder.path, iCount)
Next
End Sub
3、dir循環法
Sub Test() '使用雙字典,旨在提高速度
Dim MyName, Dic, Did, i, t, F, TT, MyFileName
'On Error Resume Next
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "選擇文件夾", 0, 0)
If Not objFolder Is Nothing Then lj = objFolder.self.path & "\"
Set objFolder = Nothing
Set objShell = Nothing
t = Time
Set Dic = CreateObject("Scripting.Dictionary") '創建一個字典對象
Set Did = CreateObject("Scripting.Dictionary")
Dic.Add (lj), ""
i = 0
Do While i < Dic.Count
Ke = Dic.keys '開始遍歷字典
MyName = Dir(Ke(i), vbDirectory) '查找目錄
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Ke(i) & MyName) And vbDirectory) = vbDirectory Then '如果是次級目錄
Dic.Add (Ke(i) & MyName & "\"), "" '就往字典中添加這個次級目錄名作為一個條目
End If
End If
MyName = Dir '繼續遍歷尋找
Loop
i = i + 1
Loop
Did.Add ("文件清單"), "" '以查找D盤下所有EXCEL文件為例
For Each Ke In Dic.keys
MyFileName = Dir(Ke & "*.xls")
Do While MyFileName <> ""
Did.Add (Ke & MyFileName), ""
MyFileName = Dir
Loop
Next
For Each Sh In ThisWorkbook.Worksheets
If Sh.Name = "XLS文件清單" Then
Sheets("XLS文件清單").Cells.Delete
F = True
Exit For
Else
F = False
End If
Next
If Not F Then
Sheets.Add.Name = "XLS文件清單"
End If
Sheets("XLS文件清單").[A1].Resize(Did.Count, 1) = WorksheetFunction.Transpose(Did.keys)
TT = Time - t
MsgBox Minute(TT) & "分" & Second(TT) & "秒"
End Sub
⑷ 如何用excel vba按關鍵字選擇性的遍歷文件夾搜索文件
Excel怎樣批量亂漏判提取文件夾和子文件夾嘩改所有文件
怎樣批量提取文件夾下搜舉文件名