简体   繁体   中英

Copy-paste special VBA does not work

I am trying to copy-paste special values and formatting from multiple workbooks into a master spreadsheet. The number of columns is the same in the source and destination and the number of rows varies.

I read the other threads on this and tried multiple ways (including defining a range in the destination file) but I still cannot make the paste special work. Below is my code:

Sub mergefiles()

Dim folderpath As String
Dim filepath As String
Dim filename As String
Dim erow As Long


folderpath = "D:\Test\"
filepath = folderpath & "*.xls*"
filename = Dir(filepath)

Dim lastrow As Long, lastcolumn As Long

Do While filename <> ""

  Workbooks.Open (folderpath & filename)

  Worksheets("EmplOffers").ShowAllData

  lastrow = ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
  lastcolumn = ActiveSheet.Cells(6, Columns.Count).End(xlToLeft).Column

  Range(Cells(6, 3), Cells(lastrow, lastcolumn)).Copy

  Application.DisplayAlerts = False

  ActiveWorkbook.Close

  erow = Worksheets("EmplOffers").Cells(Rows.Count, 3).End(xlUp).Offset(1,     0).Row

  Worksheets("EmplOffers").Range(Cells(erow, 3), Cells(erow, 20)).PasteSpecial=:xlPasteValuesAndNumberFormats

  filename = Dir

Loop

Application.DisplayAlerts = True

End Sub

Thanks a lot!

尝试调整粘贴呼叫:

Worksheets("EmplOffers").Range(Cells(erow, 3).Address).PasteSpecial Operation:=xlPasteValuesAndNumberFormats

A named parameter should precede :=
Change

Worksheets("EmplOffers").Range(Cells(erow, 3), Cells(erow, 20)).PasteSpecial=:xlPasteValuesAndNumberFormats

To:

Worksheets("EmplOffers").Range(Cells(erow, 3), Cells(erow, 20)).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM