Domanda

Ho bisogno di inserire i dati da un foglio di Excel in una tabella di Teradata. e ho bisogno di questo per essere fatto utilizzando una macro.

Ho dati in un foglio excel come

COL1  COL2 COL3 COL4
1      2    3     4
2      5    8     10
.
.
so su

e ho bisogno di mantenere un pulsante nel foglio Excel e assegnare una macro a quello pulsante in modo che quando scatto il tasto le righe nel foglio excel devono essere inseriti in una tabella del database.

Il requisito è che io manderò il foglio di Excel vuoto alla persona, egli compilare il foglio con i dati e si fa clic sul pulsante in excel ed i dati devono essere inseriti nella tabella del database. Io preferirei fare questo utilizzando una macro ..

Grazie a tutti.

È stato utile?

Soluzione

Dai un'occhiata alla questo link per l'utilizzo di codice vb / VBA (per il marco) per spostare i dati da Excel a SQL server.

Altri suggerimenti

Ho funzione che trasformerà la tabella di Excel in multipla inserire comandi creato.

Copia questo nel modulo e poi nella formula, impostato come primo parametro i valori delle celle che deve essere inserito, il secondo intervallo dovrebbero essere i nomi delle colonne (premere F4 per impostare questo come costante), e terzo (optional) il nome della tabella. Se il nome della tabella non è specificato, allora il nome del foglio verrà utilizzato come predefinito.

Nel tuo caso questo è come il foglio di calcolo dovrebbe essere simile:

