Import data from sheet2, after clear column data in sheet1

Yulyo

Board Regular
Joined
Jul 17, 2017
Messages
94
Hello all,

Please help me with a formula in sheet1 D17, to do this: (please download the test file from my Drive account)

https://drive.google.com/open?id=0B9tH3lTneqQUdWkySHlqVGFNQTg


- search for 16.10.17 in sheet2, column A, and copy the corespondent number from sheet2 column D, to sheet1 column D (in cell D17)

- sheet2 will be updated daily, so maybe tomorrow i i will not have a request in 16.10.17. In this case, the request in sheet1 cell D17 must be empty too.


I just found a VBA, but it does not solve the second problem, meaning does not delete the old values.
So if today I have a request for 15.10.2017, and after the tomorrow's update I have an request in 14.10.2017 and 16.10.2017, this vba does not delete the request from 15.10.2017. The thing is that i only need to delete the data starting from smallest date in sheet2, column A... (in this case, to clear cells in sheet1, starting 16.10.2017)

I hope i made myself understood.
Thank you in advance for any help you cand give. I'm really stuck right now...



This is the code i just found:
=======================
Option Explicit
Sub ReplaceData()
Dim lastRw1, lastRw2, nxtRw, m
'Determine last row with data, Sheet1
lastRw1 = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
'Determine last row with data, Sheet2
lastRw2 = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row
'Loop through Sheet 2, Column A
For nxtRw = 2 To lastRw2
'Search Sheet1 Column A for value from Sheet 2
With Sheets(1).Range("A2:A" & lastRw1)
Set m = .Find(Sheets(2).Range("A" & nxtRw), lookat:=xlWhole)
'Copy Sheet2 row if match is found
If Not m Is Nothing Then
Sheets(2).Range("A" & nxtRw).EntireRow.Copy _
Sheets(1).Range("A" & m.Row)
End If
End With
Next
End Sub
=======================
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Let me clarify. At the time that Sheet2!D17 is copied to Sheet1!D17, Sheet2!A17 is to become void, or is D17 to be voided?
I am trying to understand exactly what you mean here:
So if today I have a request for 15.10.2017, and after the tomorrow's update I have an request in 14.10.2017 and 16.10.2017, this vba does not delete the request from 15.10.2017. The thing is that i only need to delete the data starting from smallest date in sheet2, column A... (in this case, to clear cells in sheet1, starting 16.10.2017)
Am I believing that the request moved to Sheet1!D17 is not to be deleted? But That at Sheet2!D17will go? We are only concentrating on "today's" date, right?
 
Upvote 0
Let me clarify. At the time that Sheet2!D17 is copied to Sheet1!D17, Sheet2!A17 is to become void, or is D17 to be voided
Sheet2!A17 will remain the same. I am not changing any data to Sheet2.

I changed the file a little bit, please download again from this link:
https://drive.google.com/open?id=0B9tH3lTneqQUeEdfVV9mSmRlYjA


Thing is like this:
- if today i recieve a request from customer (sheet "first_request"), that i must send 444pcs in 16.10.2017 (first_request!D2), i copy the 444 (pcs) from first_request!D2 to delievery!D17.
- if later today i will recieve another request from customer (sheet "second_request), that i must send 555 pcs in 17.10.2017 (second_request!D2) and 666 pcs in 23.10.2017 (second_request!D3), than i must copy 555(pcs) from second_request!D2 to delievery!D18 and 666 (pcs) from second_request!D3 to delievery!D24. after this i must delete the value in delievery!D17, because the request changed and i don't have any request in 16.10.2017.

Thank you!
 
Upvote 0
So right now i must edit the above vba, so when i execute it, to first delete the numbers in sheet named "delievery" column D, stating with the exact same date as the first date in sheet "first_request" column A.
Any help?
 
Upvote 0
Sheet 1 initially:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Request[/TD]
[/TR]
[TR]
[TD]16.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]17.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]18.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]19.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]20.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]21.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]22.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]23.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]24.10.2017[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


















Sheet 2 initially:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Request[/TD]
[/TR]
[TR]
[TD]16.10.2017[/TD]
[TD]444[/TD]
[/TR]
[TR]
[TD]17.10.2017[/TD]
[TD]555[/TD]
[/TR]
[TR]
[TD]23.10.2017[/TD]
[TD]666[/TD]
[/TR]
[TR]
[TD]24.10.2017[/TD]
[TD]777[/TD]
[/TR]
</tbody>[/TABLE]










After the first update, Sheet1 should look something like this:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Date[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]16.10.2017[/TD]
[TD]444[/TD]
[/TR]
[TR]
[TD]17.10.2017[/TD]
[TD]555[/TD]
[/TR]
[TR]
[TD]18.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]19.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]20.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]21.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]22.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]23.10.2017[/TD]
[TD]666[/TD]
[/TR]
[TR]
[TD]24.10.2017[/TD]
[TD]777[/TD]
[/TR]
</tbody>[/TABLE]


















Tomorrow i will recieve a new request (Sheet2), that looks like this:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Request[/TD]
[/TR]
[TR]
[TD]17.10.2017[/TD]
[TD]888[/TD]
[/TR]
[TR]
[TD]24.10.2017[/TD]
[TD]999[/TD]
[/TR]
[TR]
[TD]26.10.2017[/TD]
[TD]1111[/TD]
[/TR]
[TR]
[TD]28.10.2017[/TD]
[TD]7878[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]










After the second update, Sheet1 should look like this:
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Request[/TD]
[/TR]
[TR]
[TD]16.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]17.10.2017[/TD]
[TD]888[/TD]
[/TR]
[TR]
[TD]18.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]19.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]20.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]21.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]22.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]23.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]24.10.2017[/TD]
[TD]999[/TD]
[/TR]
[TR]
[TD]25.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]26.10.2017[/TD]
[TD]1111[/TD]
[/TR]
[TR]
[TD]27.10.2017[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]28.10.2017[/TD]
[TD]7878[/TD]
[/TR]
[TR]
[TD]29.10.2017[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Try this:
Code:
Sub ReplaceData()
Dim lastRw1, lastRw2, nxtRw, m
'Determine last row with data, Sheet1
lastRw1 = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
[B]Range("Sheet1!B2:B" & lastRw1).ClearContents[/B]
'Determine last row with data, Sheet2
lastRw2 = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row
'Loop through Sheet 2, Column A
For nxtRw = 2 To lastRw2
'Search Sheet1 Column A for value from Sheet 2
With Sheets(1).Range("A2:A" & lastRw1)
Set m = .Find(Sheets(2).Range("A" & nxtRw), lookat:=xlWhole)
'Copy Sheet2 row if match is found
If Not m Is Nothing Then
Sheets(2).Range("A" & nxtRw).EntireRow.Copy _
Sheets(1).Range("A" & m.Row)
End If
End With
Next
End Sub
My bold line clears data in Sheet1 Column B (other than the header) before your Macro continues as I suspect that you expect.
 
Upvote 0
Hello BrianJN1,
Thank you for your help :) It does 90% of what i need. I just don't need to clear the entire column.
Is there a chance to specify this: clearcontents starting with selected cell?

10x
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,596
Members
452,657
Latest member
giadungthienduyen

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