简体   繁体   中英

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. Please note that in this example Num_Tacksta = 2, m = 9 (These two are variables).

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. I would like to also ask if there is any simple method available.

I am new to programming and also to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM