简体   繁体   中英

excel vba copy from one sheet to an other in a certain order

I have this problem with an excel file. I have in this file 200 sheets in which always in the same cells (range O2:O6) I have certain values to copy in a unique sheet (named "Final") in the same column CI I would like also to order this values that I copy and paste; I would (maybe) use the command if else to understand the order: in fact in cell A1 of all the sheets there is the name of reference of that sheet (some sheets have name A, others B, others C);I must put in order in the sheet Final, before the values of sheets with name A then values with name B and then values of sheets with name C. so in practice I would do, "If (in the cell A1 of each sheet is present letter A) then (copy the relative values)" then all values of sheets with name B in that cell, and then name C. Could you help me?

This will get you started, it loops through the sheets and copies the range to "Final" sheet.

It does not copy the sheets in order, you can use my example and create a loop that will do that.

Sub Do_Something()
    Dim sh As Worksheet
    Dim ws As Worksheet


    Set ws = Sheets("Final")

    With ws
        For Each sh In Sheets
            If sh.Name <> ws.Name Then
                sh.Range("O2:O6").Copy .Cells(.Rows.Count, "C").End(xlUp).Offset(1)
            End If
        Next sh
    End With

End Sub

But my problem is the order, to copy is ok. i would want to copy that range but before from some sheet and then from others, regarding the name contained in that cell

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