I have a sheet A that looks something like this:
Name Phone Number Title Id
Ron 08900108 Carpenter 440
Bob 48951230 Painter 830
Ron 14890560 Carpenter 561
Steve 98023023 Carpenter 112
and another sheet called Carpenter (and another called Painter on the same formatting)
Name Phone Number Id Comment
Ron 08900108 440 Cool dude
Bob 48951230 830 Less Awesome Dude
I want to copy the rows of A to the sheets called Carpenter and Painter, putting the Name, Phone Number on the same line as the existing ID (if someone has changed number for example) or add a new line if it is a new id. I want to keep the exising comments.
Since I have no experience whatsoever in VBA, you can help me by either providing the code solving the problem or hints on what functions(?) and other language constructs to use and where I can find information about them.
I guess I need to iterate over the rows in A and copying it into an array which I send to the correct sheet in some way.
First of all, consider the fact that you are using Excel for operations that are typically database related and can easily be done using Access.
I notice more and more the fact that Excel is being used as a datasheet. While in some circumstances this may be practical, let’s not forget that the main function of Excel is a spreadsheet and NOT a database.
If you still insist on doing this in Excel, there are many ways to do this:
1) Load the source range into an array, as well as the destiny range. Start comparing the two loaded arrays and perform operations at will; When needing to set a new id, you can extend the array dynamically and use ubound(array) to retrieve the size of the array. You can also set the range dynamically using the size of your array as input parameter. Up to you to figure out how it needs to be done for your case.
2) Create a custom self sustaining class for each sheet that returns a dictionary of instances representing the records. Dictionaries allow key property-usage. This can also be the base for further manipulations. It may seem as a rather weird way, but it works but I wouldn’t use it for a mass amount of data).
Benefit is that this class is rather flexible and can easily be called again later on with very limited code.
3) Use ADODB to perform queries on the sheets (don’t forget to reference the library). ADODB allows you to treat an Excel sheet as table where the first row of the sheet is considered as field header.
Any of these methods would eventually work but you’re making it hard on yourself.
As I said, Access if far better at this because you can create a relational table based on foreign keys.
If you need these data in Excel, it is not a problem since you can still create a connection between Excel – Access using ADO.
Sorry to put this as a reply, but since I’m not often on Stack Overflow, I cannot post comments.
Although I cannot write the code for you, I hope this may help you on the way.