Runtime Error 9

久未见 提交于 2019-12-12 02:37:54

问题


I am writing a Macro in VBA. This Macro was working fine until I changed the order around and now I am being shown "runtime error 9". After extensive searching on the internet I am at a loose end, I have checked that the sheet name is the same and that the sheet still exists as well as making sure I am referencing the correct workbook. Can anyone tell me why I am being shown this error?

Sub CopyStuff()

Sheets("DR1 -TC-001").Select
Range("C2:P23").Select
 'this is the cell range
 Selection.Copy

 With ActiveWorkbook Sheets.Add
 Range("C2:P23").Select
 Selection.Paste = wkb2

End Sub

回答1:


Your code runs to error on the With statement as the End With is missing and some periods.

Try this adaptation of your code.

Sub CopyStuff()

 Workbooks("With_data.xlsx").Activate
  'Activate the workbook that contains the data you want to copy    
 Sheets("DR1 -TC-001").Range("C2:P23").Copy
  'Copy a range

 ActiveWindow.ActivatePrevious
  'Revert to the previous new workbook

 With ActiveWorkbook
    .Sheets.Add
    .ActiveSheet.Range("C2:P23").PasteSpecial Paste:=xlPasteAll
 End With
  'Add a sheet and paste the copied range

 Application.CutCopyMode = False
  'Clear the clipboard

End Sub


来源:https://stackoverflow.com/questions/12258986/runtime-error-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!