Hi
I've got a sheet with data and the Department name in each row.
I'd like to replace data for a specific Department when I import data from a folder.
I've written the code to import the data. But I'd like to know if it's possible to write code that will search for a Department name in the worksheet I'm importing data into, then replace all rows that relate to that specific Department?
To illustrate, I've created a simple example below. It has two Departments - Chocolate and Cereals, over 5 rows.
There are TWO tabs - Sheet 1 and Sheet 2 - the only difference is the names and prices of products the Cereals in Sheet 2.
So is it possible to create code that OVER-WRITES the Cereals data, ONLY, in Sheet 1 (assuming that the new Cereals data has already been copied)?
Data for Sheet 1 (paste into cell A1)
[TABLE="width: 264"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Department[/TD]
[TD]Product[/TD]
[TD]Price[/TD]
[/TR]
[TR]
[TD]Chocolate[/TD]
[TD]Choc 1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Chocolate[/TD]
[TD]Choc2[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Cornflakes[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]All Bran[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Oats Porridge[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]
Data for Sheet 2 (paste into cell A1 of Sheet 2)
[TABLE="width: 257"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Department[/TD]
[TD]Product[/TD]
[TD]Price[/TD]
[/TR]
[TR]
[TD]Chocolate[/TD]
[TD]Choc 1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Chocolate[/TD]
[TD]Choc2[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Muesli[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Lucky Charms[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Cheerios[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]
I've got code below which copies data from Sheet 2 then pastes it into Sheet 1. But I'd like to be able to replace specific Departments eg Cereals only or Chocolate only, when I've copied new data for that Department.
Thanks in advance
I've got a sheet with data and the Department name in each row.
I'd like to replace data for a specific Department when I import data from a folder.
I've written the code to import the data. But I'd like to know if it's possible to write code that will search for a Department name in the worksheet I'm importing data into, then replace all rows that relate to that specific Department?
To illustrate, I've created a simple example below. It has two Departments - Chocolate and Cereals, over 5 rows.
There are TWO tabs - Sheet 1 and Sheet 2 - the only difference is the names and prices of products the Cereals in Sheet 2.
So is it possible to create code that OVER-WRITES the Cereals data, ONLY, in Sheet 1 (assuming that the new Cereals data has already been copied)?
Data for Sheet 1 (paste into cell A1)
[TABLE="width: 264"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Department[/TD]
[TD]Product[/TD]
[TD]Price[/TD]
[/TR]
[TR]
[TD]Chocolate[/TD]
[TD]Choc 1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Chocolate[/TD]
[TD]Choc2[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Cornflakes[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]All Bran[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Oats Porridge[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]
Data for Sheet 2 (paste into cell A1 of Sheet 2)
[TABLE="width: 257"]
<colgroup><col><col><col></colgroup><tbody>[TR]
[TD]Department[/TD]
[TD]Product[/TD]
[TD]Price[/TD]
[/TR]
[TR]
[TD]Chocolate[/TD]
[TD]Choc 1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Chocolate[/TD]
[TD]Choc2[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Muesli[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Lucky Charms[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]Cereal[/TD]
[TD]Cheerios[/TD]
[TD]6[/TD]
[/TR]
</tbody>[/TABLE]
I've got code below which copies data from Sheet 2 then pastes it into Sheet 1. But I'd like to be able to replace specific Departments eg Cereals only or Chocolate only, when I've copied new data for that Department.
Thanks in advance
Code:
Sub replacementcode()
Sheet2.Activate
Range("A1").CurrentRegion.Copy
Sheet1.Activate
Range("A1").Activate
ActiveCell.PasteSpecial (xlPasteAll)
End Sub