简体   繁体   English

宏以基于单元格值复制范围和粘贴

[英]Macro to Copy Range and Paste Based on Cell Value

I had created a macro to copy the data and paste into another sheet. 我创建了一个宏来复制数据并粘贴到另一张纸上。

The cell reference where the data needs to be pasted is in the last column of table. 表的最后一列是需要粘贴数据的单元格引用。

Range A2:E2 needs to be copied and paste at "A2" (mentioned in "H2") 范围A2:E2需要复制并粘贴到“ A2”(在“ H2”中提到)

The below code constantly gives and error "Object Required" 下面的代码不断给出错误“ Object Required”

Google Doc Version of the Worksheet 工作表的Google文档版本


Sub Reconcile()

Set i = Sheets("Data")
Set e = Sheets("Final")

Dim r1 As Range
Dim r2 As Variant
Dim j
j = 2
Set r1 = i.Range(Cells(j, 1), Cells(j, 5))
Set r2 = i.Cells("I" & j).Value

Do Until IsEmpty(i.Range("A" & j))
    r1.Select
    Selection.Copy
    e.Range(r2).Select
    Selection.Paste
    j = j + 1
Loop

End Sub

You didn't dimension all your variables. 您并未确定所有变量的尺寸。 Let me know if it doesn't fix your error: 让我知道它是否不能解决您的错误:

Sub Reconcile()

Dim i as Worksheet
Dim e As Worksheet
Dim r1 As Range
Dim r2 As Variant
Dim j As Integer

Set i = Sheets("Data")
Set e = Sheets("Final")

j = 2
Set r1 = i.Range(Cells(j, 1), Cells(j, 5))
Set r2 = i.Cells("I" & j).Value

Do Until IsEmpty(i.Range("A" & j))
    r1.Select
    Selection.Copy
    e.Range(r2).Select
    Selection.Paste
    j = j + 1
Loop

End Sub

Try the following code (in the sample sheet and in the description the target is in H column, not I as in sample VBA) 尝试以下代码(在示例表和说明中,目标位于H列中,而不是示例VBA中的I

Sub Reconcile()

Set i = Sheets("Data")
Set e = Sheets("Final")

Dim r1 As Range
Dim r2 As Range
Dim j As Integer
j = 2

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Do Until IsEmpty(i.Range("A" & j))
    Set r1 = i.Range(Cells(j, 1), Cells(j, 5))
    Set r2 = e.Range(i.Range("H" & j).Value)
    r2.Resize(1, 5).Value = r1.Value
    j = j + 1
Loop

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic

End Sub

EDIT: 编辑:

I don't think you can achieve that without a loop, but I have edited the code to: 我认为没有循环就无法实现,但是我将代码编辑为:

  • disable screen updates 禁用屏幕更新
  • disable events 禁用事件
  • disable formula calculation 禁用公式计算
  • assign range values instead of copy/paste 指定范围值,而不是复制/粘贴

On my computer test with 18000 rows finished in less than 3 seconds. 在我的计算机上,在不到3秒的时间内完成了18000行的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于单元格值的Excel宏复制范围粘贴偏移量 - Excel Macro Copy Range Paste offset based on cell value 根据单元格值复制/粘贴列范围 - Copy/Paste column range based on cell value Excel宏,要基于另一个单元格值复制和粘贴单元格值? - Excel macro, to copy and paste a cell value based on another cell value? 宏根据特定的单元格值将行粘贴范围复制到不同的工作表中 - Macro to copy-paste range in row to different sheets based on specific cell value 宏根据特定单元格的值向左重复复制粘贴 - Macro to repeat copy paste to the left based on the value of a particular cell VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏 - VBA Macro to copy/paste cell value from sheet 1 to 2 based on date Excel宏,根据另一个单元格值复制和粘贴多个单元格? - Excel macro, to copy and paste a multiple cells based on another cell value? Excel宏复制单元格范围并将数据粘贴到另一个工作表 - Excel Macro Copy cell range & paste data one sheet to another Excel VBA宏-将单元格和粘贴值复制到活动单元格范围 - Excel VBA Macro - Copy Cells and Paste Values to Active Cell Range 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM