简体   繁体   中英

EXCEL VBA Trying to copy data from non consecutive cells on one worksheet to the next available row on separate worksheet

I am very new to all this and have been taking bits from here and there on various excel websites and have managed to get most of what i need working macro-wise. But this one has me stumped..

I have an order from on a worksheet labeled "Order Form" that is filled in by my colleagues for each new order, i need them to be able to click a button and then all the data they have filled in will be copied to another worksheet labeled "Orders". I have managed to get the data copied but the best i could get was the data pasted into the exact same cells on the "Orders" worksheet.

Heres what i have code wise this works for copying...

Dim copyRng As Range, cel As Range, _
    pasteRng As Range

Set copyRng = ThisWorkbook.Sheets("Order Form").Range("B3,F3,C10,C11,C12,C13,C14,C15,D21,D22,D23,D24,D25,D26,D27,D28,D29,D30,D31,D32,D33,D34,E48,D36,G10,C40,G40")
Set pasteRng = ThisWorkbook.Sheets("Orders").Range("A2") 

What i need is to paste the data that is copied above into the next blank row on the "orders" worksheet. I know that code is very messy and is probably overkill for copying those cells and its probably really simple to do but i cannot work it out... Please someone help its doing my head in lol

You can't copy and paste multiple selections. Quick and easy solution is to do one cell at a time.

Dim pasterange As Long
pasterange = ThisWorkbook.Sheets("Orders").Range("A" & Rows.Count).End(xlUp).Row + 1 ' the plus 1 is to get the next blank row

ThisWorkbook.Sheets("Order Form").Range("B3").Copy Destination:=ThisWorkbook.Sheets("Orders").Range("A" & pasterange)
ThisWorkbook.Sheets("Order Form").Range("F3").Copy Destination:=ThisWorkbook.Sheets("Orders").Range("B" & pasterange)
ThisWorkbook.Sheets("Order Form").Range("C10").Copy Destination:=ThisWorkbook.Sheets("Orders").Range("C" & pasterange)

I would just copy and paste that down and add the additional cells you need copied and continue the A,B,C, etc in the destination portion.

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