Guten Morgen Zusammen,
ich brauche mal wieder eure Hilfe.
Ich lese mit VBA-Code Daten mittels import einer csv-Datei als query ein. Jetzt ist Excel leider etwas eigensinnig und löscht manchmal das query nicht am Ende des Makros und dann funktioniert der Code natürlich nicht beim nächsten Aufruf.
Ich würde gerne am Anfang prüfen, ob das entsprechende Query bereits vorhanden ist, wenn ja -> dann löschen.
Kann mir bitte jemand helfen, diese Abfrage am Anfang einzubauen?
'hier sollte die Abfrage stehen
ActiveWorkbook.Queries.Add Name:="ABCDE", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""C:\Users\xxx\ABCDE.csv""),[Delimiter="";"", Columns=27, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""Promoted Headers"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumn" & _
"Types(#""Promoted Headers"",{{""Ergebnis-ID"", Int64.Type}, {""Kurve verfügbar"", type text}, {""Sitzung"", type datetime}, {""Testparameter ID"", Int64.Type}, {""Name"", type text}, {""Testtyp"", type text}, {""Werkzeugtyp"", type text}, {""Verbundene Geräte"", type text}, {""Datum/Zeit"", type datetime}, {""Status"", type text}, {""Einheit"", type text}, {""Drehmo" & _
"ment"", Int64.Type}, {""Nominal Drehmoment"", Int64.Type}, {""Winkelschwellwert"", Int64.Type}, {""Min Drehmoment"", Int64.Type}, {""Max Drehmoment"", Int64.Type}, {""Winkel"", Int64.Type}, {""Spitzenwert Drehmoment"", Int64.Type}, {""Winkel bei Spitzenwert Drehmoment"", Int64.Type}, {""Drehmoment max. Winkel"", Int64.Type}, {""Max. Winkel"", Int64.Type}, {""Nominal" & _
" Winkel"", Int64.Type}, {""Min. Winkel"", Int64.Type}, {""Max. Winkel_1"", Int64.Type}, {""VIN"", type text}, {""Seq. Ergebnis"", Int64.Type}, {""Hinweis Kurve"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=ABCDE;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [ABCDE]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "ABCDE"
.Refresh BackgroundQuery:=False
End With
ActiveWorkbook.Queries("ABCDE").Delete
ActiveSheet.Name = "ABCDE"
Vielen Dank schon im Voraus für eure Hilfe.
Gruß Klaus