Hello everyone,
I have a problem related to exporting an Excel spreadsheet to an Access form.
I am using a module:
Code:
Option Compare Database
Public Function buscaArchivo() As String
Dim fDialog As Office.FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = False
.ButtonName = "Seleccionar"
.Title = "Seleccionar el archivo"
.InitialFileName = Application.CurrentProject.Path
.InitialView = msoFileDialogViewDetails
.Filters.Clear
.Filters.Add "All Files", "*.*"
If .Show = True Then
buscaArchivo = .SelectedItems(1)
Else
MsgBox "Ha pulsado el botón <Cancelar>."
End If
End With
End Function
and a button which is located in my form whose function is to look up an excel file I previously saved and transfer all the excel data which is at the second sheet.
Code:
Private Sub cmdNavegar_Click()
Dim vArchivo As String
vArchivo = buscaArchivo()
If IsNull(vArchivo) Or vArchivo = "" Then
Exit Sub
Else
Me.Archivo.Value = vArchivo
End If
Dim xlApp As Excel.Application
Dim xlLibro As Excel.Workbook
Dim xlHoja As Excel.Worksheet
Set xlApp = Excel.Application
Set xlLibro = xlApp.Workbooks.Open(vArchivo)
Set xlHoja = xlLibro.Sheets(2)
'xlHoja.Range("A2").Select
'Me.codigoRFQ = xlHoja.Range("A2").Value
xlHoja.Range("B2").Select
Me.solicitante = xlHoja.Range("B2").Value
.
.
.
xlHoja.Range("AJ2").Select
Me.comentarios = xlHoja.Range("AJ2").Value
xlLibro.Close
Set xlHoja = Nothing
Set xlLibro = Nothing
Set xlApp = Nothing
End Sub
There are some times that the export action works as intended. But quite often I receive an error called: "run-time error 1004"
When I check the error it shows me that the Range is wrong but it isn´t.
If i go back to my excel file and click on a different cell and then save it. Go then to my Access application I try again, usually it is solved.
I don´t why this happen. Is there any solution?
Thank you in advance.