Access-o-Mania

Access-Forum (Deutsch/German) => Access Programmierung => Thema gestartet von: vittorio0 am Mai 20, 2012, 09:07:59

Titel: mehrere Excel Dateien öffnen und schliessen mit VBA Access
Beitrag von: vittorio0 am Mai 20, 2012, 09:07:59
Liebes Forum

was fehlt hier? Die routine läuft für eine Datei durch und bei der 2. Datei kommt die Fehlermeldung 91, Objektvariable oder with block variablenicht festgelegt.

Danke für alle Antworten!

On Error GoTo error_routine

Dim xl As Excel.Application
Dim xlWrkBk As Excel.Workbook
Dim xlsht As Excel.Worksheet
Dim selImportFile As String
Dim prev As String
Dim z As Integer
DoCmd.SetWarnings True

fpath = "C:\far\importarchiv\"

z = 1

selImportFile = Dir(fpath & "*--import.xls")
Do While selImportFile <> ""


'open file chosen
Set xl = CreateObject("Excel.Application")
Set xlWrkBk = xl.Workbooks.Open(fpath & selImportFile)
Set xlsht = xlWrkBk.Worksheets(1)

    xl.Application.Visible = True
   
       
    xlsht.Range("L2:L1000").Select
    Selection.NumberFormat = "0.00"
   
    xlWrkBk.Close True
    xl.Quit
   
    Set xlWrkBk = Nothing
    Set xlsht = Nothing
    Set xl = Nothing
   
   
         
    Kill fpath & selImportFile
   
   
    selImportFile = Dir()
   
   
    Loop

exit_sub:
Exit Sub

error_routine:

MsgBox Err.Number & "   " & Err.Description
MsgBox "Formatfehler in der Datei europa.txt", vbInformation, "N:\far\import\europa.txt"
Resume exit_sub

End Sub
Titel: Re: mehrere Excel Dateien öffnen und schliessen mit VBA Access
Beitrag von: DF6GL am Mai 20, 2012, 12:33:21
Hallo,


das fehlt:


    xl.Selection.NumberFormat = "0.00"


Außerdem wäre es sinnvoll (performanter), Createobject vor die Do-Schleife zu setzen..