資料夾路徑如下↓
D:\AA\BB\CC\DD
檔案名稱如下↓
L-M AA R S L(XXX0001)20210820-20210823.xlsx
L-M AA R S L(XXX0002)20210824-20210827.xlsx
L-M AA R S L(XXX0002)20210828-20210831.xlsx
假設A: 今天日期是2021/08/22,那麼我想開啟L-M AA R S L(XXX0001)20210820-20210823.xlsx的檔案。
假設B: 今天日期是2021/08/29,那麼我想開啟L-M AA R S L(XXX0002)20210828-20210831.xlsx的檔案。
請測試看看,謝謝
Sub 選擇檔案可複選()
With Application.FileDialog(msoFileDialogOpen)
.InitialFileName = "D:\"
.AllowMultiSelect = True
.Show
fc = .SelectedItems.Count: If fc = 0 Then Exit Sub
For x = 1 To fc
FPath = .SelectedItems(x)
Set WB = Workbooks.Open(FPath)
With WB.Sheets(1)
'需求程式
End With
WB.Close
Next
End With
End Sub作者: zz0660 時間: 2021-8-23 13:16
Sub test()
Dim Arr(), T$, d$, d1$, d2$, xY$, xM$, xD$
T = Date
Set fs = CreateObject("Scripting.FileSystemObject")
a = "D:\AA\BB\CC\DD"
Set f = fs.GetFolder(a)
Set fc = f.Files
For Each f1 In fc
d = Split(Split(f1.Name, ")")(1), "-")(0)
xY = Left(d, 4): xM = Mid(d, 5, 2): xD = Right(d, 2)
d1 = DateSerial(xY, xM, xD)
d = Split(Split(Split(f1.Name, ")")(1), "-")(1), ".")(0)
xY = Left(d, 4): xM = Mid(d, 5, 2): xD = Right(d, 2)
d2 = DateSerial(xY, xM, xD)
If T >= d1 And T <= d2 Then
ReDim Preserve Arr(n)
Arr(n) = f1.Path
n = n + 1
End If
Next
If n > 0 Then
For i = 0 To n - 1
Set WB = Workbooks.Open(Arr(i))
'主程式需求
WB.Close
Next
End If
End Sub作者: zz0660 時間: 2021-8-23 15:55
Sub test()
Dim Arr(), T$, d$, d1$, d2$, xY$, xM$, xD$
T = Date
Set fs = CreateObject("Scripting.FileSystemObject")
a = "D:\AA\BB\CC\DD"
Set f = fs.GetFolder(a)
Set fc = f.Files
For Each f1 In fc
d = Split(Split(f1.Name, ")")(1), "-")(0)
xY = Left(d, 4): xM = Mid(d, 5, 2): xD = Right(d, 2)
d1 = DateSerial(xY, xM, xD)
d = Split(Split(Split(f1.Name, ")")(1), "-")(1), ".")(0)
xY = Left(d, 4): xM = Mid(d, 5, 2): xD = Right(d, 2)
d2 = DateSerial(xY, xM, xD)
If T >= d1 And T <= d2 Then
ReDim Preserve Arr(n)
Arr(n) = f1.Path
n = n + 1
End If
Next
If n > 0 Then
For i = 0 To n - 1
Set WB = Workbooks.Open(Arr(i))
'主程式需求
您好,我使用此段程式碼↓
Sub test()
Dim Arr(), T$, d$, d1$, d2$, xY$, xM$, xD$
T = Date
Set fs = CreateObject("Scripting.FileSystemObject")
a = "D:\AA\BB\CC\DD"
Set f = fs.GetFolder(a)
Set fc = f.Files
For Each f1 In fc
d = Split(Split(f1.Name, ")")(1), "-")(0)
xY = Left(d, 4): xM = Mid(d, 5, 2): xD = Right(d, 2)
d1 = DateSerial(xY, xM, xD)
d = Split(Split(Split(f1.Name, ")")(1), "-")(1), ".")(0)
xY = Left(d, 4): xM = Mid(d, 5, 2): xD = Right(d, 2)
d2 = DateSerial(xY, xM, xD)
If T >= d1 And T <= d2 Then
ReDim Preserve Arr(n)
Arr(n) = f1.Path
n = n + 1
End If
Next
If n > 0 Then
For i = 0 To n - 1
Set WB = Workbooks.Open(Arr(i))
'主程式需求
>> 新增如下紅字,請再測試,謝謝
Sub test()
Dim Arr(), T$, d$, d1$, d2$, xY$, xM$, xD$
T = Date
Set fs = CreateObject("Scripting.FileSystemObject")
a = "D:\AA\BB\CC\DD"
Set f = fs.GetFolder(a)
Set fc = f.Files
For Each f1 In fc
If UCase(Left(f1.Name, 3)) <> "L-M" Then GoTo 99
d = Split(Split(f1.Name, ")")(1), "-")(0)
xY = Left(d, 4): xM = Mid(d, 5, 2): xD = Right(d, 2)
d1 = DateSerial(xY, xM, xD)
d = Split(Split(Split(f1.Name, ")")(1), "-")(1), ".")(0)
xY = Left(d, 4): xM = Mid(d, 5, 2): xD = Right(d, 2)
d2 = DateSerial(xY, xM, xD)
If T >= d1 And T <= d2 Then
ReDim Preserve Arr(n)
Arr(n) = f1.Path
n = n + 1
End If
99: Next
Dim fs As Object, f, fc, f1, d1, d2, d3
Set fs = CreateObject("Scripting.FileSystemObject")
a = "D:\AA\BB\CC\DD"
Set f = fs.GetFolder(a)
Set fc = f.Files
d3 = Format(Now(), "yyyymmdd")
For Each f1 In fc
If f1.Name Like "L-M AA R S L(?*.xlsx" Then
d1 = Mid(f1.Name, 22, 8)
d2 = Mid(f1.Name, 31, 8)
If d3 >= d1 And d3 <= d2 Then
DoEvents
Set WB = Workbooks.Open(f1)
End If
End If
Next