问题
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