Combining rows from 2 sheets that matches using VBA/macro

%3CLINGO-SUB%20id%3D%22lingo-sub-3026445%22%20slang%3D%22en-US%22%3ECombining%20rows%20from%202%20sheets%20that%20matches%20using%20VBA%2Fmacro%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3026445%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everybody%20%26amp%3B%20thankyou%20in%20advance%20for%20considering%20my%20problem!%20I%20have%202%20sheets.%20Sheet%201%20(26%20columns%20by%2085896%20rows)%20and%20Sheet%202%20(15%20columns%20by%2043201%20rows).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBoth%20of%20the%20sheets%20contain%20different%20variables%20with%20different%20values%20however%20they%20both%20share%20similarities%20in%20the%20time%20column%20(column%20A).%20For%20example%2C%20Sheet%201%20records%20data%20almost%20every%201%20second%20of%20a%20day%20while%20Sheet%202%20records%20data%20every%202%20seconds%20of%20the%20day.%20I%20am%20looking%20to%20create%20a%20macro%20so%20that%20I%20am%20able%20to%20compare%20the%20time%20column%20in%20sheet%201%20to%20sheet%202%2C%20if%20it%20matches%2C%20I%20would%20like%20to%20copy%20the%20row%20of%20data%20from%20both%20sheets%20and%20put%20it%20into%20a%20new%20sheet%203.%20The%20result%20would%20be%20a%20fresh%20sheet%20(sheet%203)%2C%20that%20contains%20the%20data%20from%20both%20tools%2Fsheets%20when%20they%20are%20recorded%20at%20the%20same%20time.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3ECurrently%20I%20am%20really%20struggling%20with%20this%2C%20any%20help%20would%20be%20really%20really%20appreciated!%20Thanks%20so%20much%20for%20having%20a%20look%20at%20this%2C%20and%20if%20you%20can%20help%2C%20please%20let%20me%20know!%20Thanks%20again!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3026445%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3026803%22%20slang%3D%22en-US%22%3ERe%3A%20Combining%20rows%20from%202%20sheets%20that%20matches%20using%20VBA%2Fmacro%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3026803%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1233383%22%20target%3D%22_blank%22%3E%40Rory123908%3C%2FA%3E%26nbsp%3BI'd%20suggest%20you%20forget%20about%20VBA%20and%20look%20into%20PowerQuery%20in%20stead.%20Quite%20easy%2C%20I%20dare%20to%20say%2C%20though%20difficult%20to%20demonstrate%20with%20some%20real%20data%20to%20play%20with.%20Can%20you%20upload%20a%20file%20that%20represents%20the%20actual%20structure%20of%20your%20real%20sheets%2Ftables%3F%20Say%2C%20a%20hundred%20rows%20of%20data%20in%20each%20or%20so%2C%20%3CU%3Ewith%3C%2FU%3E%20matching%20time-stamps%20between%20the%20two.%20Remove%20any%20private%20and%2For%20confidential%20information%2C%20though.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3026845%22%20slang%3D%22en-US%22%3ERe%3A%20Combining%20rows%20from%202%20sheets%20that%20matches%20using%20VBA%2Fmacro%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3026845%22%20slang%3D%22en-US%22%3EHi%20there%2C%20many%20thanks%20for%20your%20response!%20I%20have%20uploaded%20a%20file%20representing%20my%20problem%2C%20100%20rows%20long.%20As%20you%20may%20see%2C%20each%20sheet%20represents%20the%20responses%20from%20a%20tool%20that%20records%20data.%20They%20were%20both%20recorded%2Ftaken%20at%20the%20same%20time%2C%20but%20at%20different%20frequencies%20and%20with%20the%20occasional%20missing%20data%20point.%20I%20am%20looking%20to%20combine%20the%20responses%20into%20a%20new%20sheet%20(sheet%203)%2C%20so%20that%20each%20row%20on%20sheet%203%20represents%20a%20point%20where%20all%20variables%20share%20recordings%20at%20the%20same%20time.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3026967%22%20slang%3D%22en-US%22%3ERe%3A%20Combining%20rows%20from%202%20sheets%20that%20matches%20using%20VBA%2Fmacro%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3026967%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1233383%22%20target%3D%22_blank%22%3E%40Rory123908%3C%2FA%3E%26nbsp%3BThanks!%20Attached%20file%20now%20includes%20a%20sheet%20with%20records%20from%20both%20tables%2C%20every%202nd%20second.%20Is%20that%20what%20you%20had%20in%20mind.%20The%20PQ%20steps%20are%20relatively%20simple%20but%20it%20needs%20getting%20used%20to%20if%20you%20have%20never%20worked%20with%20it%20before.%3C%2FP%3E%3CP%3EThis%20would%20be%20a%20good%20starting%20point%20to%20learn%20more%20about%20it.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fexceloffthegrid.com%2Fpower-query-introduction%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fexceloffthegrid.com%2Fpower-query-introduction%2F%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

