[英]How to copy the values in a range from one sheet to and paste it another sheet on a predefied order using VBA?
I am trying to copy the values in a specific range from one sheet to another.我试图将特定范围内的值从一张纸复制到另一张纸。 While pasting the values to the new sheet, there should be a predefined order in which the values are pasted.
将值粘贴到新工作表时,应该有预定义的值粘贴顺序。
I have created the program, but there comes an error in definition of For loop.我已经创建了程序,但是在 For 循环的定义中出现错误。 Please note that in this example Num_Tacksta = 2, m = 9 (These two are variables).
请注意,在此示例中 Num_Tacksta = 2, m = 9(这两个是变量)。
Sub New_Try(m)
Dim n, i, j, x, k, a, rowinres, Num_Tacksta, Num_TackMul As Variant
Num_Tacksta = Sheets("ALLO").Range("E4").Value
Num_TackMul = (Sheets("ALLO").Range("E4").Value) * 2
x = Sheets("ALLO").Range("E4").Value
For rowinres = 2 To Num_Tacksta Step 1
For i = 2 To Num_TackMul Step 2
Sheets("Final").Range("A" & rowinres &, ",M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & i)
rowinres = rowinres + 1
Next i
For j = 3 To Num_TackMul Step 2
Sheets("Dummy_Result").Range("A" & rowinres &, ",M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & j)
rowinres = rowinres + 1
Next j
For k = Num_TackMul + 1 To m Step 1
Sheets("Dummy_Result").Range("A" & rowinres &, ",M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & k)
rowinres = rowinres + 1
Next k
Next rowinres
End Sub
To my basic Knowledge in VBA, I think the problem is in this Sheets("Dummy_Result").Range("A:M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & j)
line.根据我在 VBA 中的基本知识,我认为问题出在这个
Sheets("Dummy_Result").Range("A:M" & rowinres).Copy Destination:=Sheets("Results").Range("A" & j)
线。 I would like to also ask if there is any simple method available.我也想问有没有什么简单的方法可以用。
I am new to programming and also to Stack Overflow.我是编程新手,也是 Stack Overflow 的新手。 Helping me in this would be really helpful!
在这方面帮助我真的很有帮助! I thank you guys in advance.
我提前感谢你们。
Try colon instead of comma in your copy range:在复制范围内尝试冒号而不是逗号:
Sheets("Results").Range("A" & rowinres & ":M" & rowinres).Copy Destination:=Sheets("Final").Range("A" & i)
A comma is used in functions such as SUM() but a colon is used to specify a range.逗号用于SUM()等函数,但冒号用于指定范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.