Sub MakeTables()
Dim wbTarget As Object
Set wb = Workbooks.Open("C:\Users\A9900899\Desktop\Desmond\VBAProject\GenerateTablesFormulas.xlsx")
Set wbTarget = Workbooks.Open("C:\Users\A9900899\Desktop\Desmond\VBAProject\USDReport.xlsx")
With wb.Sheets("Sheet1").UsedRange
.Copy
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82:X97")
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
End With
End Sub
有人可以告诉我我在这里犯了什么错误。它给了我范围类失败的错误。谢谢
答案 0 :(得分:0)
如果你想保留复制的范围大小,那么:
With wb.Sheets("Sheet1").UsedRange
.Copy
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82") '.Resize(.Rows.Count, .Columns.Count) '<--| you can omit the 'Resize' part but it can be useful to make it clear (code can be read in years to come) you want to stick to copied range size
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
End With
Application.CutCopyMode = False '<--| empty clipboard
否则,如果您希望将force
复制的范围大小转移到要粘贴的,那么:
' Create the new sheet and name it at the end
With wbTarget.Sheets("HK").Range("D82:X97") '<--| reference range you want to paste values to and keep sizes of
wb.Sheets("Sheet1").UsedRange.Resize(.Rows.Count, .Columns.Count).Copy '<--| copy the source sheet range with same sizes as referenced range
.PasteSpecial xlValues
.PasteSpecial xlFormats
End With
Application.CutCopyMode = False '<--| empty clipboard