+---+------+------+------+------+-----------------------------------------+
|   | A    | B    | C    | D    | E                                       |
+---+------+------+------+------+-----------------------------------------+
| 1 | COL1 | COL2 | COL3 | COL4 |                                         |
+---+------+------+------+------+-----------------------------------------+
| 2 | 1    | 2    | 3    | 4    | =Insert2DB(A2:D2,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------+
| 3 | 2    | 5    | 8    | 10   | =Insert2DB(A3:D3,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------+

Questo genererà per voi queste due domande:

INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (1,2,3,4)
INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (2,5,8,10)

Ecco la funzione (funziona bene con Microsoft SQL (T):

    Function Insert2DB(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

      Dim rangeCell As Range
      Dim InsertValues As String
      Dim CellValue As String
      Dim C As Range

        Dim AllColls As String
        Dim SingleCell As Range
        Dim TableColls As String

    InsertValues = ""

    'Start Loop
    For Each rangeCell In InputRange.Cells

    'Recognize data type
    Set C = rangeCell
        If IsEmpty(C) Then
                'DataType is NULL then NULL
                CellValue = "NULL"
            ElseIf Application.IsText(C) Then
                'DataType is VARCHAR or CHAR
                CellValue = "'" & Trim(rangeCell.Value) & "'"
            ElseIf Application.IsLogical(C) Then
                'DataType is bit eg. TRUE / FALSE
                    If rangeCell.Value = True Then
                        CellValue = "1"
                    ElseIf rangeCell.Value = False Then
                        CellValue = "0"
                    End If
            ElseIf Application.IsErr(C) Then
                'If there is an ERROR in cell, the statment will return 0
                CellValue = "NULL"
            ElseIf IsDate(C) Then
                'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
                CellValue = "'" & VBA.Format(rangeCell.Value, "yyyymmdd hh:mm:ss") & "'"
            ElseIf InStr(1, C.Text, ":") <> 0 Then
                'DataType is TIME
                CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
            ElseIf IsNumeric(C) Then
                'DataType is number
                CellValue = rangeCell.Value
        End If

    If (Len(InsertValues) > 0) Then
        InsertValues = InsertValues + "," + CellValue
    Else
        InsertValues = CellValue
    End If

    Next rangeCell
    'END Loop

    If IsMissing(ColumnsNames) Then
        TableColls = ""
        Else

        For Each SingleCell In ColumnsNames.Cells
            If Len(AllColls) > 0 Then
                     AllColls = AllColls + "," + "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
            Else
                    AllColls = "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
            End If
        Next SingleCell
        TableColls = " (" & AllColls & ")"
    End If


    'If TableName is not set, then take the name of a sheet
    If IsMissing(TableName) = True Then
        TableName = ActiveSheet.Name
    Else
    TableName = TableName
    End If

    'Set the return value
        Insert2DB = "INSERT INTO " & TableName & TableColls & " VALUES (" & InsertValues & ")"

    End Function

Se si dispone di un bel dati molto da inserire, si potrebbe non essere necessario utilizzare INSERT INTO in ogni comando, poi basta usare la funzione Insert2DB in prima fila (e ogni 500) e per l'utilizzo resto solo Insert2DBValues:

+---+------+------+------+------+-----------------------------------------------+
|   | A    | B    | C    | D    | E                                             |
+---+------+------+------+------+-----------------------------------------------+
| 1 | COL1 | COL2 | COL3 | COL4 |                                               |
+---+------+------+------+------+-----------------------------------------------+
| 2 | 1    | 2    | 3    | 4    | =Insert2DB(B3:E3,$B$2:$E$2,"TableName")       |
+---+------+------+------+------+-----------------------------------------------+
| 3 | 2    | 5    | 8    | 10   | =Insert2DBValues(A3:D3,$A$1:$D$1,"TableName") |
+---+------+------+------+------+-----------------------------------------------+

Questo vi darà i seguenti comandi:

INSERT INTO TableName ([COL1],[COL2],[COL3],[COL4]) VALUES (1,2,3,4)
,(2,5,8,10)


Function Insert2DBValues(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

  Dim rangeCell As Range
  Dim InsertValues As String
  Dim CellValue As String
  Dim C As Range

    Dim AllColls As String
    Dim SingleCell As Range
    Dim TableColls As String

InsertValues = ""

'Start Loop
For Each rangeCell In InputRange.Cells

'Recognize data type
Set C = rangeCell
    If IsEmpty(C) Then
            'DataType is NULL then NULL
            CellValue = "NULL"
        ElseIf Application.IsText(C) Then
            'DataType is VARCHAR or CHAR
            CellValue = "'" & Trim(rangeCell.Value) & "'"
        ElseIf Application.IsLogical(C) Then
            'DataType is bit eg. TRUE / FALSE
                If rangeCell.Value = True Then
                    CellValue = "1"
                ElseIf rangeCell.Value = False Then
                    CellValue = "0"
                End If
        ElseIf Application.IsErr(C) Then
            'If there is an ERROR in cell, the statment will return 0
            CellValue = "NULL"
        ElseIf IsDate(C) Then
            'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
            CellValue = "'" & VBA.Format(rangeCell.Value, "yyyy-mm-dd hh:mm:ss") & "'"
        ElseIf InStr(1, C.Text, ":") <> 0 Then
            'DataType is TIME
            CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
        ElseIf IsNumeric(C) Then
            'DataType is number
            CellValue = rangeCell.Value
    End If

If (Len(InsertValues) > 0) Then
    InsertValues = InsertValues + "," + CellValue
Else
    InsertValues = CellValue
End If

Next rangeCell
'END Loop

If IsMissing(ColumnsNames) Then
    TableColls = ""
    Else

    For Each SingleCell In ColumnsNames.Cells
        If Len(AllColls) > 0 Then
                 AllColls = AllColls + "," + "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
        Else
                AllColls = "[" + Trim(Replace(SingleCell.Value, Chr(160), "")) + "]"
        End If
    Next SingleCell
    TableColls = " (" & AllColls & ")"
End If


'If TableName is not set, then take the name of a sheet
If IsMissing(TableName) = True Then
    TableName = ActiveSheet.Name
Else
TableName = TableName
End If

'Set the return value
    Insert2DBValues = ",(" & InsertValues & ")"

End Function

E, infine, se si sta utilizzando MySQL, v'è diverso fuga di stringhe, quindi in tal caso utilizzare Insert2DBMySQL:

    Function Insert2DBMySQL(InputRange As Range, Optional ColumnsNames As Variant, Optional TableName As Variant)

      Dim rangeCell As Range
      Dim InsertValues As String
      Dim CellValue As String
      Dim C As Range

        Dim AllColls As String
        Dim SingleCell As Range
        Dim TableColls As String

    InsertValues = ""

    'Start Loop
    For Each rangeCell In InputRange.Cells

    'Recognize data type
    Set C = rangeCell
        If IsEmpty(C) Then
                'DataType is NULL then NULL
                CellValue = "NULL"
            ElseIf Application.IsText(C) Then
                'DataType is VARCHAR or CHAR
                CellValue = "'" & Trim(rangeCell.Value) & "'"
            ElseIf Application.IsLogical(C) Then
                'DataType is bit eg. TRUE / FALSE
                    If rangeCell.Value = True Then
                        CellValue = "1"
                    ElseIf rangeCell.Value = False Then
                        CellValue = "0"
                    End If
            ElseIf Application.IsErr(C) Then
                'If there is an ERROR in cell, the statment will return 0
                CellValue = "NULL"
            ElseIf IsDate(C) Then
                'DataType is DATE or DATETIME, in case it is DATE specifying HH:mm:ss would do no harm
                CellValue = "'" & VBA.Format(rangeCell.Value, "yyyy-mm-dd hh:mm:ss") & "'"
            ElseIf InStr(1, C.Text, ":") <> 0 Then
                'DataType is TIME
                CellValue = "'" & VBA.Format(rangeCell.Value, "hh:mm:ss") & "'"
            ElseIf IsNumeric(C) Then
                'DataType is number
                CellValue = rangeCell.Value
        End If

    If (Len(InsertValues) > 0) Then
        InsertValues = InsertValues + "," + CellValue
    Else
        InsertValues = CellValue
    End If

    Next rangeCell
    'END Loop

    If IsMissing(ColumnsNames) Then
        TableColls = ""
        Else

        For Each SingleCell In ColumnsNames.Cells
            If Len(AllColls) > 0 Then
                     AllColls = AllColls + "," + "" + Trim(Replace(SingleCell.Value, Chr(160), "")) + ""
            Else
                    AllColls = "" + Trim(Replace(SingleCell.Value, Chr(160), "")) + ""
            End If
        Next SingleCell
        TableColls = " (" & AllColls & ")"
    End If


    'If TableName is not set, then take the name of a sheet
    If IsMissing(TableName) = True Then
        TableName = ActiveSheet.Name
    Else
    TableName = TableName
    End If

    'Set the return value
        Insert2DBMySQL = "INSERT INTO " & TableName & TableColls & " VALUES (" & InsertValues & ");"

    End Function
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top