简体   繁体   中英

VBA code to copy rows from one excel sheet to another

The original spreadsheet was from column A to E. I just pasted the numbers in column F from another sheet. My new spreadsheet has 1517 rows and goes from columns AH. The numbers in column F (row 2 to row 420) are also in column C.

I was able to use the MATCH function of Excel to find the exact position of a number in column C ie In column C, on which row can I find the numbers that are in column F? For instance, the number "12345678910" in cell F2 can be found in cell C1049,on row 1049. I put those position numbers in a column that I called "index".

Now, the goal is to write a vba code to copy those rows at once. I am trying to extract/copy to another sheet the numbers in column F that are also in column C but I only want columns A to E - from the original sheet. Copy each row number that is in the index column but only the columns AE. To be clear, "12345678910" is also in cell C1049. "Copy row 1049 from column AE" and do that for each position number in the index column.

I cannot sort the spreadsheet because the columns AE are linked. A sort would alter the information. Any help would be appreciated.

A sample of my spreadsheet

Sub filterCopy()

   Dim ws1 As Worksheet
   Dim ws2 As Worksheet

   Set ws1 = Sheet1
   Set ws2 = Sheet2

   ws1.Range("A1:F1517").AutoFilter 6, "<>"
   ws1.Range("A1:F1517").SpecialCells(xlCellTypeVisible).Copy ws2.Range("A1")

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