运行时错误9 [英] Runtime Error 9

查看:127
本文介绍了运行时错误9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VBA写宏。这个宏工作正常,直到我改变了顺序,现在我被显示运行时错误9。在互联网上进行广泛的搜索之后,我已经检查了表单的名称是否相同,该表仍然存在,并确保我引用了正确的工作簿。任何人都可以告诉我为什么我出现这个错误?

  Sub CopyStuff()

表格选择
范围(C2:P23)。选择
'这是单元格范围
Selection.Copy

With ActiveWorkbook Sheets.Add
Range(C2:P23)。选择
Selection.Paste = wkb2

End Sub
pre>

解决方案

您的代码运行到使用语句错误,因为 End With 缺少一些时间段。



尝试这种适配代码。

 code> Sub CopyStuff()

工作簿(With_data.xlsx)。激活
'激活包含要复制的数据的工作簿
表格( DR1 -TC-001)范围(C2:P23)复制
'复制范围

ActiveWindow.ActivatePrevious
'还原到以前的新工作簿

使用ActiveWorkbook
.Sheets.Add
.ActiveSheet.Range(C2:P23)。 PasteSpecial Paste:= xlPasteAll
End with
'添加工作表并粘贴复制的范围

Application.CutCopyMode = False
'清除剪贴板

End Sub


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

解决方案

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

这篇关于运行时错误9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