Hi I need some help writing VBA code to automate the importing of multiple Excel (csv format) files into Access. There are 2 ranges of data on one sheet in each file that I need to import. Both ranges of data have dynamic row counts. The Excel files , lets call them “SourceDataXXX.csv”all have the data on the same sheet, lets call it “InputData”. The first set of data always starts at cell A4 and is 7 columns of data (ending at cell G4). This set of data has a variable number of rows of data. There is always a blank row then a row of text to be ignored before the second set of data. This set of data is 19 columns wide and has a variable number of rows. The 2 sets of data will be put into 2 different tables. All data from the first set for all excel files (approx. 70-80 files) will be in one table and all data from the second set will be in a second table. From other questions on the site I can see how to do a single dynamic range, but I’m not sure how to jump to the second set of data.
Sub ImportDataFromRange()
'Access variables
Dim dbFile As Database
Dim tbl As TableDef, fld As Field
'Excel variables
Dim xlApp As Excel.Application
Dim xlFile As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
Dim r#, c#
Dim clVal As String 'string to hold cell's value, may need to modify this type.
Set dbFile = CurrentDb
'Use this to create a new table definition
' Set tbl = dbFile.CreateTableDef("Test")
'Use this if your table already exists:
Set tbl = dbFile.TableDefs("Test")
'Get the info from Excel:
Set xlApp = New Excel.Application
Set xlFile = xlApp.Workbooks.Open("C:\Users\david_zemens\desktop\Book1.xlsx")
Set xlSheet = xlFile.Sheets("Sheet1")
Set xlRange = xlSheet.Range("A1:B10")
For r = 1 To xlRange.Rows.Count
For c = 1 To xlRange.Columns.Count
'Add code to append new fields/records/etc to your table
Next c
Next r
In this example could I use a Do While loop to cycle through the rows and stop when I hit a Null (Note there are never Null rows of data, or even cells for that matter, in the dataset). Once I hit the Null I could add 2 to the current row number and begin again with a second For/Next loop. Also note I am importing this data and not linking it to allow me to combine the various individual Excel sheets. Thanks in advance for any support
