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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
Dim DerniereCelluleRemplie As Integer
Dim i As Integer
Dim nom_station As String
Dim numero_station As String
Dim rs As ADODB.Recordset
Dim oConn As ADODB.Connection
'On retire les caractère "'" par "\'"
Function esc(txt As String)
esc = Trim(Replace(txt, "'", "\'"))
End Function
'Connection à MYSQL
Sub ConnectDB()
Set oConn = New ADODB.Connection
oConn.Open "DRIVER={MySQL ODBC 5.3 ANSI Driver};" & _
"SERVER=localhost;" & _
"DATABASE=xxx;" & _
"USER=xxx;" & _
"PASSWORD=xxx;" & _
"Option=3"
oConn.MaintainConnection = False
End Sub
Sub lancement_macro_synop()
Sheets("cv").Calculate
heure = Sheets("cv").Range("F4")
Application.OnTime TimeValue(heure), "a01_insert_data_station_all"
End Sub
Sub interval_synop()
interval = Sheets("cv").Range("F5")
Application.OnTime Now + TimeValue(interval), "a01_insert_data_station_all"
End Sub
Sub a01_insert_data_station_all()
'Désactivation de la mise à jour visuelle
Application.ScreenUpdating = False
DerniereCelluleRemplie = Sheets("variable").Range("A1").End(xlDown).Row
For i = 2 To DerniereCelluleRemplie
nom_station = Sheets("variable").Range("A" & i)
numero_station = Sheets("variable").Range("B" & i)
date_du_jour = Format(Now, "dd")
mois_du_jour = Format(Now, "mm")
heure_now = Format(Now, "hh")
annee = Format(Now, "yyyy")
Sheets("d_a_st_ex").Select
ActiveSheet.Range("A1:W300").Select
Selection.ClearContents
'Télécharger les données de la station
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.ogimet.com/cgi-bin/gsynres?ind=" & numero_station & "&lang=en&decoded=yes&ndays=10&ano=" & annee & "&mes=" & mois_du_jour & "&day=" & date_du_jour & "&hora=" & heure_now & "" _
, Destination:=Range("$A$1"))
.Name = _
"gsynres?ind=" & numero_station & "&lang=en&decoded=yes&ndays=10&ano=" & annee & "&mes=" & mois_du_jour & "&day==" & date_du_jour & "&hora=" & heure_now & ""
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "4"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
If Sheets("d_a_st_ex").Range("AA1").Value <> Sheets("d_a_st_ex").Range("AB1").Value Then
'Suppression de la connexion
ActiveWorkbook.Connections("Connexion").Delete
GoTo Line1
Else
'Suppression de la connexion
ActiveWorkbook.Connections("Connexion").Delete
'Effacer les lignes vides
Sheets("d_a_st_ex").Select
x = Range("A250").End(xlUp).Row
Cells.Range("A1:A" & x). _
SpecialCells(xlCellTypeBlanks).EntireRow.Delete
'Renseigner les cellules de d_b par celle de d_a
Sheets("d_b_st_ex").Range("B1:Z250").Value = Sheets("d_a_st_ex").Range("B1:Z250").Value
End If
Sheets("d_t_g_st_ex").Range("CG7:CW7").Value = Sheets("variable").Range(("G" & i & ":W" & i)).Value
'Insérer les données traités dans la base de données
Set rs = New ADODB.Recordset
ConnectDB
With d_t_g_st_ex
For rowCursor_a = 2 To 39
strSQL = "INSERT IGNORE INTO archive_station_" & nom_station & " (" & _
"date_heure_loc_ms_archive_station_" & nom_station & ", " & _
"date_heure_loc_txt_archive_station_" & nom_station & ", " & _
"vitesse_vent_archive_station_" & nom_station & ", " & _
"vitesse_rafale_archive_station_" & nom_station & ", " & _
"press_atmo_archive_station_" & nom_station & ", " & _
"temp_air_archive_station_" & nom_station & ", " & _
"precipitation_archive_station_" & nom_station & ", " & _
"duree_vent_faible_archive_station_" & nom_station & ", " & _
"duree_vent_offshore_archive_station_" & nom_station & ", " & _
"dir_vent_archive_station_" & nom_station & ") " & _
"VALUES ('" & esc(.Cells(rowCursor_a, 1)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 2)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 3)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 4)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 5)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 6)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 7)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 8)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 9)) & "', " & _
"'" & esc(.Cells(rowCursor_a, 10)) & "')"
rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
Next
End With
'Insertion données vent graphique station
Set rs = New ADODB.Recordset
ConnectDB
With d_t_g_st_ex
For rowCursor_b = 2 To 11
strSQL = "INSERT IGNORE INTO dir_vent_graph_station_" & nom_station & " (" & _
"date_heure_loc_ms_dir_vent_graph_station_" & nom_station & "," & _
"date_heure_loc_txt_dir_vent_graph_station_" & nom_station & "," & _
"NO_16_dir_vent_graph_station_" & nom_station & "," & _
"NNO_15_dir_vent_graph_station_" & nom_station & "," & _
"N_14_dir_vent_graph_station_" & nom_station & "," & _
"NNE_13_dir_vent_graph_station_" & nom_station & "," & _
"NE_12_dir_vent_graph_station_" & nom_station & "," & _
"ENE_11_dir_vent_graph_station_" & nom_station & "," & _
"E_10_dir_vent_graph_station_" & nom_station & "," & _
"ESE_9_dir_vent_graph_station_" & nom_station & ","
strSQL = strSQL & _
"SE_8_dir_vent_graph_station_" & nom_station & "," & _
"SSE_7_dir_vent_graph_station_" & nom_station & "," & _
"S_6_dir_vent_graph_station_" & nom_station & "," & _
"SSO_5_dir_vent_graph_station_" & nom_station & "," & _
"SO_4_dir_vent_graph_station_" & nom_station & "," & _
"OSO_3_dir_vent_graph_station_" & nom_station & "," & _
"O_2_dir_vent_graph_station_" & nom_station & "," & _
"ONO_1_dir_vent_graph_station_" & nom_station & ") " & _
"VALUES ('" & esc(.Cells(rowCursor_b, 13)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 14)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 15)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 16)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 17)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 18)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 19)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 20)) & "', "
strSQL = strSQL & _
"'" & esc(.Cells(rowCursor_b, 21)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 22)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 23)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 24)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 25)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 26)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 27)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 28)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 29)) & "', " & _
"'" & esc(.Cells(rowCursor_b, 30)) & "')"
rs.Open strSQL, oConn, adOpenDynamic, adLockOptimistic
Next
End With
Line1:
Next
'Active la mise à jour visuelle
Application.ScreenUpdating = True
Application.Run "interval_synop"
End Sub |
Partager