Hello everybody & thankyou in advance for considering my problem! I have 2 sheets. Sheet 1 (26 columns by 85896 rows) and Sheet 2 (15 columns by 43201 rows).

 

Both of the sheets contain different variables with different values however they both share similarities in the time column (column A). For example, Sheet 1 records data almost every 1 second of a day while Sheet 2 records data every 2 seconds of the day. I am looking to create a macro so that I am able to compare the time column in sheet 1 to sheet 2, if it matches, I would like to copy the row of data from both sheets and put it into a new sheet 3. The result would be a fresh sheet (sheet 3), that contains the data from both tools/sheets when they are recorded at the same time. 


Currently I am really struggling with this, any help would be really really appreciated! Thanks so much for having a look at this, and if you can help, please let me know! Thanks again!

7 Replies

@Rory123908 I'd suggest you forget about VBA and look into PowerQuery in stead. Quite easy, I dare to say, though difficult to demonstrate with some real data to play with. Can you upload a file that represents the actual structure of your real sheets/tables? Say, a hundred rows of data in each or so, with matching time-stamps between the two. Remove any private and/or confidential information, though.

Hi there, many thanks for your response! I have uploaded a file representing my problem, 100 rows long. As you may see, each sheet represents the responses from a tool that records data. They were both recorded/taken at the same time, but at different frequencies and with the occasional missing data point. I am looking to combine the responses into a new sheet (sheet 3), so that each row on sheet 3 represents a point where all variables share recordings at the same time.

@Rory123908 Thanks! Attached file now includes a sheet with records from both tables, every 2nd second. Is that what you had in mind. The PQ steps are relatively simple but it needs getting used to if you have never worked with it before.

This would be a good starting point to learn more about it.

https://exceloffthegrid.com/power-query-introduction/ 

Hi again, many thanks for helping. There are 2 points that I should have made. Firstly, I am creating a system so that our engineers are able to process their data without software knowledge. The intention of a macro was that all they have to do is press a button and then they would get this combined sheet, ready to upload into power BI and use for visualisations of our data. The data I am presenting to you is from 1 job/operation, but there will be many different jobs/operations over time and the process will have to be repeated. I'm not too knowledgable on PQ, however if there is a requirement to manually implement it every time, this is not ideal. additionally, I should have explained the nature of the data, this was not obvious in my example 100 rows. There are missing & duplicated time points in the sheet 1. For example rows 309 and 310 both show the time as 00:05:09 and row 467 shows 00:07:47 while row 468 shows 00:07:49. The data in sheet 2 is consistently taken every 2 seconds however. Please refer to the initial problem, you will see there is not exactly double the amount of entriesin sheet 1 than there is sheet 2. Do you think it is possible to account for this while still being able to merge the sheets?

@Rory123908 If you insist on VBA, I can't help. But your description is definitely suited for Power Query. Once set-up correctly, it can be used over and over again. The user doesn't even have to see the individual files you currently have in Sheet1 and Sheet2. All they would need to learn is to right-click in the merged. But perhaps I underestimate the complexity of your issue.

Hi again, I've spent the day looking through how PQ could apply to my problem, and your correct, it does seem to fit very nicely here! I've managed to apply what you have done to my full dataset and can now visualise properly, so thank you very much for advising this. The process in which I did this was somewhat manual, is there anyway that I can automate this? for example, press a button and it would create the queries, merge them, show all columns etc, so that the users will not have to know PQ, but instead can just press the button or something similar to this to save the PQ process? Thanks again, learning new things about excel/VBA/PQ everyday, its great!

@Rory123908 I'm sure you could automate PQ in conjunction with VBA, but that would go beyond the scope of this "Help-forum", I think. And I'm not really comfortable with VBA.