[英]VBA Excel copying range of formulas from one worksheet to another
I am trying to copy a couple of ranges with formulas from one worksheet to another. 我正在尝试将两个范围的公式从一个工作表复制到另一个工作表。 As the contents of the "Schedule" ranges change, they are copied to different ranges on the "Data_Design" worksheet.
随着“计划”范围的内容更改,它们将被复制到“ Data_Design”工作表上的不同范围。 The code that I have works intermittently but I cannot determine why it is not stable and repeatable.
我拥有的代码会间歇性地工作,但是我无法确定为什么它不稳定和可重复。 I have tried a couple of different variations of code but to no avail.
我尝试了几种不同的代码变体,但无济于事。 When I get the runtime error (1004) it is at the first line of code equating the ranges.
当我收到运行时错误(1004)时,它在代码的第一行等于范围。 Any idea why the 1004?
知道为什么1004吗?
Sub Design_Save()
Dim A, i, D, Ans As Integer
Redo:
D = Application.InputBox("Enter Design Number" & vbNewLine & vbNewLine & "Note: Default value = current stage number", "Design Number Assignment", Worksheets("Job").Cells(10, 3).Value, Type:=1)
For i = 2 To 101 ' Revise the 500 to include all of your values
A = Worksheets("Data_Design").Cells(i, 1).Value
If A = D Then
Ans = MsgBox("Design Number " & D & " already exists." & vbNewLine & vbNewLine & "Overwrite exiting design?", vbYesNo + vbQuestion, "Overwrite Design")
If Ans = vbYes Then
Save_Design (D)
Else
GoTo Redo
End If
Else
Exit Sub
End If
Next i
End Sub
Sub Save_Design(A As Integer)
Dim r1, r2 As Integer
Dim dd1, dd2, dd3, dd4, dd101, dd102, dd103, dd104 As Range
If A = 0 Then
r1 = 1
Else
r1 = A * 100
End If
r2 = r1 + 49
Worksheets("Data_Design").Range(Cells(r1, 2), Cells(r2, 6)).Formula = Worksheets("Schedule").Range("C5:G54").Formula
Worksheets("Data_Design").Range(Cells(r1, 7), Cells(r2, 8)).Formula = Worksheets("Schedule").Range("I5:J54").Formula
Worksheets("Data_Design").Range(Cells(r1, 9), Cells(r2, 23)).Formula = Worksheets("Schedule").Range("L5:Z54").Formula
Worksheets("Data_Design").Range(Cells(r1, 24), Cells(r1, 38)).Formula = Worksheets("Schedule").Range("L3:Z3").Formula
End Sub
You need to assign proper parentage to the Cells()
range objects. 您需要为
Cells()
范围对象分配适当的父项。 Try this 尝试这个
With Worksheets("Data_Design")
.Range(.Cells(r1, 2), .Cells(r2, 6)).Formula = Worksheets("Schedule").Range("C5:G54").Formula
.Range(.Cells(r1, 7), .Cells(r2, 8)).Formula = Worksheets("Schedule").Range("I5:J54").Formula
.Range(.Cells(r1, 9), .Cells(r2, 23)).Formula = Worksheets("Schedule").Range("L5:Z54").Formula
.Range(.Cells(r1, 24), .Cells(r1, 38)).Formula = Worksheets("Schedule").Range("L3:Z3").Formula
End With
Using the with block makes it so anything that starts with a .
使用with块可以使其以
.
开头.
is assigned to the with block parentage. 被分配给with块父项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.