有时,当我运行代码时,它可以工作,而其他时候,我会收到运行错误“ 1004”,但我不知道为什么?

问题描述

首先,我是VBA的初学者,所以我不太了解。我倾向于记录宏,然后稍加修改。

我收到以下错误1004消息

您不能在此处粘贴此内容,因为“复制区域”和“粘贴区域”的大小不同。在粘贴区域或相同大小的区域中仅选择一个单元格,然后尝试再次粘贴。

   Private Sub TransferExpenses_Click()
    
        ThisWorkbook.Activate   'Transfer ExpenseImport Data over to CMiCExport Tab
        Sheets("ExpenseImport").Select
        Range("A1:AE1",Selection.End(xlDown)).Select
        Selection.Copy
        ThisWorkbook.Activate
        Sheets("CMiCExport").Select
        Sheets("CMiCExport").Range("A1").Select
        Selection.End(xlDown).Select
        ActiveCell.Offset(1,0).Range("A1").Select
        Sheets("CMiCExport").Paste
    
        MsgBox Title:="Expenses Imported Successfully!",Prompt:="The data for your expenses was verified and transferred to the CMiCExport Tab. Please double check column C -Job & Scope- and revise the .XXDefault entries."
    
   End Sub

我基本上只是想将数据从一张工作表“ ExpenseImport”复制到下一个空白行的“ CMiCExport”。由于数据已映射,因此将始终从A列到AE,但行将始终根据该特定星期的条目量而变化。当我进入并使用“ F8”运行代码时,它可以正常工作,但是当使用活动控件运行代码时,它会失败。有人可以帮我吗?

解决方法

我认为是因为空白单元格。试试这个:

Private Sub TransferExpenses_Click()
     ThisWorkbook.Activate   'Transfer ExpenseImport Data over to CMiCExport Tab
     Sheets("ExpenseImport").Select
     Range("A1:AE1",Selection.End(xlDown)).Select
     'Avoid the copy if all cells are blank
     If Application.WorksheetFunction.CountBlank(Selection) = Application.WorksheetFunction.CountBlank(Selection) Then
         Exit Sub
     End If
     Selection.Copy
     ThisWorkbook.Activate
     Sheets("CMiCExport").Select
     Sheets("CMiCExport").Range("A1").Select
     Selection.End(xlDown).Select
     ActiveCell.Offset(1,0).Range("A1").Select
     Sheets("CMiCExport").Paste
 
     MsgBox Title:="Expenses Imported Successfully!",Prompt:="The data for your expenses was verified and transferred to the CMiCExport Tab. Please double check column C -Job & Scope- and revise the .XXDefault entries."
End Sub

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...