简体   繁体   中英

Using macro to copy multiple ranges from one sheet, and paste them into another sheet in the first empty row

I have to copy hundreds of 2700 row x 3 column ranges, and then paste them into another sheet in the first empty row, (so the first range copied from sheet "CPR Test 2013-2015" will be pasted into row 1 on the sheet "Maps", the second range will be pasted into row 2701 in "Maps", the third range will be pasted into row 5402 in "Maps", etc.). If it helps to know why I'm doing this, I've included the explanation at the bottom. If not, I'll continue the nuts and bolts of the problem here:

Each range that needs to be copied is separated from the next range by 6 columns. So, for example, the first range is (XG2:XI2700), six columns later we have the second range (XP2:XR2700), and the last range is (BHP2:BHR2700).*

*Note: XG is column 631, XI is column 633; XP is column 640, XR is column 642; BHP is column 1576, BHR is column 1578.

When pasting the ranges into the sheet "Maps", it would be nice to be able to paste them in the first empty row, since the number of rows in the ranges will vary in the future. If this can't be done, pasting the ranges every 2700 rows into "Maps" will work for now.

First range to be copied

Pasting the first range in "Maps"

Here is some code i've tried in the VBA editor (bear with me, I'm a beginner):

Sub CPR_to_Calculator()
'
' CPR_to_Calculator Macro
'

'
Application.ScreenUpdating = False
Sheets("CPR Test 2013-2015").Select
    For ColNum = 631 To 1578 Step 7
        Sheets("CPR Test 2013-2015").Select
        Range(Cells(2, ColNum), Cells(2700, ColNum + 2)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Sheets("Maps").Select
        Selection.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial     xlPasteValues
            Application.CutCopyMode = False
            Application.ScreenUpdating = True
    Next ColNum

End Sub

Why I'm doing this: I'm calculating the distance between students and universities in the sheet "Maps" using data from the sheet "CPR 2013-2015". The sheet "Maps" is already set up to calculate the distance between zip codes with the push of a button.

The three columns in each range in "CPR Test 2013-2015" are ID, Student Zip Code, and School Zip Code. I need to calculate the distance between the Student Zip Codes and the School Zip codes in the sheet "Maps". Since each range pertains to a specific university, and there are hundreds of universities in the data set, I don't want to calculate the distances between the student and school zip codes for one university at a time. I would rather copy the ranges for all the universities, and paste them into the "Maps" sheet stacked on top of each other. This way, I can just click the calculate button in "Maps", and it will calculate the distance between the zip codes in each row. I can then use INDEX MATCH formulas in "CPR Test 2013-2015" to pull the distance data for each unique ID.

Unless you want to preserve the formmating; it's better to assign the values directly.

Sub CPR_to_Calculator()
    Dim y As Long

    With Worksheets("CPR Test 2013-2015")
        For y = 631 To 1582 Step 9

           Worksheets("Maps").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(2699, 3).Value = .Cells(2, y).Resize(2699, 3).Value

        Next
    End With

End Sub

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