Forum Discussion
VBA to append multiple columns to last column across multiple worksheets
What should the code look up in that lookup table?
Hi Hans - I do hope there is some viable code to help me append columns to the end of my downloaded data sets across multiple Worksheets (within the same Workbook) as noted above. I wanted to follow up to see if any additional clarifications are needed. I do appreciate your help!
- HansVogelaarFeb 28, 2023MVP
I'm sorry, I wasn't notified of your reply.
But I'm afraid I don't even begin to understand what you want.
Could you attach a sample workbook with some explanations?
- PatDoolsFeb 28, 2023Brass Contributor
Hi Hans - please find attached a sample Workbook with a series of Worksheets with some data in it (gathered from a publicly-available dataset with no PII info., so no worries on privacy concerns).
I have 3 Worksheets: 'main diagnosis', 'secondary diagnosis', and 'procedures'. Each Worksheet has a couple columns of data, and the number of columns varies from sheet to sheet. What I want to do is to add the same four columns to the end of each dataset found on the individual Worksheets based on values from the Lookup Table found on the 'Lookup_Tbl' Worksheet in the attached Workbook.
Lookup Table month monthname year age range 3 Mar 2022 0-2 yrs The end result can be seen in the 'expanded' Worksheet tabs, where these 4 columns would be added to the last column of each dataset on each Worksheet, and then the values from the Lookup Table copied down to each row on that Worksheet. For example, the 'main diagnosis' Worksheet has the original columns provided from the data extract. Right next to it is a Worksheet called 'main diagnosis exp' which shows the original columns + the 4 columns from the Lookup Table (highlighted in yellow), populated with the data values from the Lookup Table copied down for each record. The 'expanded' Worksheet tabs are there for illustration purposes only - I'm looking simply to append the 4 columns onto the existing Worksheets, not create additional Worksheets. Again, the Lookup Table is my workaround the fact that the extract data does not contain these data elements and I thought this would be the easiest way to append these values to the existing dataset.
I hope this makes my request clearer - don't hesitate with any questions you may have. Thank you - I do appreciate it!
- HansVogelaarMar 01, 2023MVP
For example: select E2:E9 on the 'main diagnoses' sheet.
Enter the following formula and confirm it by pressing Ctrl+Enter:
=Lookup_Tbl!$A$4:$D$4