Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Copy data from table B and C to table A

0

Table A, column A is the date column from 1-31, table B, C has 2 rows of data in the same day, how when transferring data from B, C to table A will automatically add rows and bring the data together? that date is empty.

I attached the file, thank you

Answer
Discuss

Answers

0

Hi HGVIET and welcome to the Forum

Something like your required result can be achived using a combination of Excel's Power Query and a small event macro.

In the attached file, I've created a table (called "Days") with calendar days 1 to 31. I've then loaded that plus your existing named tables Table1 and Table3 as queries into Excel's PQE. Once the "blanks" under the day columns of the latter were removed (using a right click), I combined the queries into a new query (called Append1) , set it to Sort Ascending and loaded it to cell P3- see the result under the heading "Loaded Power Query".

That's okay but if you change a value in the other tables, it doesn't update automatically unless... 

To get the table to update automatically, the event macro below is used (in the attached file- you'll need to enable macro for it to work). It sees if any of the body cell of the three named tables has been updated and, if so, it refreshs the query (so the table instantly reflects the change).

Private Sub Worksheet_Change(ByVal Target As Range)

'do nothing unless tables are changed
If Intersect(Target, Union(Range("Days"), Range("Table1"), Range("Table3"))) Is Nothing Then Exit Sub
' if tables changed, refresh the Power Query
ActiveWorkbook.Connections("Query - Append1").Refresh

End Sub

A nice feature is that you can add data to Table1 and Table 3 out of sequence (e.g. below existing data row) and the query will sort it anway!

It's not perfect since if there's a day with one or more data rows, you also get a blank row from the Days table (given time  I think that can be filtered out in PQE).

Hope this works for you or at least gives you a good start. If so, kindly mark this Answer as Selected.

Happy New Year!

Discuss

Discussion

So did you try that?
John_Ru (rep: 6142) Jan 4, '23 at 9:15 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login