Hallo zusammen,
Chat GPT bringt mich leider nicht zum Ziel.
Ich habe eine excel Datei: c_lick_This.xlsm
mit dem Blatt: Auswertung
dort liegt eine Tabelle: OCC_Auswertung
mit ca 150-300 Zeilen und 79 Spalten
ich möchte die Werte in eine Acces Datenbank einfügen.
Acces: Datenbank_Gesamt.accdb
Tabelle: tbl_OCC_Voice
mit 79 Spalten
Name und Reihnfolge ist 1:1
Ziel des Makro:
suche den Bereich in der Tabelle in Excel wo Daten stehen, kopiere sie und füge sie in Acces ein.
Excel und Acces liegen nicht im selben Ordner so das ich einen Pfad eingeben muss um zu bestimmen wo sie sind.
Bitte um Hilfe 
 
ChatGpt hat nach 4h mir unteranderem folgenden Code geben:
	
	
	
	
	
Chat GPT bringt mich leider nicht zum Ziel.
Ich habe eine excel Datei: c_lick_This.xlsm
mit dem Blatt: Auswertung
dort liegt eine Tabelle: OCC_Auswertung
mit ca 150-300 Zeilen und 79 Spalten
ich möchte die Werte in eine Acces Datenbank einfügen.
Acces: Datenbank_Gesamt.accdb
Tabelle: tbl_OCC_Voice
mit 79 Spalten
Name und Reihnfolge ist 1:1
Ziel des Makro:
suche den Bereich in der Tabelle in Excel wo Daten stehen, kopiere sie und füge sie in Acces ein.
Excel und Acces liegen nicht im selben Ordner so das ich einen Pfad eingeben muss um zu bestimmen wo sie sind.
Bitte um Hilfe
 
 ChatGpt hat nach 4h mir unteranderem folgenden Code geben:
Code:
Sub ImportDataToAccess()
    ' Deklarieren der Variablen
    Dim excelFilePath As String
    Dim accessFilePath As String
    Dim excelSheetName As String
    Dim tableName As String
    Dim conn As Object
    Dim ws As Worksheet
    Dim r As Long
    Dim sql As String
    Dim lastRow As Long
    Dim fieldArray As Variant
    Dim i As Integer
    Dim value As String
    ' Pfade und Namen festlegen
    excelFilePath = "R:\###\CustomerServiceManagement\KC_CSM\WFM\fLink\c_lick_This.xlsm"
    accessFilePath = "R:\###\CustomerServiceManagement\KC_CSM\WFM\fLink\Datenbank_Gesamt.accdb"
    excelSheetName = "Auswertung"
    tableName = "tbl_OCC_Voice"
    ' Verbindung zu Access herstellen
    Set conn = CreateObject("ADODB.Connection")
    conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & accessFilePath
    ' Aktuelles Workbook und Worksheet setzen
    Set ws = ThisWorkbook.Sheets(excelSheetName)
    ' Bestimmen der letzten genutzten Zeile in der Excel-Tabelle
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    ' Felder definieren
    fieldArray = Array("tblDatum", "tblSkillID", "tblSkillName", _
                       "tblAnwahl0", "tblAnwahl1", "tblAnwahl2", "tblAnwahl3", "tblAnwahl4", _
                       "tblAnwahl5", "tblAnwahl6", "tblAnwahl7", "tblAnwahl8", "tblAnwahl9", _
                       "tblAnwahl10", "tblAnwahl11", "tblAnwahl12", "tblAnwahl13", "tblAnwahl14", _
                       "tblAnwahl15", "tblAnwahl16", "tblAnwahl17", "tblAnwahl18", "tblAnwahl19", _
                       "tblAnwahl20", "tblAnwahl21", "tblAnwahl22", "tblAnwahl23", "tblAnwahlGesamt", _
                       "tblAnnahme0", "tblAnnahme1", "tblAnnahme2", "tblAnnahme3", "tblAnnahme4", _
                       "tblAnnahme5", "tblAnnahme6", "tblAnnahme7", "tblAnnahme8", "tblAnnahme9", _
                       "tblAnnahme10", "tblAnnahme11", "tblAnnahme12", "tblAnnahme13", "tblAnnahme14", _
                       "tblAnnahme15", "tblAnnahme16", "tblAnnahme17", "tblAnnahme18", "tblAnnahme19", _
                       "tblAnnahme20", "tblAnnahme21", "tblAnnahme22", "tblAnnahme23", "tblAnnahmeGesamt", _
                       "tblAHT0", "tblAHT1", "tblAHT2", "tblAHT3", "tblAHT4", "tblAHT5", "tblAHT6", _
                       "tblAHT7", "tblAHT8", "tblAHT9", "tblAHT10", "tblAHT11", "tblAHT12", "tblAHT13", _
                       "tblAHT14", "tblAHT15", "tblAHT16", "tblAHT17", "tblAHT18", "tblAHT19", "tblAHT20", _
                       "tblAHT21", "tblAHT22", "tblAHT23", "tblAHTGesamt", "tblAATGesamt")
    ' SQL Insert-Befehl für jede Zeile in der Excel-Tabelle
    For r = 3 To lastRow
        sql = "INSERT INTO " & tableName & " ("
        ' Feldnamen hinzufügen
        For i = 0 To UBound(fieldArray)
            If i > 0 Then
                sql = sql & ", "
            End If
            sql = sql & "[" & fieldArray(i) & "]"
        Next i
        sql = sql & ") VALUES ("
        ' Werte hinzufügen
        For i = 0 To UBound(fieldArray)
            If i > 0 Then
                sql = sql & ", "
            End If
            value = ws.Cells(r, i + 1).Value
            ' Werte korrekt formatieren
            If IsNumeric(value) Then
                sql = sql & value
            ElseIf IsDate(value) Then
                sql = sql & "#" & Format(ws.Cells(r, i + 1).Value, "yyyy-mm-dd") & "#"
            Else
                sql = sql & "'" & Replace(value, "'", "''") & "'"
            End If
        Next i
        sql = sql & ")"
        ' Debug-Druck des SQL-Befehls
        Debug.Print sql
        ' Ausführen des SQL-Befehls
        On Error GoTo ErrorHandler
        conn.Execute sql
    Next r
    ' Verbindung schließen
    conn.Close
    Set conn = Nothing
    Exit Sub
ErrorHandler:
    MsgBox "Fehler beim Ausführen des SQL-Befehls: " & Err.Description
    conn.Close
    Set conn = Nothing
    Exit Sub
End Sub
 
 

 
 
 
		![[-]](https://www.clever-excel-forum.de/images/collapse.png)

