根据单元格值复制范围和粘贴的宏
Macro to Copy Range and Paste Based on Cell Value
我创建了一个宏来复制数据并粘贴到另一个 sheet。
需要粘贴数据的单元格引用在table的最后一列。
范围 A2:E2 需要复制并粘贴到 "A2"(在 "H2" 中提到)
下面的代码不断给出错误"Object Required"
Google Doc Version of the Worksheet
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
您没有标注所有变量。如果它不能解决您的错误,请告诉我:
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
试试下面的代码(在示例 sheet 和描述中,目标位于 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
编辑:
我认为如果没有循环就无法实现,但我已将代码编辑为:
- 禁用屏幕更新
- 禁用事件
- 禁用公式计算
- 分配范围值而不是copy/paste
在我的电脑上测试 18000 行不到 3 秒就完成了。
我创建了一个宏来复制数据并粘贴到另一个 sheet。
需要粘贴数据的单元格引用在table的最后一列。
范围 A2:E2 需要复制并粘贴到 "A2"(在 "H2" 中提到)
下面的代码不断给出错误"Object Required"
Google Doc Version of the Worksheet
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
您没有标注所有变量。如果它不能解决您的错误,请告诉我:
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
试试下面的代码(在示例 sheet 和描述中,目标位于 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
编辑:
我认为如果没有循环就无法实现,但我已将代码编辑为:
- 禁用屏幕更新
- 禁用事件
- 禁用公式计算
- 分配范围值而不是copy/paste
在我的电脑上测试 18000 行不到 3 秒就完成了。