2014-10-27 124 views
0

我有一個代碼從工作簿A中的特定工作表中選擇所有數據(在代碼中名稱爲'fn')並將其複製到工作簿B中的特定工作表中。我想要做的是僅貼上值,即貼,同時保持/轉換(到)過去片表的格式從複印紙上覆制的值,這裏是我的代碼 -使用vba粘貼值

Set copySheet = copyBook.Sheets(fn) 'Points to the sheet in the raw data file 
Set pasteBook = ThisWorkbook 'Sets the file where we are supposed to paste to as the template file 
Application.DisplayAlerts = False 
Set pasteSheet = pasteBook.Sheets("Transaction") 'Points to the Transaction sheet on the template 
lastRow = copySheet.Cells(Rows.Count, 1).End(xlUp).row 'Computes the last row on the raw data file 


'Copies raw data to tab/sheet in template 
copySheet.Activate 
Application.ScreenUpdating = False 
For column = 1 To 27 
    For row = 2 To lastRow 
    cp = copySheet.Cells(row, column) 
    pasteSheet.Cells(row + 211, column).Value = cp 
    Next 
Next 
Application.ScreenUpdating = True 


copyBook.Close 'Closes the raw data file 
End If 

注 - 我真的喜歡這我只是對上面的代碼做了一些小改動,而不是完全製作其他代碼。謝謝!

+0

你爲什麼循環? Wy不會複製並一次性完成粘貼操作? – 2014-10-27 16:02:06

+0

你能告訴我怎麼做:)(如在代碼本身) – 2014-10-27 16:18:59

+2

我可以但爲什麼你不記錄一個宏,並嘗試爲自己:) – 2014-10-27 16:33:27

回答

0
cp = copySheet.Cells(row, column) 
pasteSheet.Cells(row + 211, column).Value = cp 

更改爲

copySheet.Cells(row, column).Select 
Selection.Copy 
pasteSheet.Cells(row + 211, column).Select 
Selection.PasteSpecial Paste:=xlPasteValues 

Application.CutCopyMode = False 

Application.CutCopyMode這裏被設置爲false,只是需要你的程序出的複製模式。

0

試試這個。您也可以跳過激活工作表,因爲您只是複製數據。

Application.ScreenUpdating = False 
For column = 1 To 27 
    For row = 2 To lastRow 

    pasteSheet.Cells(row + 211, column) = copySheet.Cells(row, column).Value 

    Next 
Next 
0

更新你的循環與此:

For column = 1 To 27 
    For row = 2 To lastRow 
     copySheet.Cells(row, column).copy Destination:=pasteSheet.Cells(row + 211, column) 
    Next 
Next