2012-05-23 58 views
0

這是我的腳本。複製工作表數據並將其粘貼到其中的腳本

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

+0

當我遍歷代碼時,fstcell變爲45,並且lastrow在突出顯示的行中變爲2。我想知道從底部開始定義的範圍是否會影響您的抵消? – SeanC

回答

1

在你的第二張的代碼添加的+ 1

lastrow = wsTNO.Cells(Rows.Count, "R").End(xlUp).Row 

所以,你必須

lastrow = wsTNO.Cells(Rows.Count, "R").End(xlUp).Row + 1 

前者爲您提供了第2行,這是你的標題行。你想要的是第3行,你的標題後面的行。

更新:展示如何在未來測試

雖然。選擇方法在通常皺起了眉頭。這對測試/調試非常有用。我跑了

wsTNO.Range("O" & fstcell & ":S" & lastrow).Select 

在我設置lastrow和fstcell找到設置的範圍之後的立即窗口中。所以我知道你不想複製你的頭文件。從那裏你可以找出是什麼驅動該範圍進行設置和相應調整。

+0

我想知道這是否可行,當你需要的時候,這些東西似乎永遠不會出現在網絡上,我翻閱了幾張紙。非常感謝! –

+0

看到我的編輯,以幫助您自己調試這個東西在未來。 –

+0

相信我我已經記錄了,爲了將來的目的,再次,非常感謝。 –

相關問題