请帮我优化以下代码,使他在对比时如果只有原数据一行的情况就按代码的逻辑复制这一行并粘贴Sub CopySameDay() Dim ws As Worksheet Dim lastRow As Long Dim i As Long Dim copyRange As Range Dim pasteRange As Range Dim wb As Workbook Dim folderPath As String Dim fileName As String Dim asteRange As Range Set ws = ActiveSheet lastRow = ws.Cells(Rows.Count, "D").End(xlUp).Row For i = 2 To lastRow If Format(ws.Range("D" & i).Value, "yyyy-mm-dd") = Format(ws.Range("D" & i - 1).Value, "yyyy-mm-dd") And ws.Range("B" & i).Value = ws.Range("B" & i - 1).Value Then If copyRange Is Nothing Then Set copyRange = ws.Range("A" & i - 1) End If Set pasteRange = ws.Range("A" & i) Else If Not copyRange Is Nothing Then folderPath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\")) Set asteRange = ws.Range("B" & i - 1) fileName = asteRange.Value & ".xlsx" If Dir(folderPath & fileName) = "" Then Set wb = Workbooks.Add wb.SaveAs folderPath & fileName Else Set wb = Workbooks.Open(folderPath & fileName) End If wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = Format(copyRange.Offset(0, 3).Value, "yyyy-mm-dd") copyRange.Resize(i - copyRange.Row, ws.Columns.Count).Copy wb.Sheets(wb.Sheets.Count).Range("A1").PasteSpecial xlPasteValues wb.Save wb.Close Set copyRange = Nothing End If End If Next i If Not copyRange Is Nothing Then folderPath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\")) Set asteRange = ws.Range("B" & i - 1) fileName = asteRange.Value & ".xlsx" If Dir(folderPath & fileName) = "" Then Set wb = Workbooks.Add wb.SaveAs folderPath & fileName Else Set wb = Workbooks.Open(folderPath & fileName) End If wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = Format(copyRange.Offset(0, 3).Value, "yyyy-mm-dd") copyRange.Resize(lastRow - copyRange.Row + 1, ws.Columns.Count).Copy wb.Sheets(wb.Sheets.Count).Range("A1").PasteSpecial xlPasteValues wb.Save wb.Close End If End Sub
时间: 2024-01-27 17:05:49 浏览: 81
代码变形乱序demo
Sub CopySameDay()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim copyRange As Range
Dim pasteRange As Range
Dim wb As Workbook
Dim folderPath As String
Dim fileName As String
Dim asteRange As Range
Set ws = ActiveSheet
lastRow = ws.Cells(Rows.Count, "D").End(xlUp).Row
For i = 2 To lastRow
If Format(ws.Range("D" & i).Value, "yyyy-mm-dd") = Format(ws.Range("D" & i - 1).Value, "yyyy-mm-dd") And ws.Range("B" & i).Value = ws.Range("B" & i - 1).Value Then
If copyRange Is Nothing Then
Set copyRange = ws.Range("A" & i - 1)
End If
Set pasteRange = ws.Range("A" & i)
Else
If Not copyRange Is Nothing Then
folderPath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))
Set asteRange = ws.Range("B" & i - 1)
fileName = asteRange.Value & ".xlsx"
If Dir(folderPath & fileName) = "" Then
Set wb = Workbooks.Add
wb.SaveAs folderPath & fileName
Else
Set wb = Workbooks.Open(folderPath & fileName)
End If
wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = Format(copyRange.Offset(0, 3).Value, "yyyy-mm-dd")
copyRange.Resize(i - copyRange.Row, ws.Columns.Count).Copy
wb.Sheets(wb.Sheets.Count).Range("A1").PasteSpecial xlPasteValues
wb.Save
wb.Close
Set copyRange = Nothing
End If
End If
Next i
If Not copyRange Is Nothing Then
folderPath = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, "\"))
Set asteRange = ws.Range("B" & i - 1)
fileName = asteRange.Value & ".xlsx"
If Dir(folderPath & fileName) = "" Then
Set wb = Workbooks.Add
wb.SaveAs folderPath & fileName
Else
Set wb = Workbooks.Open(folderPath & fileName)
End If
wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count)).Name = Format(copyRange.Offset(0, 3).Value, "yyyy-mm-dd")
copyRange.Resize(lastRow - copyRange.Row + 1, ws.Columns.Count).Copy
wb.Sheets(wb.Sheets.Count).Range("A1").PasteSpecial xlPasteValues
wb.Save
wb.Close
End If
End Sub
优化后的代码只在最后一行数据为唯一数据时进行额外的复制操作。在原代码中,在最后一行数据为唯一数据时,会进行两次复制操作。
阅读全文