Ciao Daniel,
Prova qualcosa del genere:
Fai clic dx sulla linguetta del foglio che viene aggiornato tramite DDE | Visualizza Codice |
e incolla il seguente codice nel modulo di codice del foglio:
'===========>>
Option Explicit
'----------->>
Private Sub WorksheetChange(ByVal Target As Range)
Dim Rng As Range
Const sIndirizzo_DDE As String = "C2:C1****00" '<<==== Modifica**[1]**
Set Rng = Me.Range(sIndirizzo_DDE)
If Not Intersect(Rng, Target) Is Nothing Then
Call MySpreads
End If
End Sub
'<<===========
Alt IM per inserire un nuvo modulo di codice e nel modulo vuoto, incolla il seguente codice:
'==========>>
Option Explicit
'---------->>
Public Sub MySpreads()
Dim WB As Workbook
Dim SH As Worksheet, destSH As Worksheet
Dim oDic As Object, oDic2 As Object
Dim Rng As Range, destRng As Range
Dim rCell As Range
Dim arrParentKeys As Variant, arrParentItems As Variant
Dim arrChildKeys As Variant, arrChildItems As Variant
Dim arrHeaders As Variant, arrOut() As Variant
Dim i As Long, j As Long, k As Long
Dim p As Long, q As Long, r As Long
Dim iLastRow As Long, jLastRow As Long
Dim dDate As Date, dTime As Date
Const sIntestazioni As String = _
"ID,Titolo-1,Quote,Titolo-2,Quote,Spread,Data,Ora"
Const mioParametro As Double = 10 '<<==== Modifica**[2]**
Const sReportShName As String = "Report" '<<==== Modifica [3]
Const sCopyFileName As String = "C:\MyAlarms\Alarm.xls" '<<==== Modifica**[4]**
On Error GoTo ErrHandler
Application.ScreenUpdating = False
dDate = Format(Date, "dd/mm/yy")
dTime = Format(Time, "hh:mm:ss")
Set WB = ThisWorkbook 'Workbooks("Pippo.xlsx") '<<==== Modifica**[5]**
With WB
Set SH = .Sheets("Foglio1") '<<==== Modifica**[6]**
On Error Resume Next
Set destSH = .Sheets(sReportShName)
If Err.Number = 9 Then
arrHeaders = Split(sIntestazioni, ",")
Set destSH = .Sheets.Add(after:=SH)
With destSH
.Name = sReportShName
With .Range("A1").Resize(1, UBound(arrHeaders) + 1)
.Value = arrHeaders
With .Font
.Size = 12
.Bold = True
.Underline = True
End With
End With
End With
End If
Err.Clear
End With
On Error GoTo ErrHandler
With SH
iLastRow = LastRow(SH, .Columns("A:A"))
Set Rng = .Range("A2:C" & iLastRow)
End With
With destSH
jLastRow = LastRow(destSH, .Columns("A:A"))
Set destRng = .Range("A" & jLastRow + 1 - (jLastRow <> 1))
End With
Set oDic = CreateObject("Scripting.Dictionary")
oDic.CompareMode = vbTextCompare
For i = 1 To Rng.Rows.Count
With Rng.Cells(i, 2)
If oDic.Exists(.Value) Then
Set oDic2 = oDic.Item(.Value)
If Not oDic2.Exists(.Value) Then
oDic2.Add Key:=.Offset(0, -1), Item:=.Offset(0, 1)
End If
Else
Set oDic2 = CreateObject("Scripting.Dictionary")
oDic2.CompareMode = vbTextCompare
oDic2.Add Key:=.Offset(0, -1), Item:=.Offset(0, 1)
oDic.Add Key:=.Value, Item:=oDic2
End If
End With
Next i
arrParentKeys = oDic.Keys
For i = 0 To UBound(arrParentKeys)
Set oDic2 = oDic.Item(arrParentKeys(i))
arrChildKeys = oDic2.Keys
arrChildItems = oDic2.Items
k = oDic2.Count
For j = 0 To k - 1
For p = j + 1 To k - 1
If Abs(arrChildItems(j) - arrChildItems(p)) > mioParametro Then
q = q + 1
ReDim Preserve arrOut(1 To 8, 1 To q)
arrOut(1, q) = arrParentKeys(i)
arrOut(2, q) = arrChildKeys(j)
arrOut(3, q) = arrChildItems(j)
arrOut(4, q) = arrChildKeys(p)
arrOut(5, q) = arrChildItems(p)
arrOut(6, q) = arrChildItems(j) - arrChildItems(p)
arrOut(7, q) = dDate
arrOut(8, q) = dTime
End If
Next p
Next j
Next i
With destRng.Resize(q, UBound(arrOut, 1))
.Value = Application.Transpose(arrOut)
.EntireColumn.AutoFit
For r = 1 To .Columns.Count Step 2
With destSH.UsedRange
.Columns(r).Interior.ColorIndex = 9
.Columns(r + 1).Interior.ColorIndex = 6
.Columns(6).Font.ColorIndex = 3
End With
Next r
End With
destSH.Copy
With Application
.DisplayAlerts = False
With ActiveWorkbook
.SaveAs Filename:=sCopyFileName, FileFormat:=OpenXMLWorkbook
.Close
End With
.DisplayAlerts = True
End With
Call InformaDaniel(q, dTime, dDate, sCopyFileName)
XIT:
Set oDic2 = Nothing
Set oDic = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
ErrHandler:
Call MsgBox(Prompt:="Error " _
& Err.Number _
& " (" _
& Err.Description _
& ") nella routine: Tester", _
Buttons:=vbCritical, _
Title:="ERRORE")
Err.Clear
Resume XIT
End Sub
'---------->>
Public Sub InformaDaniel(AlarmNum As Long, _
AlarmTime As Date, _
AlarmDate As Date, _
AlarmFile As String)
Dim oApp As Object
Dim oMail As Object
Dim sBody As String
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
sBody = "Ciao Daniel," _
& vbNewLine & vbNewLine _
& "Alle " & AlarmTime & Space(1) _
& Format(AlarmDate, "ddd dd/mm/yyyy") _
& vbNewLine & _
"Ci sono stato verificato " & AlarmNum _
& " coppie di titoli" _
& vbNewLine & _
"in cui lo spread e' stato in eccesso del valore previsto" _
& vbNewLine & _
"Vedi il file allegato per i dettagli."
On Error Resume Next
With oMail
.To = "topoDOTgiggioAToutlookDOTcom" '<<==== Modifica**[7]**
.CC = ""
.BCC = ""
.Subject = "SPREAD WARNING!"
.Body = sBody
.Attachments.Add (AlarmFile)
.Send
End With
On Error GoTo 0
Set oMail = Nothing
Set oApp = Nothing
End Sub
'---------->>
Public Function LastRow(SH As Worksheet, _
Optional Rng As Range)
If Rng Is Nothing Then
Set Rng = SH.Cells
End If
On Error Resume Next
LastRow = Rng.Find(What:="*", _
after:=Rng.Cells(1), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
'<<==========
[1] Sostituisci C2:C100 con l’intervallo di interesse.
******[2]******Sostituisci **** il valore di
10 **** con il valore voluto per li parametro del spread
[3]
Sostituisci il nome del foglio a scelta personale
[4]
Sostituisci C:\MyAlarms\Alarm.xls con un percorso e nome di file appropriato
[5]
Sostituisci Pippo.xlsxcon il nome del file di interesse
[6]
Sostituisci Foglio1con il nome del foglio che viene aggiornato tramite DDE
[7] Sostituisci topoDOTgiggioAToutlookDOTcom ****con
un indirizzo di email valido.
Alt-Q per chiudere l’editor di VBA e tornare a Excel.
Per vedere il funzionamento del codice 'a mano',:
Alt-F8 per aprire la finestrina macro
Seleziona MySpreads | Esegui
Il codice copi i suoi risultati nella forma di una tabella di 8 colonne sul foglio Report, lasciando una riga vuota sotto i dati precedenti. I dati avranno la seguente forma:
| ID |
Titolo-1 |
Quote |
Titolo-2 |
Quote |
Spread |
Data |
Ora |
| ITALIA |
Fiat |
8.56 |
Eni |
18.61 |
-10.05 |
20/04/2014 |
21:44:51 |
| ITALIA |
Eni |
18.61 |
Finmeccanica |
6.42 |
12.19 |
20/04/2014 |
21:44:51 |
| FRANCIA |
Carrefour |
28.6 |
Peugeout |
12.96 |
15.64 |
20/04/2014 |
21:44:51 |
| GERMANIA |
Mercedes |
17.25 |
Vw |
28.75 |
-11.5 |
20/04/2014 |
21:44:51 |
| GERMANIA |
Mercedes |
17.25 |
Audi |
6.95 |
10.3 |
20/04/2014 |
21:44:51 |
| GERMANIA |
Mercedes |
17.25 |
Polo |
3 |
14.25 |
20/04/2014 |
21:44:51 |
| GERMANIA |
Vw |
28.75 |
Audi |
6.95 |
21.8 |
20/04/2014 |
21:44:51 |
| GERMANIA |
Vw |
28.75 |
Polo |
3 |
25.75 |
20/04/2014 |
21:44:51 |
|
|
|
|
|
|
|
|
| ITALIA |
Fiat |
8.56 |
Eni |
18.61 |
-10.05 |
20/04/2014 |
21:49:31 |
| ITALIA |
Eni |
18.61 |
Finmeccanica |
6.42 |
12.19 |
20/04/2014 |
21:49:31 |
| FRANCIA |
Carrefour |
28.6 |
Peugeout |
12.96 |
15.64 |
20/04/2014 |
21:49:31 |
| GERMANIA |
Mercedes |
17.25 |
Vw |
28.75 |
-11.5 |
20/04/2014 |
21:49:31 |
| GERMANIA |
Mercedes |
17.25 |
Audi |
6.95 |
10.3 |
20/04/2014 |
21:49:31 |
| GERMANIA |
Mercedes |
17.25 |
Polo |
3 |
14.25 |
20/04/2014 |
21:49:31 |
| GERMANIA |
Vw |
28.75 |
Audi |
6.95 |
21.8 |
20/04/2014 |
21:49:31 |
| GERMANIA |
Vw |
28.75 |
Polo |
3 |
25.75 |
20/04/2014 |
21:49:31 |
Come vedrai, ho utilizzato i tuoi dati, aggiungendo un nuovo ID
GERMANIA e i titoli dependenti Audi, vW,
Polo e Mercedes; questo allo scopo di aumentare il numero di
'coppie'.
Inoltre, il codice crea un nuovo workbook Alarms.xlsx con una copia dei dati del foglio Report; questo file viene salvato nella cartella C:\myAlarms. Per soddifare la tua esigenza di "scattare allarmi (pop up o altro)", il codice
ti invia una email con allegato una copia del file Alarms.xlsx.
Infine, in utilizo
'normale', il codice di evento (WorkSheet_Change), nel modulo di codice del foglio che viene aggiornato tramite DDE (Foglio1), risponde ai cambiamenti dei valori di interesse (C2:C100) e avvia la procedura
MySpreads con i risultati descritti.
===
Regards,
Norman