2014-02-08 191 views
10

我試圖讓一個Excel宏工作,但我有一個問題,從包含公式的單元格複製值。複製範圍和粘貼值在另一個工作表的特定範圍

到目前爲止,這是我的,它與非公式單元格工作正常。

Sub Get_Data() 
Dim lastrow As Long 

lastrow = Sheets("DB").Range("A65536").End(xlUp).Row + 1 

Range("B3:B65536").Copy Destination:=Sheets("DB").Range("B" & lastrow) 
Range("C3:C65536").Copy Destination:=Sheets("DB").Range("A" & lastrow) 
Range("D3:D65536").Copy Destination:=Sheets("DB").Range("C" & lastrow) 
Range("E3:E65536").Copy Destination:=Sheets("DB").Range("P" & lastrow) 
Range("F3:F65536").Copy Destination:=Sheets("DB").Range("D" & lastrow) 
Range("AH3:AH65536").Copy Destination:=Sheets("DB").Range("E" & lastrow) 
Range("AIH3:AI65536").Copy Destination:=Sheets("DB").Range("G" & lastrow) 
Range("AJ3:AJ65536").Copy Destination:=Sheets("DB").Range("F" & lastrow) 
Range("J3:J65536").Copy Destination:=Sheets("DB").Range("H" & lastrow) 
Range("P3:P65550").Copy Destination:=Sheets("DB").Range("I" & lastrow) 
Range("AF3:AF65536").Copy Destination:=Sheets("DB").Range("J" & lastrow). 

End Sub 

我怎樣才能讓它粘貼值呢?

如果這可以改變/優化,我也會很感激。

回答

20

您可以更改

Range("B3:B65536").Copy Destination:=Sheets("DB").Range("B" & lastrow) 

Range("B3:B65536").Copy 
Sheets("DB").Range("B" & lastrow).PasteSpecial xlPasteValues 

順便說一句,如果你有xls文件(Excel 2003中),你會得到一個錯誤,如果你lastrow會更大3.

請嘗試使用此代碼:

Sub Get_Data() 
    Dim lastrowDB As Long, lastrow As Long 
    Dim arr1, arr2, i As Integer 

    With Sheets("DB") 
     lastrowDB = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 
    End With 

    arr1 = Array("B", "C", "D", "E", "F", "AH", "AI", "AJ", "J", "P", "AF") 
    arr2 = Array("B", "A", "C", "P", "D", "E", "G", "F", "H", "I", "J") 

    For i = LBound(arr1) To UBound(arr1) 
     With Sheets("Sheet1") 
      lastrow = Application.Max(3, .Cells(.Rows.Count, arr1(i)).End(xlUp).Row) 
      .Range(.Cells(3, arr1(i)), .Cells(lastrow, arr1(i))).Copy 
      Sheets("DB").Range(arr2(i) & lastrowDB).PasteSpecial xlPasteValues 
     End With 
    Next 
    Application.CutCopyMode = False 
End Sub 

請注意,上面的代碼確定DB表中DB表的最後一個非空行,列A(變量lastrowDB)。如果你需要找到LASTROW在DB表中的每個目標列,用下一個修改:

For i = LBound(arr1) To UBound(arr1) 
    With Sheets("DB") 
     lastrowDB = .Cells(.Rows.Count, arr2(i)).End(xlUp).Row + 1 
    End With 

    ' NEXT CODE 

Next 

你也可以用另一個方法代替Copy/PasteSpecial。更換

.Range(.Cells(3, arr1(i)), .Cells(lastrow, arr1(i))).Copy 
Sheets("DB").Range(arr2(i) & lastrowDB).PasteSpecial xlPasteValues 

Sheets("DB").Range(arr2(i) & lastrowDB).Resize(lastrow - 2).Value = _ 
     .Range(.Cells(3, arr1(i)), .Cells(lastrow, arr1(i))).Value 
+0

謝謝您的回覆。我嘗試使用數組的第一個代碼,並且出現「腳本超出範圍」的錯誤,並且在將表格(「Sheet1」)更改爲「DB」後,它只是沒有做任何事情。就像FYI一樣,我正在使用Excel 2010.我還沒有嘗試過其他選項。有什麼建議麼? – BlueSun3k1

+0

我也糾正了這部分。 >> arr1 = Array(「B」,「C」,「D」,「E」,「F」,「AH」,「AIH」,「AJ」,「J」,「P」,「AF」) 「從AIH到AI並且這個固定的,仍然有同樣的錯誤。 – BlueSun3k1

+1

你應該將'Sheet1'換成'With With Sheets(Sheet1)''到你正在處理數據的表格名稱_from。例如。如果將表單'mySheet'中的數據複製到'DB'表單中,則應將'Sheet1'(與'Sheets(「Sheet1」)')一致更改爲'mySheet'。如果您想從_active sheet_中複製,請將'With Sheets(「Sheet1」)'改爲'With ActiveSheet'(它會將當前活動工作表中的數據複製到'DB'工作表中) –

0

怎麼樣,如果你在一個表到不同的紙張複印每列? 示例:sheetheet的B行到sheet1的B行,mysheet的C行到sheet 2的B行...

+0

這應該是一條評論 –

相關問題