Moderatori: Anthony47, Flash30005
#If VBA7 Then '!!! ON TOP OF THE VBA MODULE !!!!
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Dim IE As Object
Sub DaReteMir()
Dim myURLb As String, myID As Object
Dim I As Long, skArr, myTim As Single, dSh As Worksheet
Dim ArrSk(1 To 3), noStop As Boolean
'
Set dSh = Sheets("SheetA") '<<< Il foglio "parametri"
'
myURLb = "https://retemir.regione.marche.it/meteo/stazioni?codstaz="
If IE Is Nothing Then
noStop = False
Set IE = CreateObject("InternetExplorer.Application")
Else
noStop = True
End If
myTim = Timer
Debug.Print Format(Timer - myTim, "hh:mm:ss"), ">>>"
skArr = Array("Stazione", "Ultimo agg.", "Total Rain Today :", "Rain Intensity :", "Air Temperature :", "Relative Umidity :")
skArr1 = Array("Stazione", "Ultimo agg.", "Total Rain Todaly :", "Intensità Pioggia :", "Temperatura Aria :", "Umidità Relativa :")
skArr2 = Array("Stazione", "Ultimo agg.", "Pioggia TOT Oggi :", "Intensità di Pioggia :", "Temperatura Aria :", "Umidità Relativa :")
ArrSk(1) = skArr
ArrSk(2) = skArr1
ArrSk(3) = skArr2
Debug.Print "Via >>>>> " & Format(Now, "hh-mm-ss")
For I = 5 To dSh.Cells(Rows.Count, "A").End(xlUp).Row
dSh.Cells(I, 2).Resize(1, 6).ClearContents
dSh.Cells(I, 2).Value = "##Cerca##"
If dSh.Cells(I, 1) <> "" Then
reVai:
With IE
Debug.Print Format(Timer - myTim, "0.00"), myURLb & dSh.Cells(I, "A")
.navigate myURLb & dSh.Cells(I, "A")
.Visible = True
If noStop = False Then
Debug.Print "Halt per Autorizzazione su IE"
MsgBox ("Autorizzare IE")
.navigate myURLb & dSh.Cells(I, "A")
noStop = True
Else
End If
'' Stop '*** VEDI Testo
Do While .Busy: DoEvents: Loop 'Attesa not busy
Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With
'
myStart = Timer 'attesa addizionale
Sleep 500
If IE.LocationURL = "https://retemir.regione.marche.it/login" Then
'eventuale Login:
Debug.Print Format(Timer - myTim, "0.00"), "Eseguo Login"
Set myID = IE.document.getElementById("loginform")
myID.getElementsByTagName("input")(0).Value = dSh.Range("B1").Value
myID.getElementsByTagName("input")(1).Value = dSh.Range("B2").Value
Sleep 100
IE.document.getElementById("btn-login-extended").Click
Sleep 3000
runlin = True
GoTo reVai
End If
Debug.Print Format(Timer - myTim, "0.00"), "Arrivato su: " & IE.LocationURL
If IE.LocationURL <> (myURLb & dSh.Cells(I, 1)) And runlin Then
MsgBox ("Destinazione non raggiunta, operazione terminata")
Debug.Print myURL, IE.LocationURL
GoTo ExitA
End If
'Importa dati di Stazione:
Set myColl = Nothing
For J = 1 To 10
Set myColl = IE.document.getElementsByClassName("leaflet-popup-content")
If myColl.Length > 0 Then Exit For
Sleep 200
Next J
Debug.Print Format(Timer - myTim, "0.00"), "Typename(myColl): " & TypeName(myColl)
Debug.Print " ", "Items in myColl: " & myColl.Length
Debug.Print " ", "Loop J=" & J
If myColl.Length > 0 Then
On Error Resume Next
Debug.Print Format(Timer - myTim, "0.00"), "Ubound(mySplit): " & UBound(mySplit)
dSh.Cells(4, 2).Resize(1, 6) = skArr
mySplit = Split(myColl(0).innerText, Chr(10), , vbTextCompare)
dSh.Cells(I, 2) = mySplit(1)
dSh.Cells(I, 3) = GimmeVal(ArrSk, 1, myColl(0).innerText)
dSh.Cells(I, 4) = GimmeVal(ArrSk, 2, myColl(0).innerText)
dSh.Cells(I, 5) = GimmeVal(ArrSk, 3, myColl(0).innerText)
dSh.Cells(I, 6) = GimmeVal(ArrSk, 4, myColl(0).innerText)
dSh.Cells(I, 7) = GimmeVal(ArrSk, 5, myColl(0).innerText)
On Error GoTo 0
End If
End If
Next I
'Chiudi IE e completa
Debug.Print Format(Timer - myTim, "0.00"), "Completato"
ExitA:
'IE.Quit
'Set IE = Nothing
End Sub
Function GimmeVal(ByRef ArrArr, ByVal iInd As Long, ByVal InnerT As String) As Variant
Dim myPos As Long, I As Long, UpTo As Long
If Len(InnerT) < 5 Then Exit Function
For I = 1 To 3
' Set larr = ArrArr(I)
myPos = InStr(1, InnerT, ArrArr(I)(iInd), vbTextCompare)
If myPos > 0 Then Exit For
Next I
If myPos = 0 Then Exit Function
UpTo = InStr(myPos, InnerT & Chr(10), Chr(10), vbTextCompare)
GimmeVal = Mid(InnerT, myPos + Len(ArrArr(I)(iInd)), UpTo - myPos - Len(ArrArr(I)(iInd)))
End Function
Paolo67 ha scritto:Una cosa che ho notato è che quando importo un dato proveniente dal foglio "ReteMir" in un altro foglio (ma anche nello stesso è uguale...),questo risulta come formattato in modo strano,come se fosse testo...o qualcos'altro restituendomi un errore.
Ad esempio l'orario riportato della stazione 719 è riferito all'ora solare (GMT+1).
Visto che siamo in ora legale ossia GMt+2 ho impostato una semplice formula per aumentare in questo caso di 1 ora l'orario:
Se in C5 mi compare "04/05/2021 16:15" e voglio far attuare la modificare nella cella H5,in quest'ultima scrivo =C5+1/24 ma mi viene restituito un errore:
#VALORE! ossia un valore utilizzato nella formula è del tipo dati errato
Come hai intuito, il testo importato contiene caratteri spuri (CR, nel nostro caso); li elimino all'interno della nuova Function GimmeVal usando la WorksheetFunction.CleanUna cosa che ho notato è che quando importo un dato proveniente dal foglio "ReteMir" in un altro foglio (ma anche nello stesso è uguale...),questo risulta come formattato in modo strano,come se fosse testo...o qualcos'altro restituendomi un errore
#If VBA7 Then 'STRICTLY ON TOP OF A STANDARD MODULE
Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
Declare PtrSafe Function ShowWindow Lib "user32" (ByVal Hwnd As Long, ByVal nCmdSHow As Long) As LongPtr
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function ShowWindow Lib "user32" (ByVal Hwnd As Long, ByVal nCmdSHow As Long) As Long
#End If
''Declare Function SetForegroundWindow Lib "user32.dll" (ByVal Hwnd As Long) As Long
Declare Function GetForegroundWindow Lib "user32" () As Long
Declare Function BringWindowToTop Lib "user32" (ByVal Hwnd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, _
ByVal lpString As String, ByVal cch As Long) As Long
Dim IE As Object
Sub DaReteMir()
Dim myURLb As String, myID As Object
Dim I As Long, skArr, myTim As Single, dSh As Worksheet
Dim ArrSk(1 To 3), noStop As Boolean
Dim eHwnd As Long, ieHwnd As Long
'
Set dSh = Sheets("SheetA") '<<< Il foglio "parametri"
'
eHwnd = Application.Hwnd
myURLb = "https://retemir.regione.marche.it/meteo/stazioni?codstaz="
If IE Is Nothing Then
noStop = False
Set IE = CreateObject("InternetExplorer.Application")
Else
noStop = True
End If
ieHwnd = IE.Hwnd
Shell "RunDll32.exe InetCpl.Cpl, ClearMyTracksByProcess 3" 'Cokies + history
Sleep 1000
myTim = Timer
Debug.Print Format(Timer - myTim, "hh:mm:ss"), ">>>"
skArr = Array("Stazione", "Ultimo agg.", "Total Rain Today :", "Rain Intensity :", "Air Temperature :", "Relative Umidity :")
skArr1 = Array("Stazione", "Ultimo agg.", "Total Rain Todaly :", "Intensità Pioggia :", "Temperatura Aria :", "Umidità Relativa :")
skArr2 = Array("Stazione", "Ultimo agg.", "Pioggia TOT Oggi :", "Intensità di Pioggia :", "Temperatura Aria :", "Umidità Relativa :")
ArrSk(1) = skArr
ArrSk(2) = skArr1
ArrSk(3) = skArr2
Debug.Print "Via >>>>> " & Format(Now, "hh-mm-ss")
For I = 5 To dSh.Cells(Rows.Count, "A").End(xlUp).Row
dSh.Cells(I, 2).Resize(1, 6).ClearContents
dSh.Cells(I, 2).Value = "##Cerca##"
If dSh.Cells(I, 1) <> "" Then
reVai:
With IE
Debug.Print Format(Timer - myTim, "0.00"), myURLb & dSh.Cells(I, "A")
.navigate myURLb & dSh.Cells(I, "A")
.Visible = True
If noStop = False Then
Debug.Print "Halt per Autorizzazione su IE"
Sleep 100
rispo = OnTop(eHwnd, 5)
Debug.Print "Requested XL: " & eHwnd, "Got: " & rispo
MsgBox ("Autorizzare IE")
.navigate myURLb & dSh.Cells(I, "A")
noStop = True
rispo = OnTop(ieHwnd, 5)
Debug.Print "Requested IE: " & ieHwnd, "Got: " & rispo
Else
rispo = OnTop(ieHwnd, 5)
Debug.Print "Requested IE: " & ieHwnd, "Got: " & rispo
End If
Do While .Busy: DoEvents: Loop 'Attesa not busy
Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With
'
myStart = Timer 'attesa addizionale
Sleep 200
If IE.LocationURL = "https://retemir.regione.marche.it/login" Then
'eventuale Login:
Debug.Print Format(Timer - myTim, "0.00"), "Eseguo Login"
Set myID = IE.document.getElementById("loginform")
myID.getElementsByTagName("input")(0).Value = dSh.Range("B1").Value
myID.getElementsByTagName("input")(1).Value = dSh.Range("B2").Value
Sleep 100
IE.document.getElementById("btn-login-extended").Click
Sleep 3000
runlin = True
GoTo reVai
End If
Debug.Print Format(Timer - myTim, "0.00"), "Arrivato su: " & IE.LocationURL
If IE.LocationURL <> (myURLb & dSh.Cells(I, 1)) And runlin Then
MsgBox ("Destinazione non raggiunta, operazione terminata")
Debug.Print myURL, IE.LocationURL
GoTo ExitA
End If
'Importa dati di Stazione:
Set myColl = Nothing
For J = 1 To 10
Set myColl = IE.document.getElementsByClassName("leaflet-popup-content")
If myColl.Length > 0 Then Exit For
Sleep 200
Next J
Debug.Print Format(Timer - myTim, "0.00"), "Typename(myColl): " & TypeName(myColl)
Debug.Print " ", "Items in myColl: " & myColl.Length
Debug.Print " ", "Loop J=" & J
If myColl.Length > 0 Then
On Error Resume Next
Debug.Print Format(Timer - myTim, "0.00"), "Ubound(mySplit): " & UBound(mySplit)
dSh.Cells(4, 2).Resize(1, 6) = skArr
mySplit = Split(myColl(0).innerText, Chr(10), , vbTextCompare)
dSh.Cells(I, 2) = mySplit(1)
dSh.Cells(I, 3) = GimmeVal(ArrSk, 1, myColl(0).innerText)
dSh.Cells(I, 4) = GimmeVal(ArrSk, 2, myColl(0).innerText)
dSh.Cells(I, 5) = GimmeVal(ArrSk, 3, myColl(0).innerText)
dSh.Cells(I, 6) = GimmeVal(ArrSk, 4, myColl(0).innerText)
dSh.Cells(I, 7) = GimmeVal(ArrSk, 5, myColl(0).innerText)
On Error GoTo 0
End If
End If
Next I
'Chiudi IE e completa
Debug.Print Format(Timer - myTim, "0.00"), "Completato"
rispo = OnTop(eHwnd, 5)
Debug.Print "Requested XL: " & eHwnd, "Got: " & rispo
DoEvents
ExitA:
'IE.Quit
'Set IE = Nothing
End Sub
Function GimmeVal(ByRef ArrArr, ByVal iInd As Long, ByVal InnerT As String) As Variant
Dim myPos As Long, I As Long, UpTo As Long
If Len(InnerT) < 5 Then Exit Function
For I = 1 To 3
' Set larr = ArrArr(I)
myPos = InStr(1, InnerT, ArrArr(I)(iInd), vbTextCompare)
If myPos > 0 Then Exit For
Next I
If myPos = 0 Then Exit Function
UpTo = InStr(myPos, InnerT & Chr(10), Chr(10), vbTextCompare)
GimmeVal = Application.WorksheetFunction.Clean(Mid(InnerT, myPos + Len(ArrArr(I)(iInd)), UpTo - myPos - Len(ArrArr(I)(iInd))))
End Function
Function OnTop(ByVal Handle As Long, Optional ByVal MaxI As Long = 5) As Long
Dim II As Long, CW As Long, WinText As String, cCapt As String
BringWindowToTop (Handle)
For II = 1 To MaxI
CW = GetForegroundWindow
Sleep 100: DoEvents
If CW = Handle Then Exit For
Next II
WinText = String(255, vbNullChar)
cCapt = GetWindowText(Handle, WinText, 255)
cCapt = Application.WorksheetFunction.Clean(WinText)
Debug.Print "Bring On Top: " & Handle, II, cCapt
OnTop = CW
End Function
'altre istruzioni
GimmeVal = Application.WorksheetFunction.Clean(Mid(InnerT, myPos + Len(ArrArr(I)(iInd)), UpTo - myPos - Len(ArrArr(I)(iInd))))
If IsDate(GimmeVal) Then GimmeVal = CDate(GimmeVal) 'AGGIUNGI QUESTA
End Function
Come hai intuito, il testo importato contiene caratteri spuri (CR, nel nostro caso); li elimino all'interno della nuova Function GimmeVal usando la WorksheetFunction.Clean
#If VBA7 Then '!!! ON TOP OF THE VBA MODULE !!!!
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#End If
Sub DaReteMir()
Dim IE As Object, myURLb As String, myID As Object
Dim I As Long, skArr, myTim As Single, dSh As Worksheet
Dim ArrSk(1 To 3)
'
Set dSh = Sheets("ReteMir") '<<< Il foglio "parametri"
'
myURLb = "https://retemir.regione.marche.it/meteo/stazioni?codstaz="
Set IE = CreateObject("InternetExplorer.Application")
myTim = Timer
Debug.Print Format(Timer - myTim, "hh:mm:ss"), ">>>"
skArr = Array("Stazione", "Ultimo agg.", "Total Rain Today :", "Rain Intensity :", "Air Temperature :", "Relative Umidity :")
skArr1 = Array("Stazione", "Ultimo agg.", "Total Rain Todaly :", "Intensità Pioggia :", "Temperatura Aria :", "Umidità Relativa :")
skArr2 = Array("Stazione", "Ultimo agg.", "Pioggia TOT Oggi :", "Intensità di Pioggia :", "Temperatura Aria :", "Umidità Relativa :")
ArrSk(1) = skArr
ArrSk(2) = skArr1
ArrSk(3) = skArr2
For I = 5 To dSh.Cells(Rows.Count, "A").End(xlUp).Row
dSh.Cells(I, 2).Resize(1, 6).ClearContents
dSh.Cells(I, 2).Value = "##Cerca##"
If dSh.Cells(I, 1) <> "" Then
reVai:
With IE
Debug.Print Format(Timer - myTim, "0.00"), myURLb & dSh.Cells(I, "A")
.navigate myURLb & dSh.Cells(I, "A")
.Visible = True
'' Stop '*** VEDI Testo
Do While .Busy: DoEvents: Loop 'Attesa not busy
Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With
'
myStart = Timer 'attesa addizionale
Sleep 18000
If IE.LocationURL = "https://retemir.regione.marche.it/login" Then
'eventuale Login:
Debug.Print Format(Timer - myTim, "0.00"), "Eseguo Login"
Set myID = IE.document.getElementById("loginform")
myID.getElementsByTagName("input")(0).Value = dSh.Range("B1").Value
myID.getElementsByTagName("input")(1).Value = dSh.Range("B2").Value
Sleep 100
IE.document.getElementById("btn-login-extended").Click
Sleep 3000
runlin = True
GoTo reVai
End If
Debug.Print Format(Timer - myTim, "0.00"), "Arrivato su: " & IE.LocationURL
If IE.LocationURL <> (myURLb & dSh.Cells(I, 1)) And runlin Then
MsgBox ("Destinazione non raggiunta, operazione terminata")
Debug.Print myURL, IE.LocationURL
GoTo ExitA
End If
'Importa dati di Stazione:
Set myColl = Nothing
For J = 1 To 10
Set myColl = IE.document.getElementsByClassName("leaflet-popup-content")
If myColl.Length > 0 Then Exit For
Sleep 200
Next J
Debug.Print Format(Timer - myTim, "0.00"), "Typename(myColl): " & TypeName(myColl)
Debug.Print " ", "Items in myColl: " & myColl.Length
Debug.Print " ", "Loop J=" & J
If myColl.Length > 0 Then
On Error Resume Next
Debug.Print Format(Timer - myTim, "0.00"), "Ubound(mySplit): " & UBound(mySplit)
dSh.Cells(4, 2).Resize(1, 6) = skArr
mySplit = Split(myColl(0).innerText, Chr(10), , vbTextCompare)
dSh.Cells(I, 2) = mySplit(1)
dSh.Cells(I, 3) = GimmeVal(ArrSk, 1, myColl(0).innerText)
dSh.Cells(I, 4) = GimmeVal(ArrSk, 2, myColl(0).innerText)
dSh.Cells(I, 5) = GimmeVal(ArrSk, 3, myColl(0).innerText)
dSh.Cells(I, 6) = GimmeVal(ArrSk, 4, myColl(0).innerText)
dSh.Cells(I, 7) = GimmeVal(ArrSk, 5, myColl(0).innerText)
On Error GoTo 0
End If
End If
Next I
'Chiudi IE e completa
Debug.Print Format(Timer - myTim, "0.00"), "Completato"
ExitA:
IE.Quit
Set IE = Nothing
End Sub
Function GimmeVal(ByRef ArrArr, ByVal iInd As Long, ByVal InnerT As String) As Variant
Dim myPos As Long, I As Long, UpTo As Long
If Len(InnerT) < 5 Then Exit Function
For I = 1 To 3
' Set larr = ArrArr(I)
myPos = InStr(1, InnerT, ArrArr(I)(iInd), vbTextCompare)
If myPos > 0 Then Exit For
Next I
If myPos = 0 Then Exit Function
UpTo = InStr(myPos, InnerT & Chr(10), Chr(10), vbTextCompare)
GimmeVal = Mid(InnerT, myPos + Len(ArrArr(I)(iInd)), UpTo - myPos - Len(ArrArr(I)(iInd)))
End Function
Se il dato viene importato in formato "data" allora la formattazione la decidi tu, tramite il comando Formato celle. Puoi controllare se si tratta di "data" formattando la cella con un formato numerico con due decimali: se viene visualizzato come numero (es 44323,43) allora e' ragionevolmente una data.Non riesco a risolvere questo:
Mi appare col formato aaaa-mm-gg hh:mm:ss
2021-05-07 10:14:00
ma vorrei che mi apparisse con
gg/mm/aaaa hh:mm
Non so cosa hai fatto e cosa hai messo in A1...Anche per quest'altro non riesco a risolvere,mi appare:
Stazione Frontino, Pian dei Prati alt. m. 780 - Dati aggiornati il 06/05/21 alle ore 21.01
cerco di estrapolare solo il formato gg/mm/aaaa hh:mm
con
- Codice: Seleziona tutto
=SOSTITUISCI(STRINGA.ESTRAI(A1;67;23);" alle ore ";"")*1
ma ottengo un errore #VALORE!
Se il dato viene importato in formato "data" allora la formattazione la decidi tu, tramite il comando Formato celle. Puoi controllare se si tratta di "data" formattando la cella con un formato numerico con due decimali: se viene visualizzato come numero (es 44323,43) allora e' ragionevolmente una data.
Non so cosa hai fatto e cosa hai messo in A1...
Io se provo a leggere la stazione Frontino-721 ottengo dati pulitI:
=DATA.VALORE(SOSTITUISCI(A1;"Stato Centralina: Online Aggiornamento:";))+ORARIO.VALORE(SOSTITUISCI(A1;"Stato Centralina: Online Aggiornamento:";))
=DATA.VALORE(SOSTITUISCI(EY64;"Stazione Frontino, Pian dei Prati alt. m. 780 - Dati aggiornati il "));+ORARIO.VALORE(SOSTITUISCI(EY64;"Stazione Frontino, Pian dei Prati alt. m. 780 - Dati aggiornati il ";))
=ANNULLA.SPAZI(SOSTITUISCI(SOSTITUISCI(STRINGA.ESTRAI(A1;TROVA("Dati aggiornati il";A1)+18;99);"alle ore";"");".";":"))
Intanto do per scontato che l'estrazione di data & ora dalla stringa Stato Centralina: Online Aggiornamento: 2021-05-07 11:00:00 sia stata risolta.
Tuttavia la stringa ottenuta con questa formula e' piu' "pulita" rispetto a quella precedente; quindi probabilmente riuscirai ad avere il valore Data & Ora (gia' in formato Data & Orario) aggiugendo "*1" (senza le virgolette) in coda alla formula appena data, senza bisogno di passare per Data.Valore e Orario.Valore
Torna a Applicazioni Office Windows
Inserire dati filtrati da 2 file ad un terzo file Autore: Ricky0185 |
Forum: Applicazioni Office Windows Risposte: 14 |
Perchè l'importazione dati con Selenium non fuziona? Autore: aggittoriu |
Forum: Applicazioni Office Windows Risposte: 7 |
copia di dati da un file chiuso e elaborazione Autore: luca62 |
Forum: Applicazioni Office Windows Risposte: 2 |
Visitano il forum: Nessuno e 8 ospiti