Import Dynamic and Static ranges from Excel in to MS-Access that do not start at cell A1

前端 未结 2 803
故里飘歌
故里飘歌 2020-12-11 06:27

How might I link a data range from an Excel spreadsheet so that the data in that range appears as a useable table in Access?

Linking to an Excel sheet that has data

2条回答
  •  独厮守ぢ
    2020-12-11 06:51

    I'm not an Access guy but let me give you some pointers that should be able to help you.

    1 of the sheets/tabs, let's call it "Dynamic", has a data range I want available as a table in Access, but its column headings start at row 14, going across to column EL. What I'd like Access to do is pick up this data range as a table. Furthermore, "ExcelFile1.xls" will also get updated periodically, i.e. a new version of the "ExcelFile.xls" file will become available, but with more data below row 14's column headings, so ideally I would like Access to pick up the new data in this range whenever I overwrite the previous version of "ExcelFile1.xls".

    OK, so on this worksheet you will want to establish a dynamic Named Range. Basically you create a formula that determines/resizes the range whenever new data is added to it.

    For an overview of how to create dynamic named ranges, start here:

    http://support.microsoft.com/kb/830287

    With VBA, then, worst-case scenario is that you can access this named range, read its contents in to an array variable or simply iterate over the rows/columns, and then write those contents out to your Access table. But, not being an Access programmer, there may be some slightly more efficient ways of doing this.

    This has a sheet/tab, let's call it "Static", that similarly has a data range I want as a table in Access, and again, there will be newer versions of "ExcelFile2.xls" that will overwrite previous versions that I would ideally like Access to pick up on. This range is A14:O19 and will always be this range (i.e. static range).

    You can likewise create another Named Range that defines as =$A$14:$O$19 which will be a static named range. Then, you can treat it just like the above.

    EDIT Here is an example of getting the Excel data and then iterate over the rows & columns, you just need to add code to add the fields/records/etc to the table in Access.

    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
    
    xlApp.Quit
    Set xlApp = Nothing
    
    
    End Sub
    

    Hope that's enough to get you started!

提交回复
热议问题