这是我的脚本。
Sub Update_OOR()
Dim wsTNO As Worksheet
Dim wsTND As Worksheet
Dim wsTNA As Worksheet
Dim lastrow As Long, fstcell As Long
Set wsTNO = Sheets("Tel-Nexx OOR")
Set wsTND = Sheets("Tel-Nexx Data")
Set wsTNA = Sheets("Tel-Nexx Archive")
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
With Intersect(wsTNO.UsedRange, wsTNO.Columns("S"))
.AutoFilter 1, "<>Same"
With Intersect(.Offset(2).EntireRow, .Parent.Range("B:P"))
.Copy wsTNA.Cells(Rows.Count, "B").End(xlUp).Offset(1)
.EntireRow.Delete
End With
.AutoFilter
End With
'Blow away rows that are useless
lastrow = wsTND.Range("A2").End(xlDown).Row
wsTND.Range("O1:P1").Copy wsTND.Range("O2:P" & lastrow)
wsTND.UsedRange.Copy Sheets.Add.Range("A1")
With Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns("P"))
ActiveSheet.Range("O:P").Calculate
.AutoFilter 1, "<>Different"
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
With ActiveSheet
lastrow = wsTND.Range("A2").End(xlDown).Row
Intersect(.UsedRange, .Range("A2:M" & lastrow)).Copy wsTNO.Cells(Rows.Count, "B").End(xlUp).Offset(1)
.Delete
End With
With wsTNO
lastrow = wsTNO.Cells(Rows.Count, "B").End(xlUp).Row
wsTNO.Range("T1:AD1").Copy
wsTNO.Range("B3:N" & lastrow).PasteSpecial xlPasteFormats
lastrow = wsTNO.Cells(Rows.Count, "R").End(xlUp).Row
fstcell = wsTNO.Cells(Rows.Count, "N").End(xlUp).Row
wsTNO.Range("AE1:AI1").Copy wsTNO.Range("O" & fstcell & ":S" & lastrow).Offset(1, 0)
End With
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
它在技术上完美地工作到这里:
With wsTNO
lastrow = wsTNO.Cells(Rows.Count, "B").End(xlUp).Row
wsTNO.Range("T1:AD1").Copy
wsTNO.Range("B3:N" & lastrow).PasteSpecial xlPasteFormats
lastrow = wsTNO.Cells(Rows.Count, "R").End(xlUp).Row
fstcell = wsTNO.Cells(Rows.Count, "N").End(xlUp).Row
wsTNO.Range("AE1:AI1").Copy wsTNO.Range("O" & fstcell & ":S" & lastrow).Offset(1, 0)
End With
现在从技术上讲,这部分中的所有内容都可以正常工作,但是代码中的最后一行,它正确地传递了所有内容,然后又向前迈出了一步。我想知道为什么。如果我摆脱偏移量,它会覆盖 O 到 S 中上面单元格中的内容。我需要知道第一个和最后一个单元格,因为数据只需要写入特定的单元格范围。
如果有更简单的方法可以做到这一点,如果有人可以告诉我,将不胜感激,如果没有,那么有人可以告诉我如何解决这个问题吗?
谢谢。
附上工作簿。