PCosta87
Well-Known Member
Hi,
This is correct... if last row number of location 4 is >1 it means that the file has more data than just the headers, meaning we can copy.
Usually we would just copy and paste after the last used row (as it was in the original code), but with a Table as destination it is not that simple.
You see, when the Table has data (one row at least) there is no problem. However, when it is empty, it will retain an empty row below the headers. If you look for the last row with an empty Table, you will get row 2 and not row 1 as you would without it. That's because Excel interprets that empty row as being in use.
I had to make sure that something had already been copied before starting to paste after the last row. Since we had a variable for each of the locations (lrow1, lrow2, lrow3 and so on) I simply checked if any of the previous locations had any data (lrow(x) >1) and if so i could resume pasting after the last row... if not, I knew I needed to paste from row 2 downwards.
For instance, you could check if "A2" (or any of the cells in the second row) isn't empty and if so paste after the last row. I opted for checking the variables because I wasn't sure if I could rely on that.
Imagine that, for some reason, you had a missing Sr# in the 2nd row of Loc1, or the entire 2nd row for that matter. You would still copy from Loc1 to the destination Table because lrow1 would be >1 (there would be data after row 2) but then the 2nd row in the destination sheet would be empty. Checking for that when copying from any of the following locations would trigger the paste action from row 2 downwards thus deleting the previously copied data from Loc1.
Not really, no... if none of the previous lrow is >1 it means that no data has been copied yet so we can paste in table 2... in other words, we paste from row 2 downwards.
Pasting in Table2 means pasting immediately below the headers.
The beauty of Excel is that there are multiple ways of completing a specific task... that being said, slightly tweaked, I'm sure this code will also work.
I'm obviously not saying there aren't any other, perhaps better, ways of doing it![Smile :) :)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
It defines and locates lrow4 which is the last row of location 4 and if it is More than 1, it copies range A2 to n till last row...
This is correct... if last row number of location 4 is >1 it means that the file has more data than just the headers, meaning we can copy.
Question 1....Once copied.......why do you check again for lrow3, lrow2, lrow1 also as greater than 1 ?? I did not understand why this condition??
Usually we would just copy and paste after the last used row (as it was in the original code), but with a Table as destination it is not that simple.
You see, when the Table has data (one row at least) there is no problem. However, when it is empty, it will retain an empty row below the headers. If you look for the last row with an empty Table, you will get row 2 and not row 1 as you would without it. That's because Excel interprets that empty row as being in use.
I had to make sure that something had already been copied before starting to paste after the last row. Since we had a variable for each of the locations (lrow1, lrow2, lrow3 and so on) I simply checked if any of the previous locations had any data (lrow(x) >1) and if so i could resume pasting after the last row... if not, I knew I needed to paste from row 2 downwards.
For instance, you could check if "A2" (or any of the cells in the second row) isn't empty and if so paste after the last row. I opted for checking the variables because I wasn't sure if I could rely on that.
Imagine that, for some reason, you had a missing Sr# in the 2nd row of Loc1, or the entire 2nd row for that matter. You would still copy from Loc1 to the destination Table because lrow1 would be >1 (there would be data after row 2) but then the 2nd row in the destination sheet would be empty. Checking for that when copying from any of the following locations would trigger the paste action from row 2 downwards thus deleting the previously copied data from Loc1.
Question 2....
If the or condition is false...... we simply paste in Table2 ....Does it mean the data will just get appended automatically to the last row of the table??
Not really, no... if none of the previous lrow is >1 it means that no data has been copied yet so we can paste in table 2... in other words, we paste from row 2 downwards.
Pasting in Table2 means pasting immediately below the headers.
Question 3..... not about above....but for a different consolidation need...
I have one more need of consolidation .....I need to consolidated approximately 50 to 100 rows appended each month at 10 locations to a Trainings Sheet (annually about 600 to 800 training rows) and 500 to 1000 rows appended each month for same locations in Employees Sheet (annually approximately 8000 to 12000 rows)......Eventually I do a lot of calculations on this data and prepare a dashboard.
Would it be a good idea to adopt the code you gave for this job too??
The beauty of Excel is that there are multiple ways of completing a specific task... that being said, slightly tweaked, I'm sure this code will also work.
I'm obviously not saying there aren't any other, perhaps better, ways of doing it