我有一个带有子文件夹“ Test1”,“ Test2”,“ Test3”等的“ Tests”文件夹... 每个子文件夹中都有很多文件。
在主文件.xlsm中,使用vba代码,我想打开每个子文件夹中的每个文件以从中提取数据。 我应该写什么代码?
我只能从主文件夹根目录中的.txt文件提取数据
Private Sub CommandButtonl_Click()
Dim my_files As String
Dim folder_path As String
Dim SubFolder As Object
Dim wb As Workbook
Dim ws As Worksheet
x = 1
folder_path = ThisWorkbook.Path & (".\")
my_files = Dir(folder_path & "\*.txt")
Do While my_files <> vbNullString
Set wb = Workbooks.Open(folder_path & "/" & my_files)
Set ws = wb.Sheets(1)
Range("A45").Copy
wb.Close SaveChanges:=False
Range("D" & x).Select
ActiveSheet.PasteSpecial Format:="Texte", Link:=False, DisplayAsIcon:= _
False
x = x + 1
my_files = Dir()
Loop
Range("D:D").Select
Selection.Replace What:="<IMG SRC=""", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:=""" ></U>", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Font.Size = 10
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.ColumnWidth = 24
End Sub