Question

I have created an excel worksheet in a such way that the user can insert some rows (max 100).

The value of some fields are chosen from a dropdown list.

I put the formula on all 100 rows.

The problem is that the data are not imported in access, if the user inserts less of 100 rows. In this case the data are imported only if I delete the formula for the dropdown list from the remaining rows.

To import excel data in access I use:

DoCmd.TransferSpreadsheet [Transfer Type], [Spreadsheet Type], [Table Name], [File Name], [Has Field Names], [Range]

How could I resolve this issue?

EDIT

After the Remou's answer, I tried with:

Private Sub import_Click()

Dim openDialog As FileDialog
Dim FileChosen As Integer

Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
On Error GoTo DoNothing
With openDialog
  .title = "Import"
  .AllowMultiSelect = False
  .Show
End With

filename = openDialog.SelectedItems.Item(1)

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim rng As Excel.Range

Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Open(nomeFile)

c = wb.Worksheets("Sheet1").Range("1:1").end(-4161).Address
r = wb.Worksheets("Sheet1").Range("A:A").end(-4121).Row

ImportRange = "Sheet1!" & "A1:" _
   & Replace(Mid(c, 1, InStrRev(c, "$")) & r, "$", "")

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
"ImportTable", filename, True, ImportRange

DoNothing:
    If Err.Number = cdlCANCEL Then
    End If

End Sub

but I've a debug error on Dim xl As Excel.Application

Was it helpful?

Solution

NOTE Having tested with Excel dropdowns and validation dropdowns, I find I do not have any problems importing data with validated cells or dropdowns in unused ranges, other than those rows are imported, even though all other cells are empty. The problem may be with data types and importing into an existing table. You may have to import to a scratch table and then append.

I suggest you use automation to get the used range and add that as the range to import.

This sample code uses late binding. You can add a reference to the Microsoft Excel x.x Object Library, so I have kept the various definitions, but late binding is best if you are using the code on more than one PC.

Dim xl As Object ''Excel.Application
Dim wb As Object ''Excel.Workbook
Dim rng As Object ''Excel.Range

Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Open("z:\docs\test.xlsx")

'Choosing a column that is fully filled on sheet5
'xldown=-4121,xltoright=-4161
c = wb.Worksheets("Sheet5").Range("1:1").end(-4161).Address
r = wb.Worksheets("Sheet5").Range("A:A").end(-4121).Row

ImportRange = "Sheet5!" & "A1:" _
    & Replace(Mid(c, 1, InStrRev(c, "$")) & r, "$", "")

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, _
    "ImportSheet2", "Z:\Docs\csharp.xlsm", True, ImportRange
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top