+ Reply to Thread
Results 1 to 15 of 15

appending data to table from a dynamic (database) table

  1. #1
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    appending data to table from a dynamic (database) table

    I've attached a workbook which shows two tables. The "Database Table" is a query table, always showing 100 rows. It is connected to a live mysql database.

    It gets updated whenever the query is refreshed.

    The Master Table is partially static. Any data it has collected prior to the first row of the Database Table needs to remain intact.

    Any help with VBA code to make this happen?

    Thanks.
    Attached Files Attached Files

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: appending data to table from a dynamic (database) table

    I have a clarifying question: if a date exists in the master table and that date also exists in the query table, you want to replace the information for that date in the master table with the information from the query table and append any new dates from the query table. Did I understand the requirement correctly?
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: appending data to table from a dynamic (database) table

    One more question, is it OK if I add a helper column to the permanent table?

  4. #4
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    Thanks dflak, honestly I don't think it is necessary to overwrite existing data in the Master Table. What I want to be sure of is that if I don't update the workbook for 3 weeks, then update it, the Master Table will "know" not just to look back a day... but as far back as necessary to complete itself. I hope I'm making sense.

  5. #5
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    sure.. helper columns are fine

  6. #6
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: appending data to table from a dynamic (database) table

    Are you looking to pass some kind of parameter to the query based on the latest date in the master table, or are you merely interested in concatenating the two tables regardless of when the query is run?

  7. #7
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    Hmm... the ultimate goal is for the Master Table to contain every row of data that the Database Table "produced", in order of dates, ascending, without duplication. And I'd like it to do this regardless of when the database table is updated.

    I have a manual constraint in that I can't "lapse" for more than 100 "days" of data, otherwise the Master Table will never contain all the possible rows once available to me. I put "days' in quotes here because you will see that the database data skips certain days (i.e. holidays and weekends).

    Let me know if this makes sense.

  8. #8
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    And it is fine to use helper cells on either worksheet

  9. #9
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    PS the database query itself returns "the bottom 100 rows" of whatever is in the mysql database.

  10. #10
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: appending data to table from a dynamic (database) table

    OK, I think I got it. The query is getting the last 100 days of data. If you wait 10 days then only 90 new days are added and the oldest 10 are ignored.

  11. #11
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    Correct... except we need the last 100 rows of data, not really the last 100 days, since the database is not going to have any data at all for weekends and holidays.

  12. #12
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    I hope this is enough. I have to run out for a while and will check back. I really appreciate your help!

  13. #13
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: appending data to table from a dynamic (database) table

    I based this on a similar report I have.

    First a couple setup items:

    I converted the master table into an actual Excel Table. The main reason is because tables copy down formulas automatically and I need the formula in the helper column copied down as I add rows. The helper column looks at the Database Table and is true if the date in column A is found in that table.

    I made a static named range Date_List ='Database Table'!$A$4:$A$103. If the database table doesn't actually start in row 4, change the value to reflect the new range.

    I also have a count of the number true in cell J3. The code tries to delete rows, but will have an error if there are no rows to delete. I check this cell to see if there are rows to be deleted.

    How the code works: When the query is run, the helper column shows where there are duplicate dates in the Master Table. The code then deletes these rows and copies in the whole database table.

    Below is the code. Take a look at the constants DB1Range and DB2Range to make sure they line up with the actual database table.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    Thanks dflack. I appreciate this. I'll take a look later today and get back. I forgot that I have another simple macro in the workbook already, which simply does a "refresh all" for all data connections.

    Please Login or Register  to view this content.
    Think it would hurt to just add ActiveWorkbook.RefreshAll somewhere at the beginning of your macro? It could take 10 seconds to make the db connections and populate the database so I don't know if also a "wait" function in the db is also required.

  15. #15
    Forum Contributor
    Join Date
    12-03-2008
    Location
    Key West, FL
    MS-Off Ver
    365 Apps for Enterprise
    Posts
    665

    Re: appending data to table from a dynamic (database) table

    Thanks dflak, it works great. I appreciate your help. I'm going to try to insert the ActiveWorkbook.RefreshAll code (even though I don't know what I'm doing!) and see if it all works together. But for now I'll mark the thread solved. Thanks so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] appending data to table by referencing static cells
    By jrtaylor in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-08-2019, 10:36 AM
  2. Make conditionally sized dynamic table (data table)
    By myersac in forum Excel General
    Replies: 0
    Last Post: 09-17-2014, 03:23 PM
  3. Replies: 2
    Last Post: 08-27-2014, 03:13 PM
  4. Replies: 0
    Last Post: 02-05-2014, 02:49 PM
  5. Dynamic table formula for sorting data to another table dynamic
    By 650dozer in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-21-2012, 07:22 PM
  6. Finding data in a table and appending it to a cell
    By Jimbo42 in forum Excel General
    Replies: 7
    Last Post: 06-02-2011, 08:10 PM
  7. [SOLVED] Automatically appending two tables of data into a third table
    By robertguy in forum Excel General
    Replies: 7
    Last Post: 02-24-2010, 12:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1