1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
Sub Btn_convertSQL()
Application.ScreenUpdating = False
Dim nomTable As String
Dim tempDate As String
Dim zones As String
zones = " "
i = 1
j = maxCol()
k = maxLigne()
a = k
MsgBox a, vbInformation
For col = 1 To j - 1
'If col < maxCol() Then
zones = zones & Feuil1.Cells(4, i).Value & ", "
i = i + 1
'End If
Next
zones = zones & Feuil1.Cells(4, j).Value
If Feuil1.Range("D2") <> "" Then
Feuil2.Cells.ClearContents 'on efface les requêtes précédentes
nomTable = Feuil1.Range("D2") 'D2 & E2 ont été fusionnés,
'mais c'est la 1ère qui contient le nom de la table
For lig = 5 To k
Feuil2.Cells(lig, 1) = "INSERT INTO " & nomTable & " (" & zones & ")" & " VALUES ("
For col = 1 To j
If col < j Then 'séparation des données entre virgules avant la dernière colonne
If IsNumeric(Feuil1.Cells(lig, col)) = False _
Or Feuil1.Cells(lig, col).NumberFormat = "@" Then
Feuil2.Cells(lig, col + 1) = "''" & Feuil1.Cells(lig, col) & "'" & "," 'affichage des chaînes, dates & heures entre ' '
Else
Feuil2.Cells(lig, col + 1) = "" & Feuil1.Cells(lig, col) & "" & "," 'sinon, affichage des nombres sans ' '
End If
If Feuil1.Cells(lig, col) = "" Then
Feuil2.Cells(lig, col + 1) = "'''" & "," 'si valeur vide, on affiche entre ' '
End If
Call InsertDateAvFin 'fonction qui s'exécute au cas où les dates sont au format YYYY-MM-DD,
'et lorsque les données sont séparées par des virgules
Else 'fermeture de la requête
If IsNumeric(Feuil1.Cells(lig, col)) = False _
Or Feuil1.Cells(lig, col).NumberFormat = "@" Then 'affichage des chaînes, dates & heures entre ' '
Feuil2.Cells(lig, col + 1) = "''" & Feuil1.Cells(lig, col) & "'" & ");"
Else
Feuil2.Cells(lig, col + 1) = "" & Feuil1.Cells(lig, col) & "" & ");" 'sinon, affichage des nombres sans ' ' (sauf 1ère col)
End If
If Feuil1.Cells(lig, col) = "" Then
Feuil2.Cells(lig, col + 1) = "'''" & ");" 'si valeur vide, on affiche entre ' '
End If
Call InsertDateFinTbl 'fonction qui s'exécute au cas où les dates sont au format YYYY-MM-DD,
'et lorsqu'on arrive à la dernière colonne
End If
a = a - 1
Next
Next
Feuil2.Select
MsgBox "Conversion terminée.", vbInformation
Else
MsgBox "Veuillez renseigner le nom de votre table !", vbCritical
End If
Application.ScreenUpdating = True
End Sub |
Partager