I have an Access database with a table to be used for importing a periodic report from Excel. The table has 5 fields.
The format of the Excel import file will always be the same; that being the field names start on row 10 and there are 4 data fields. However,
the number of data rows will vary from one file to the next. Sometimes a file may have 100 records, another time it may have 150, etc, etc.
I want to skip over the first 10 rows (because I only want to import data contained in the 4 data fields) and terminate the import when
the row with "TOTAL" is reached. Or, in other words exclude the row with "TOTAL" in it was well as the first 10 rows.
In the Excel file, the month the report applies to always appears in cell C4. I would like to copy that value into Field5 of the Access table
for every single record.
Because this will be a recurring function, ideally what I'd like to be able to do from Access is click on a query name or a button on a form
and have it open windows explorer where I can navigate to and select the file I want.
Upon doing so, it would run an import automatically that includes and excludes the data as described above,
I have attached a sample Access database with expected results in its table as well as a sample Excel import file.
Then, because you have no way of knowing where the Totals line is located, I use Excel Automation to open the Excel file and this is where the two methods differ.
1. In method 1, I determine where the Total line is located, then close the Excel File, and then use the TransferSpreadsheet method in Access to link the spreadsheet with specific cell ranges identified (eg A10:F100 ) in the Transferspreadsheet method. This way, Access only links the rows you need as a table which you can then use in an append query to get the data into your destination table.
2. In method 2, I simply delete the unwanted header rows from the spreadsheet, then locate the Totals row and clear the contents of that row and all subsequent rows, then save the workbook with a new name. I then use this new name to link the remaining rows to my database and import them into my Access table.
After importing the data via method 1 or 2, I will generally delete the linked table, and with Method #2, I also delete the file that I modified and saved.