試試看
Sub ex()
Dim fds As Object, fs$, Path$
Dim c As Variant, a As Variant
Application.ScreenUpdating = False
Set c = Nothing
Path = ThisWorkbook.Path
For Each a In Sheets("CVS").Range([a3], [a3].End(4))
If Sheets("VBA").[AS7] = "V" And Sheets("VBA").[AS6] <> "" Then '[AS7] & [AS6]皆不為空白
If a = "結束" Or a.Offset(, 8) = "" Or a.Offset(, 8) < Sheets("VBA").[AS6] Then '比對A欄是否為"結束",I欄是否為空白或小於[AS6]
If c Is Nothing Then
Set c = a.Resize(, 14)
Else
Set c = Union(c, a.Resize(, 14))
End If
End If
ElseIf Sheets("VBA").[AS7] = "V" And Sheets("VBA").[AS6] = "" Then '[AS7]不為空白,[AS6]為空白
If a = "結束" Then '比對A欄是否為"結束"
If c Is Nothing Then
Set c = a.Resize(, 14)
Else
Set c = Union(c, a.Resize(, 14))
End If
End If
ElseIf Sheets("VBA").[AS7] = "" And Sheets("VBA").[AS6] <> "" Then '[AS7]為空白,[AS6]不為空白
If a.Offset(, 8) = "" Or a.Offset(, 8) < Sheets("VBA").[AS6] Then '比對I欄是否為空白或小於[AS6]
If c Is Nothing Then
Set c = a.Resize(, 14)
Else
Set c = Union(c, a.Resize(, 14))
End If
End If
End If
Next
c.EntireRow.Delete '將符合條件的列刪除
Application.DisplayAlerts = False
Set fds = CreateObject("Scripting.filesystemobject")
fs = Dir(Path & "\AAA-" & Format(Date, "yyyymmdd") & "*.xlsx") '來源檔案資料夾內的檔案名
Do Until fs = "" '直到讀取檔案名是空字串
If fds.FileExists(Path & fs) Then Kill Path & fs '如果檔案已經存在就先刪除檔案
fs = Dir '下一個檔案
Loop
ActiveWorkbook.SaveAs Filename:=Path & "\AAA-" & Format(Date, "yyyymmdd") + ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Workbooks("促銷資訊.xlsm").Close False
End Sub作者: PJChen 時間: 2021-3-3 23:58
只做刪除部份
Sub TEST()
Dim Arr, KS$, DS, D, R&, xR As Range, xU As Range, N&
If [VBA!AS7] = "V" Then KS = "結束" Else KS = "|^|"
If IsDate([VBA!AS6]) Then DS = CDate([VBA!AS6]) Else DS = -9
R = [CVS!A65536].End(xlUp).Row - 2
If R <= 0 Then Exit Sub
For Each xR In [CVS!A3].Resize(R)
If IsDate(xR(1, 9)) Then D = CDate(xR(1, 9)) Else D = 0
If xR = KS Or xR(1, 9) < DS Then
N = N + 1
If N = 1 Then Set xU = xR Else Set xU = Union(xU, xR)
End If
Next
If N > 0 Then xU.EntireRow.Delete
MsgBox "共有 " & N & " 筆被刪除 "
End Sub
Do While FN <> ""
Set xB = Nothing
On Error Resume Next: Set xB = Workbooks(FN): On Error GoTo 0 '這用來檢查檔案是否已開啟
If xB Is Nothing Then Set xB = Workbooks.Open(PH & FN) '檔案未開啟時才能用open, 否則重覆開啟會造成當機
Set Sh = xB.Sheets("CVS")