【问题标题】:Excel copy data from one sheet to another on worksheet refresh with vbaExcel在使用vba刷新工作表时将数据从一张表复制到另一张表
【发布时间】:2016-11-19 16:56:12
【问题描述】:

我想将数据从特定单元格中的“Sheet1”复制到“Sheet2”中的下一个可用空单元格,这样更改 (F9) 会导致从 Sheet1 复制数据并在 Sheet2 中填充一列数据.

到目前为止,我有以下内容,这会引发错误:(.Rows.Count

每次按“F9”刷新工作簿时,是否可以在 Sheet2 中填充一列?

'In this example I am Copying the Data from Sheet1 (Source) to Sheet2              (Destination)
Private Sub worksheet_calculate()
'Method 2
'Copy the data
Sheets("Sheet1").Range("I1").Copy
'Activate the destination worksheet
Sheets("Sheet2").Activate
'Select the target range
Sheet2.Cells(.Rows.Count, "A").End(xlUp).Row
'Paste in the target destination
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub

【问题讨论】:

  • 试试我下面回答中的代码,让我知道它是否如您所愿

标签: vba excel


【解决方案1】:

您错误地选择了最后使用的行。试试这个:

Private Sub worksheet_calculate()
Dim lstrow As Integer
'Method 2
'Copy the data
Sheets(1).Range("I1").Copy
'Activate the destination worksheet
Sheets(2).Activate
'Select the target range
lstrow = Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row
'Paste in the target destination
ActiveSheet.Cells(lstrow + 1, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub

【讨论】:

    【解决方案2】:

    您是否只对粘贴值感兴趣

    Private Sub worksheet_calculate()
       With Worksheets("Sheet2")
           .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("I1").Value
        End With
    End Sub
    

    【讨论】:

    • @StevenBooth,你通过了吗?
    【解决方案3】:

    试试下面的代码,它会将单元格 I1 从“Sheet1”复制到“Sheet 2”的 A 列中的下一个可用行。

    最好避免使用ActivateActiveSheet,而是可以在一行中复制>粘贴(如下面的代码)

    不知道您为什么要在 worksheet_calculate 事件中使用此代码。

    'In this example I am Copying the Data from Sheet1 (Source) to Sheet2 (Destination)
    
    Private Sub worksheet_calculate()
    
    Dim LastRow As Long
    
    With Sheets("Sheet2")
        ' find last row in Sheet 2
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    
    'Copy the data and Paste in Sheet 2 (+1 for next avaialble row)
    Sheets("Sheet1").Range("I1").Copy Sheets("Sheet2").Range("A" & LastRow + 1)
    
    Application.CutCopyMode = False
    
    End Sub
    

    编辑 1:

    根据PO更新信息修改代码:

    为了避免F9 无限循环,请在Sub 中使用Application.EnableEvents = False

    Private Sub worksheet_calculate()
    
    'In this example I am Copying the Data from Sheet1 (Source) to Sheet2 (Destination)
    Dim LastRow As Long
    
    Application.EnableEvents = False
    
    With Sheets("Sheet2")
        ' find last row in Sheet 2
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row              
        ' copy the values only from "Sheet1" to "Sheet2"
        .Range("A" & LastRow + 1).Value = Sheets("Sheet1").Range("I1").Value
    End With
    
    Application.CutCopyMode = False    
    Application.EnableEvents = True
    
    End Sub
    

    您可以在一行中执行Copy>Paste,例如@user3598756 写道:

    With Worksheets("Sheet2")
       .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Value = Worksheets("Sheet1").Range("I1").Value
    End With
    

    【讨论】:

    • 谢谢 - 带着这个。 2个问题:1)这复制了“I1”中的公式,而不是值。我可以粘贴值吗? 2) 在这种情况下,这会导致“F9”上的无限循环。在按一次 F9 / 单个值打印到第 2 页后,有没有办法“结束”?谢谢
    • @StevenBooth 现在试试修改后的代码(在 Edit 1 下)
    • @StevenBooth 你在Edit 下测试过修改后的代码吗?它对你有用吗?
    • 抱歉,没有 - 使用 Edit 1 代码根本没有粘贴任何内容。
    • @StevenBooth 除非您移动了数据,否则我看不出它对您也不起作用。您可以上传工作表的屏幕截图吗?也许它会帮助我理解为什么它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2015-02-09
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多