這是我的腳本。複製工作表數據並將其粘貼到其中的腳本
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中的單元格中的內容。我需要知道第一個和最後一個單元格,因爲數據只需寫入特定的單元格區域。
如果有一個更簡單的方法做到這一點,如果有人能告訴我,如果不是那麼可以告訴我如何解決這個問題呢?
謝謝。
附加的是工作簿。
http://dl.dropbox.com/u/3327208/Excel/First%26LastRows.xlsm
當我遍歷代碼時,fstcell變爲45,並且lastrow在突出顯示的行中變爲2。我想知道從底部開始定義的範圍是否會影響您的抵消? – SeanC