Scritto da brianzatrade
Private Sub Worksheet_Calculate()
'
Dim bid1Prc As Range
Dim bid1PrcBuff As Range
Dim ask1Prc As Range
Dim asK1PrcBuff As Range
Dim bid1Vol As Range
Dim bid1VolBuff As Range
Dim ask1Vol As Range
Dim ask1VolBuff As Range
Dim volOrig As Range
Dim volDest As Range
Dim prcDest As Range
Dim volBuffer As Range
Dim numTrade As Range
Dim volAcq As Range
Dim volVend As Range
Dim tradePrc As Range
Dim tradePrcBuff As Range
Dim tradeTypeDest As Range
Dim bestBidPrc As Double
Dim bestAskPrc As Double
Dim bestBidVol As Long
Dim bestAskVol As Long
Dim dayVol As Long
Dim tradeVol As Long 'Volume del trade
Dim oldVol As Long
Dim r As Long 'Numero riga del foglio
Dim c As Long 'Numero colonna del foglio
Dim i As Byte
Dim tradeType As Byte
Dim volAcqVal As Long
Dim volVendVal As Long
'
Dim myObject As Object
'
' Carica i dati
'
Set bid1Prc = Range("N4") 'Il miglior prezzo denaro è nella cella "N4"
Set bid1PrcBuff = Range("N1") 'Deposito il valore del miglior denaro nella cella "N1"
Set ask1Prc = Range("O4") 'Il miglior prezzo lettera è nella cella "O4"
Set asK1PrcBuff = Range("O1")
Set bid1Vol = Range("M4") 'Il volume del miglior denaro è nella cella "M4"
Set bid1VolBuff = Range("M1")
Set ask1Vol = Range("P4") 'Il volume della miglior lettera è nella cella "P4"
Set ask1VolBuff = Range("P1")
Set volOrig = Range("I3") 'Il volume della giornata è nella cella "I3"
'
On Error Resume Next
volOrig.Select
dayVol = volOrig.Value
Do While dayVol = 0
volOrig.Select
dayVol = volOrig.Value 'Carico il volume della giornata al momento del ricalcolo
Loop
bid1Prc.Select '-Seleziona la cella del miglior denaro
bid1PrcBuff.Value = bid1Prc.Value '-Copia il valore del miglior denaro nella cella di buffer
bestBidPrc = bid1PrcBuff.Value '-Carica il valore del miglior denaro nella variabile <bestBidPrc>
Do While bestBidPrc = 0 '-Se il valore del miglior denaro è 0, ripete il ciclo. Quando
bid1Prc.Select 'verrà caricato un valore diverso da 0 il ciclo si interromperà
bid1PrcBuff.Value = bid1Prc.Value
bestBidPrc = bid1PrcBuff.Value 'Carico il miglior prezzo in denaro
Loop
ask1Prc.Select
asK1PrcBuff.Value = ask1Prc.Value
bestAskPrc = asK1PrcBuff.Value
Do While bestAskPrc = 0
ask1Prc.Select
asK1PrcBuff.Value = ask1Prc.Value
bestAskPrc = asK1PrcBuff.Value 'Carico il miglior prezzo in lettera
Loop
bid1Vol.Select
bid1VolBuff.Value = bid1Vol.Value
bestBidVol = bid1VolBuff.Value 'Carico il volume del miglior denaro
ask1Vol.Select
ask1VolBuff.Value = ask1Vol.Value
bestAskVol = ask1VolBuff.Value 'Carico il volume della miglior lettera
'
' verifica ed elabora i dati
'
Set myObject = ThisWorkbook.Worksheets("TS_AEM") 'Il foglio di lavoro è: TS_AEM
Set volBuffer = myObject.Range("B2") 'Il buffer del volume (volume precedente) è nella cella "B2"
Set numTrade = myObject.Range("B1") 'Il numero dei trades è nella cella "B1"
Set volAcq = myObject.Range("F1") 'Il volume cumulativo degli acquisti è nella cella "F1"
Set volVend = myObject.Range("F2") 'Il volume cumulativo delle vendite è nella cella "F2"
Set tradePrc = Range("B3") 'Il prezzo del trade è nella cella "B3"
Set tradePrcBuff = Range("B5")
r = 0
volBuffer.Select
oldVol = volBuffer.Value
'
If dayVol <> oldVol Then
tradeVol = dayVol - oldVol
If tradeVol > 0 Then
tradePrc.Select '-Seleziona cella B3 del foglio AEM
tradePrcBuff.Value = tradePrc.Value '-Copia il contenuto della cella B3 nella cella B5
tradePrice = tradePrcBuff.Value '-------------Creare routine per verificare prezzo del trade
numTrade.Select '-Seleziona cella B1 del foglio TS_AEM
r = numTrade.Value + 1 '-Calcola il numero della riga in cui scrivere i dati
If tradePrice = bestBidPrc Then '-Se il prezzo dello scambio,dalla cella di buffer, è uguale al
tradeType = 1 'miglior denaro l'iniziativa è stata di un Venditore
volVend.Select
volVend.Value = volVend.Value + tradeVol
Else
tradeType = 2 'Acquirente
volAcq.Select
volAcqVal = volAcq.Value
volAcq.Value = volAcqVal + tradeVol
End If
Set volDest = myObject.Cells(2 + r, 3)
Set prcDest = myObject.Cells(2 + r, 4)
Set tradeTypeDest = myObject.Cells(2 + r, 5)
volDest.Select
volDest.Value = tradeVol
prcDest.Select
prcDest.Value = tradePrice
tradeTypeDest.Select
tradeTypeDest.Value = tradeType
volBuffer.Select
volBuffer.Value = dayVol
numTrade.Select
numTrade.Value = r
End If
End If
End Sub
Questa macro fa quasi lo stesso lavoro. Non guardare la sintassi perchè ci sono molti passaggi superflui che servono a me per vedere meglio lo svolgersi delle operazioni.
Sergio