Hi,
I am very new to VBA and am trying to set up an import from excel into access. Both are the 2010 version. I pulled the code I am using from a previous thread posted by a user trying to do what I want to do. The problem I am having is that every time I try to run the code I get a message saying "Compile Error: Invalid Outside Procedure" and the file path is highlighted. Any advice on what I am doing wrong would be greatly appreciated. Below is the code I am using. Thanks.
I am very new to VBA and am trying to set up an import from excel into access. Both are the 2010 version. I pulled the code I am using from a previous thread posted by a user trying to do what I want to do. The problem I am having is that every time I try to run the code I get a message saying "Compile Error: Invalid Outside Procedure" and the file path is highlighted. Any advice on what I am doing wrong would be greatly appreciated. Below is the code I am using. Thanks.
Code:
Option Compare Database
Dim excelapp As New Excel.Application
Dim excelbook As New Excel.Workbook
Dim excelsheet As New Excel.Worksheet
Dim intNoOfSheets As Integer, intCounter As Integer
Dim strFilePath As String, strLastDataColumn As String
Dim strLastDataRow As String, strLastDataCell As String
strFilePath = "S:\share\Innoware Quality System\Data\Ridigity\2011 Rigidity\2011 Performa Rigidity.xlsx"
Set excelbook = excelapp.Workbooks.Open(strFilePath)
intNoOfSheets = excelbook.Worksheets.Count
For intCounter = 1 To intNoOfSheets
excelbook.Worksheets(intCounter).Activate
'The next 3 lines will obtain the last data cell reference for each Worksheet
strLastDataColumn = Chr(Selection.SpecialCells(xlLastCell).Column + 64)
strLastDataRow = Selection.SpecialCells(xlLastCell).Row
strLastDataCell = strLastDataColumn & strLastDataRow 'e.g. J123
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel14, "RigidityResultsExcelImport", strFilePath, False, _
excelbook.Worksheets(intCounter).Name & "!A1:" & strLastDataCell
Next
excelbook.Close
excelapp.Quit
Set excelapp = Nothing
Comment