使用宏时为什么一直出现运行错误?(Why do I keep getting a run error when using my macro?)

编程入门 行业动态 更新时间:2024-10-28 11:30:43
使用宏时为什么一直出现运行错误?(Why do I keep getting a run error when using my macro?)

您好我有一个宏,我在另一个项目上使用并试图在这个上使用它。

目前我有两张Excel工作表(Excel工作表A和Excel工作表B),并且有一个宏应该在Excel工作表A的某个选项卡上取一定范围,然后将其复制到Excel工作表B中的某个选项卡上。

宏运行正常,直到某个点朝着粘贴部分进入Excel工作表B.

问题就在ThisWorkbook.Activate之后:

“表格(”GTA800-1发动机小时数“)。选择”

完整代码如下:

' open the source workbook and select the source sheet Workbooks.Open Filename:="D:My Documents\Fleet Data (Hrs, Aircraft flying, etc.)\updated_monthly_engine_hrs_97_03.xls" Sheets("PW150").Select ' copy the source range Sheets("PW150").Range("B2:G13").Select Selection.Copy ' select current workbook and paste the values ThisWorkbook.Activate Sheets("GTA800-1 Engine Hours").Select Sheets("GTA800-1 Engine Hours").Range("B2:G13").Select Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _ False, Transpose:=False ' close the source workbook Windows("updated_monthly_engine_hrs_97_03.xls").Activate ActiveWorkbook.Close

Hello I have a macro that I used on another project and trying to use it on this one.

Currently I have two excel sheets (Excel sheet A and Excel sheet B) and have a macro that is supposed to take a certain range on a certain tab from Excel sheet A and then copy it on a certain tab in Excel sheet B.

The macro is running fine up until a certain point towards the pasting part into Excel sheet B.

The problem is right after ThisWorkbook.Activate:

"Sheets("GTA800-1 Engine Hours").Select"

The full code is listed below:

' open the source workbook and select the source sheet Workbooks.Open Filename:="D:My Documents\Fleet Data (Hrs, Aircraft flying, etc.)\updated_monthly_engine_hrs_97_03.xls" Sheets("PW150").Select ' copy the source range Sheets("PW150").Range("B2:G13").Select Selection.Copy ' select current workbook and paste the values ThisWorkbook.Activate Sheets("GTA800-1 Engine Hours").Select Sheets("GTA800-1 Engine Hours").Range("B2:G13").Select Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _ False, Transpose:=False ' close the source workbook Windows("updated_monthly_engine_hrs_97_03.xls").Activate ActiveWorkbook.Close

最满意答案

知道实际的错误消息会很有用,但无论如何都不需要选择:

With Workbooks.Open(Filename:="D:My Documents\Fleet Data (Hrs, Aircraft flying, etc.)\updated_monthly_engine_hrs_97_03.xls") .Sheets("PW150").Range("B2:G13").Copy ' select current workbook and paste the values ThisWorkbook.Sheets("GTA800-1 Engine Hours").Range("B2").PasteSpecial Paste:=xlValues, _ Operation:=xlNone, SkipBlanks:=False, Transpose:=False .Close End With

I'm not 100% sure what the issue was here. I just created a new tab in the worksheet and named it abc just to test it out and then the macro worked just fine. Although the naming was correct on the previous tab above ???

That previous tab was linked to access but I DID delete the link that's the only thing that I can think of that caused the error. Anyone want to follow up on this and confirm ? Otherwise this is my answer on how to solve my issue.

更多推荐

本文发布于:2023-08-02 07:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372146.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   run   macro   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!