2013-04-04 132 views
1

我已經反向工程,並開發了一個代碼感謝您在這裏的所有努力,但我已經擺脫了一件小事。從WS2複製時我想要執行的操作是粘貼爲特殊的,因爲我不想導出導致工作簿崩潰的列AD:AR中包含的所有公式。粘貼特殊只有特定範圍

Sub Copyandpaste() 
    Dim LastRow As Long 
    Dim ws1 As Worksheet, ws2 As Worksheet 

    Set ws1 = ActiveWorkbook.Sheets("RAW DATA") 
    Set ws2 = ActiveWorkbook.Sheets("DATA INPUT") 

    LastRow = ws1.UsedRange.Rows.Count 

    With ws2 
     .Range("A2:AR2" & .Cells(Rows.Count, "G").End(xlUp).Row).Copy Destination:=ws1.Range("A" & LastRow + 1) 
     For Each WS In ThisWorkbook.Worksheets 
      For Each PT In WS.PivotTables 
       PT.RefreshTable 
      Next PT 
     Next WS 
    End With 
End Sub 
+0

您提到'AD:AR',但代碼中說'A:AR'? – 2013-04-04 09:41:48

+0

添加.Value? Destination:= ws1.Range(「A」&LastRow + 1).Value – 2013-04-04 09:43:35

+0

這裏有一些問題:'.Range(「A2:AR2」&.Cells(Rows.Count,「G」)。End(xlUp) .Row)'。請檢查它... – 2013-04-04 10:09:29

回答

0

這是你正在嘗試?

Sub Copyandpaste() 
    Dim ws1LRow As Long, ws2LRow As Long 
    Dim ws1 As Worksheet, ws2 As Worksheet 

    Set ws1 = ThisWorkbook.Sheets("RAW DATA") 
    Set ws2 = ThisWorkbook.Sheets("DATA INPUT") 

    With ws1 
     ws1LRow = .Cells.Find(What:="*", _ 
        After:=.Range("A1"), _ 
        Lookat:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Row + 1 
    End With 

    With ws2 
     ws2LRow = .Range("G" & .Rows.Count).End(xlUp).Row 

     .Range("A2:AR" & ws2LRow).Copy 

     ws1.Range("A" & ws1LRow).PasteSpecial xlPasteValues 

'  For Each WS In ThisWorkbook.Worksheets 
'   For Each PT In WS.PivotTables 
'    PT.RefreshTable 
'   Next PT 
'  Next WS 
    End With 
End Sub 
+0

感謝代碼,它的工作很棒。是否有一種調整方式,以便在完成後不會使複製選項保持活動狀態。 Probaby問太多我知道。 – Werra2006 2013-04-04 12:09:36

+0

在完成粘貼後添加以下行'Application.CutCopyMOde = False :) – 2013-04-04 12:11:08