0

首先,我是 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”进入并运行代码时,它工作得很好,但是当使用活动控件运行代码时,它会失败。有人能帮我吗?

4

1 回答 1

0

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

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
于 2020-08-19T14:00:36.163 回答