Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88054

Import excel rows into MS-Access without header, VBA, TransferSpreadsheet method

$
0
0

(Using MS Office Professional Plus 2016)

I adapted this sub procedure to import MS-Excel files/their first worksheet (called "Table") into an MS-Access database. The code navigates to the indicated folder and imports all .xls files in that folder. All .xls files have the same formatting. I'm using the DoCmd.TransferSpreadsheet and a Do While.

But, the relevant data in the "Table" worksheets in the individual .xls files starts at line 29, the field names are in line 28.

My question is: Is there a way to only import the field names in line 28 and the data from line 29 to the last non-empty row - a way to include this in the Do While? Maybe with the Range option in the TransferSpreadsheet command, but I don't know how to express in the range "line x to last non-empty row".

Option Compare Database

Sub importfiles()

Dim blnHasFieldNames As Boolean
Dim strWorksheet As String, strTable As String
Dim strPath As String, strPathFile As String

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False

' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "E:\importfiles\"' Replace worksheetname with the real name of the worksheet that is to be
' imported from each file
strWorksheet = "Table"' Import the data from each workbook file in the folder
strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
      strPathFile = strPath & strFile
      strTable = "tbl_"& Left(strFile, InStrRev(strFile, ".xls") - 1)

      DoCmd.TransferSpreadsheet acImport, _
            acSpreadsheetTypeExcel9, strTable, strPathFile, _
            blnHasFieldNames, strWorksheet & "$"' Uncomment out the next code step if you want to delete the
      ' EXCEL file after it's been imported
      ' Kill strPathFile

      strFile = Dir()
Loop
MsgBox ("The sub was run.")

End Sub


Public Function runImport()

Call importfiles

End Function
(I slightly adapted this code from http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpWktFilesSepTbls)


EDIT:
[Type mismatch, see Max's answer][1]
 [Screenshot of excel file][2]

 [access table][3]

 [Compile error(see Max's answer)][4]


[for max][5]
[for max2][6]
[for max3][7]


  [1]: https://i.stack.imgur.com/PZFnl.png
  [2]: https://i.stack.imgur.com/IjvKc.png
  [3]: https://i.stack.imgur.com/jzOXu.png
  [4]: https://i.stack.imgur.com/uHUTK.png
  [5]: https://i.stack.imgur.com/slAeG.png
  [6]: https://i.stack.imgur.com/g6fNr.png
  [7]: https://i.stack.imgur.com/gFNqA.png


Viewing all articles
Browse latest Browse all 88054

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>