Copy data on sheet2 to sheet1 base on column data.

Tocix

New Member
Joined
Apr 17, 2016
Messages
48
Office Version
  1. 365
Please help me out. I want a vba that can automate this process that copy sheet2 to sheet1. For example on sheet2. Column D4 fruit column separate by a blank line with a new fruit with dates, purchase orders and quantity. There's six types of fruit that I need to keep track. What I want to do is to copy fruit types on column D all rows that are "apple" and copy data on column A,B and C and paste it to sheet1 on A9,B9 and C9 for Apple and D9,E9 and F9 for banana and so on. I'm sorry I'm trying to explain as best as I can.

Thank you very much.



A9 will always be apple
D9 will always be banana
G9 will always be grapes

example below

abcDEFGHI
datePOqtydatePOqtydatePOqty
93/18/20192557894/21/20191830094/25/20191228
103/23/2019 6060904/24/20193958814/28/20199964
11
12

<tbody>
</tbody>
Sheet1
ABCD
datePOQtyFruit
43/18/2019255789Apple
53/23/2019606090Apple
63/28/2019303098Apple
7
84/21/2019183009Banana
104/24/2019395881Banana
114/24/2019683068Banana
12
134/25/20191228Grapes
144/28/20199964

<tbody>
</tbody>
Sheet2

<tbody>
</tbody>



****** id="cke_pastebin" style="position: absolute; top: 312px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">
4/28/2019

<tbody>
</tbody>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this

Code:
Sub copy_data()
  Dim sh2 As Worksheet, r As Range, j As Long, c As Range
  Set sh2 = Sheets("Sheet2")
  j = 1
  For Each r In sh2.Range("A2:C" & sh2.Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeConstants).Areas
    Set c = Union(sh2.Range("A1:C1"), r)
    c.Copy Sheets("Sheet1").Cells(8, j)
    j = j + 3
  Next
End Sub
 
Last edited:
Upvote 0
What error message the code sends you and on which line the macro stops.

If your data is on sheet2 as your initial example, you will see the result on sheet1.
 
Upvote 0
Your code work on sample sheet, but it does not work on my real sheet. gives run-time error "1004" macros stop at c.Copy Sheets("Sheet1").Cells(8, j). I tried adjusting code with no success.

Thanks
 
Upvote 0
Your code work on sample sheet, but it does not work on my real sheet. gives run-time error "1004" macros stop at c.Copy Sheets("Sheet1").Cells(8, j). I tried adjusting code with no success.

Thanks


What else says the error?
Do you have the protected sheet, or merged cells or what is the real example?
 
Upvote 0

Forum statistics

Threads
1,215,253
Messages
6,123,891
Members
449,131
Latest member
leobueno

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top