Ho provato a fare un programma che elabora grafici di analisi tecnica. Spero sia utile a chi vuole applicarsi a questa materia magari per chi deve fare studi all'università o anche studi statistici. Più che altro a parte l'organizzazione dei dati, è stato una continua scoperta di come poter costruire grafici anche un pò complessi con diverse modalità:
1) da selezione libera (serie selrange_nome_grafico);
2) grafici da selezione fissa su foglio formule (Modulo11);
3) grafici da selezione fissa in foglio dedicato a ciascun grafico (serie Grafico_Nome_Foglio).
La soluzione 1) è stata veramente una scoperta interessante perchè costruisce il grafico in modo dinamico in base alla selezione desiderata. L'importante è selezionare i dati per quello specifico tipo di grafico.
Le varie modalità di costruzione del grafico mostrano quali dati vengono usati.
Ho provato a fare un calcolo di simulazione del trend del titolo un modello che calcola la media tra il valore della retta di regressione e media mobile a venti giorni.
Quindi si importa i dati come da macro di Anthony in foglio "Dati", si copiano in foglio "Formule" dove si elaborano tutti i vari oscillatori, supporti e resistenze (tre), alcuni grafici sono correlati anche dall'andamento del polinomiale con evidenza di R^2:
MACD
Relative Strengh Indicator
Previsione trend
Andamento storico e regressione
Fogli esistenti
Nel foglio "Dati" in A1 viene messo il nome del titolo.
Il foglio "Formule" viene alimentato dalla macro (dati ed intestazioni)
Il foglio "Controlli" contiene alcuni dati per far funzionare le macro:
in J2: il numero di dati per l'elaborazione della simulazione
in J4: il nome del titolo che viene poi collegato in A1 del foglio"Dati"
in J6: il nome del file pdf da scaricare con le informazioni della società (bisogna cercarlo e poi impostare il percorso)
in J8: il percorso completo da dove scaricare il file pdf
in J10: la directory di download
in J12: il numero di giorni per il calcolo della previsione (la macro cancella giorni di festa, sabati, domeniche e i due giorni di Pasqua e Pasquetta)
in J14: eventuale correttore per il calcolo dei valori di trend
Per questi ho notato che la regressione calcola valori troppo alti, l'intervallo di confidenza più bassi del valore del titolo.
- Codice: Seleziona tutto
Sub Lancia_tutte_le_macro()
'Lancia tutte le macro
Application.EnableEvents = False
Call Copia_file ' Esegue copia di backup del file
Call mMain ' Scarica i dati di un anno da Yahoo Finanza su una file csv
Call Cancella_Grafici ' Cancella Grafici sul Foglio1 e singolo foglio intestato ai grafici
Call Cancella_bilancio ' Cancella tabella bilancio (fonte: http://www.evaluation.it/)
Call ImportaDati ' Cancella e importa dati quotazioni
Call Scocca ' Scocca
Call Formule ' Formule
Call Cancella_righe_vuote_dopo_copia_incolla_formule ' Cancella righe vuote dopo copia-incolla formule
Call Grafici ' Grafici su foglio Formule
Call Grafico_chiusura_Regressione_Foglio ' Grafico chiusura Regressione Foglio
Call Grafico_Relative_Strenght_Indicator_Foglio ' Grafico Relative Strenght Indicator Foglio
Call Grafico_Chiusura_Supporti_e_Resistenze_Foglio ' Grafico Chiusura Supporti e Resistenze Foglio
Call Grafico_MACD_Foglio ' Grafico MACD Foglio
Call Scarica_Bilancio_Enel_Tabella_su_excel ' Scarica blancio tabella su excel (fonte: http://www.evaluation.it/)
Call Scarica_Bilancio_Enel_File ' Scarica Bilancio Enel
Call Colonna_data_per_simulazione ' Colonna data per simulazione (in colonna A)
Call Data_per_calcolo_dati_di_previsione ' Incremento colonna dati in G numero data per calcolo dati di previsione
Call Regressione_per_calcolo_dati_di_previsione ' Incremento colonna dati in AH della regressione per calcolo dati di previsione.
Call Media_mobile_a_20_giorni ' Incremento colonna dati in AG della media mobile a 20 giorni per calcolo dati di previsione
Call Calcola_dati_previsione ' Calcola dati previsione in colonna E (altro metodo di calcolo dati previsione - macro 17)
Call R1_per_calcolo_dati_di_previsione ' R1 per calcolo dati di previsione
Call S1_per_calcolo_dati_di_previsione ' S1 per calcolo dati di previsione
Call R2_per_calcolo_dati_di_prevision ' R2 per calcolo dati di previsione
Call S2_per_calcolo_dati_di_previsione ' S2 per calcolo dati di previsione
Call R3_per_calcolo_dati_di_previsione ' R3 per calcolo dati di previsione
Call S3_per_calcolo_dati_di_previsione ' S3 per calcolo dati di previsione
Call Cancella_righe_dopo_dati_previsione ' Cancella righe con data non valorizzata dopo elaborazone dati previsione
Call Grafico_previsione ' Grafico previsione
Call Grafico_previsione_Foglio ' Grafico previsione foglio
Call Grafico_Dettaglio_Previsione_Foglio ' Grafico Dettaglio Previsione con un mese di dati
Application.EnableEvents = True
MsgBox "Elaborazione terminata"
End Sub
- Codice: Seleziona tutto
Sub Copia_file()
With ActiveWorkbook
.SaveCopyAs .Path & "\" & Format(Date, "yyyymmdd") & "Back-up" & " " & [AC8] & "Scarico dati da Yahoo Finance" & [AD8] & ".xlsm"
End With
End Sub
- Codice: Seleziona tutto
Dim IE As Object 'RIGOROSAMENTE IN TESTA AL MODULO
'Scarica i dati di un anno da Yahoo Finanza su una file csv
Sub mMain()
Dim aColl As Object, bColl As Object, myTIT As String, myPath As String
'
'myTIT = "ENEL" '<<< Il titolo, senza ".MI"
Sheets("Dati").Select
If Range("A1").Value = "" Then Exit Sub
myTIT = Range("A1").Value
'myPath = "C:\PROVA"
Call ApriYF(myTIT)
Set aColl = IE.document.getElementById("Col1-1-HistoricalDataTable-Proxy") '.getElementsByTagName("input")
myWait (2)
mlink0 = IE.document.getElementsByClassName("Fl(end) Pos(r) T(-6px)")(0).getElementsByTagName("a")(0).href
'
'aColl.getElementsByTagName("input")(0).Click
myWait (0.2)
'
Set bColl = IE.document.getElementsByClassName("P(5px) W(37px) H(15px) Fl(start) Mb(5px) Cur(p) Bdbc($c-fuji-blue-1-a):h Bdbs(s) Bdbw(3px) Bdbc(t)")
'bColl(bColl.Length - 1).Click 'Max
myWait (0.2)
'
'IE.document.getElementsByClassName(" Bgc($c-fuji-blue-1-b) Bdrs(3px) Px(20px) Miw(100px) Whs(nw) Fz(s) Fw(500) C(white) Bgc($actionBlueHover):h Bd(0) D(ib) Cur(p) Td(n) Py(9px) Miw(80px)! Fl(start)")(0).Click 'Finito
myWait (0.2)
'
IE.document.getElementsByClassName(" Bgc($c-fuji-blue-1-b) Bdrs(3px) Px(20px) Miw(100px) Whs(nw) Fz(s) Fw(500) C(white) Bgc($actionBlueHover):h Bd(0) D(ib) Cur(p) Td(n) Py(9px) Fl(end)")(0).Click 'Applica
myWait (0.2)
mlink = mlink0
On Error Resume Next
mytim = Timer
Do
mlink = IE.document.getElementsByClassName("Fl(end) Pos(r) T(-6px)")(0).getElementsByTagName("a")(0).href
If Mid(mlink, InStr(1, mlink, "?period1", vbTextCompare) + 8, 7) <> _
Mid(mlink0, InStr(1, mlink0, "?period1", vbTextCompare) + 8, 7) Then Exit Do
If Timer > (mytim + 15) Then Exit Do
Loop
'Debug.Print Format(Timer - mytim, "0.00")
'Debug.Print 1, mlink0
'Debug.Print 2, mlink
On Error GoTo 0
myWait (0.5)
'GoTo impF
IE.navigate mlink
myWait (0.2)
mytim = Timer
Do While IE.Busy
DoEvents: If Timer > (mytim + 10) Then Exit Do:
Loop 'Attesa not busy
Do While IE.readyState <> 4
DoEvents: If Timer > (mytim + 30) Then Exit Do
Loop 'Attesa documento
On Error Resume Next
IE.Quit
Set IE = Nothing
End Sub
Sub ApriYF(ByVal myID As String, Optional mySt As Long = 0)
'
bURL = "https://it.finance.yahoo.com/quote/###.MI/history?p=###"
'Set IE = Nothing
If IE Is Nothing Then Set IE = CreateObject("InternetExplorer.Application")
'
With IE
.navigate Replace(bURL, "###", myID, , , vbTextCompare)
.Visible = True
Do While .Busy: DoEvents: Loop 'Attesa not busy
Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With
If mySt > 0 Then Stop
End Sub
Private Sub myWait(ByVal WSec As Single, Optional ByVal TOut As Single = 10)
'Attende WSec secondi (o il doppio se mezzanotte)
Dim lTim As Single
'
lTim = Timer
Do
DoEvents
If Timer > (lTim + WSec) Then Exit Do
DoEvents
If Timer < lTim And Timer > WSec Then Exit Do
Loop
End Sub
- Codice: Seleziona tutto
Sub Cancella_Grafici()
On Error Resume Next
'Singolo foglio intestato ai grafici
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Chiusura e retta di regressione").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = False
Sheets("RSI").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Application.DisplayAlerts = False
Sheets("Chiusura, supporti e resistenze").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Application.DisplayAlerts = False
Sheets("MACD").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Application.DisplayAlerts = False
Sheets("Dettaglio Previsione").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Application.DisplayAlerts = False
Sheets("Previsione").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Application.DisplayAlerts = False
Sheets("Chiusura, previsione, S e R").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End Sub
- Codice: Seleziona tutto
Sub Cancella_bilancio()
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Bilancio").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End Sub
- Codice: Seleziona tutto
Option Explicit
Sub ImportaDati()
Sheets("Dati").Select
Range("A2:BJ1000").Value = ""
'Application.UseSystemSeparators = False
' Application.DecimalSeparator = "."
' Application.ThousandsSeparator = ","
'With _
' ActiveSheet.QueryTables.Add _
' (Connection:="TEXT;C:\Users\Windows7\Downloads\" & Sheets("Foglio1").Range("A1").Value & ".csv", _
' Destination:=Range("$A$4"))
' .FieldNames = True
' .PreserveFormatting = True
' .SaveData = True
' .AdjustColumnWidth = True
' .TextFilePlatform = 850
' .TextFileStartRow = 1
' .TextFileParseType = xlDelimited
' .TextFileCommaDelimiter = True
' .TextFileColumnDataTypes = Array(xlGeneralFormat, xlGeneralFormat, _
' xlGeneralFormat, xlGeneralFormat, _
' xlGeneralFormat, xlGeneralFormat)
'.Refresh
'End With
Dim Carica_dati As String
Dim nomequery As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Text Files", "*.txt,*.csv"
If .Show = 0 Then Exit Sub
Carica_dati = "text;" & .SelectedItems(1)
End With
With ActiveSheet.QueryTables.Add(Connection:=Carica_dati, Destination:=Sheets("Dati").Range("A4"))
nomequery = .Name
.AdjustColumnWidth = False
.TextFileCommaDelimiter = True
.TextFileColumnDataTypes = Array(2, 1)
.Refresh BackgroundQuery:=False
End With
'Toglie colonna con valore chiusura aggiustato
Columns("F:F").Select
Columns("F:F").Delete
'Toglie riga con valore null
Dim i As Long, lastrow As Long
lastrow = Range("B" & Rows.Count).End(xlUp).Row
Sheets("Dati").Select
For i = lastrow To 1 Step -1
If LCase(Cells(i, "B").Value) Like "null" Or _
UCase(Cells(i, "C").Value) Like "null" Or _
UCase(Cells(i, "D").Value) Like "null" Or _
UCase(Cells(i, "E").Value) Like "null" Or _
UCase(Cells(i, "F").Value) Like "null" Then _
Rows(i).EntireRow.Delete
Next i
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Replace What:="-", Replacement:="/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub
- Codice: Seleziona tutto
Sub Scocca()
Sheets("Formule").Select
Range("A1:BJ1000").Value = ""
'Nome titolo
Range("A1").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 16
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
'Quotazioni
Range("A2").Value = "Quotazioni"
Range("A3").Value = "Data"
Range("B3").Value = "Apertura"
Range("C3").Value = "Massimo"
Range("D3").Value = "Minimo"
Range("E3").Value = "Chiusura"
Range("F3").Value = "Volume"
Range("G3").Value = "Data numero"
Range("A2").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("A3").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("B3").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("C3").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("D3").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("E3").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("F3").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("G3").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 11
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
'Supporti e resistenze
Range("H2").Value = "Supporti e resistenze"
Range("H3").Value = "R1"
Range("I3").Value = "S1"
Range("J3").Value = "R2"
Range("K3").Value = "S2"
Range("L3").Value = "R3"
Range("M3").Value = "S3"
Range("H2").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
'Relative Strenght Indicator
Range("N2").Value = "Relative Strenght Indicator"
Range("N3").Value = "Differenze giornaliere"
Range("O3").Value = "Rialzi giornalieri"
Range("P3").Value = "Ribassi giornalieri"
Range("Q3").Value = "SMA 14 Rialzi"
Range("R3").Value = "SMA 14 Ribassi"
Range("S3").Value = "RS"
Range("T3").Value = "RSI-Relative Strenght Index"
Range("N2").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
'MACD
Range("U2").Value = "MACD"
Range("U3").Value = "EMA12"
Range("V3").Value = "EMA26"
Range("W3").Value = "MACD"
Range("X3").Value = "Signal"
Range("Y3").Value = "Istogramma"
Range("U2").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
'Calcolo Supporti e Resistenze
Range("AA2").Value = "Calcolo Supporti e Resistenze"
Range("AA3").Value = "H"
Range("AA4").Value = "L"
Range("AA5").Value = "C"
Range("AA6").Value = "AP"
Range("AA7").Value = "R1"
Range("AA8").Value = "S1"
Range("AA9").Value = "R2"
Range("AA10").Value = "S2"
Range("AA11").Value = "R3"
Range("AA12").Value = "S3"
Range("AA2").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
'Retta di Regressione
Range("AD2").Value = "Retta di Regressione"
Range("AD5").Value = "Regr.lin"
Range("AD6").Value = "Formule"
Range("AE3").Value = "Retta regressione: m*x"
Range("AF3").Value = "Retta regressione: b"
Range("AE4").Value = "Pendenza"
Range("AF4").Value = "Intercetta"
Range("AH3").Value = "Retta di Regressione"
Range("AD2").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
Range("AG2").Value = "EMA20"
Range("AG3").Value = "per previsione"
'Grafici
Range("AJ1").Value = "Grafici"
Range("AJ1").Select
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
'Adatta colonne
Columns("A:AI").Select
Selection.Columns.AutoFit
Range("C1").Select
Columns("C:C").ColumnWidth = 7.29
Columns("A:A").ColumnWidth = 12.71
Columns("H:H").ColumnWidth = 2.86
Columns("N:N").ColumnWidth = 14.71
Columns("N:N").ColumnWidth = 16.57
Columns("N:N").ColumnWidth = 18
Columns("N:N").ColumnWidth = 19.43
Columns("AD:AD").ColumnWidth = 9
Columns("AG:AG").ColumnWidth = 13
Columns("AH:AH").ColumnWidth = 20
'Supporti e resistenze con decimali due cifre
Range("AB3:AB12").Select
Selection.NumberFormat = "0.00"
Range("I4:M1000").Select
Selection.NumberFormat = "0.00"
'Retta di regressione con decimali due cifre
Range("AD9:AD500").Select
Selection.NumberFormat = "0.00"
'RSI e MACD
Range("N9:Y500").Select
Selection.NumberFormat = "0.00"
'Grafico previsione
Range("BA2").Value = "Data"
Range("BB2").Value = "Previsione"
Range("BC2").Value = "R1"
Range("BD2").Value = "S1"
Range("BE2").Value = "R2"
Range("BF2").Value = "S2"
Range("BG2").Value = "R3"
Range("BH2").Value = "S3"
Range("BI2").Value = "Data"
Range("BJ2").Value = "Close"
End Sub
- Codice: Seleziona tutto
Sub Formule()
Worksheets("Dati").Activate
Range("A5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets("Formule").Activate
Range("A4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Worksheets("Dati").Activate
Range("B5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets("Formule").Activate
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Worksheets("Dati").Activate
Range("C5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets("Formule").Activate
Range("C4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Worksheets("Dati").Activate
Range("D5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets("Formule").Activate
Range("D4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Worksheets("Dati").Activate
Range("E5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets("Formule").Activate
Range("E4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Worksheets("Dati").Activate
Range("F5").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets("Formule").Activate
Range("F4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Dim lastrow As Long
'Data numero per regressione e previsione
Worksheets("Formule").Range("G4").FormulaLocal = "=1"
Worksheets("Formule").Range("G5").FormulaLocal = "=G4+1"
Range("G5").Copy
Range("G6").Select
ActiveSheet.Paste
Range("G6:G500").FillDown
'Supporti e resistenze
' Mi posiziono alla cella AB3
Range("E4").Select
' Individuo l'ultima riga che contiene dati della colonna E
lastrow = Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Row
' Aggiungo la formula che fa max da E4 a E<ultimarigapiena>
'Worksheets("Foglio1").Range("E" & lastrow).FormulaLocal = "=MAX(E4:E" & lastrow - 1 & ")"
Worksheets("Formule").Range("AB3").FormulaLocal = "=MAX(E4:E" & lastrow - 1 & ")"
' Mi posiziono alla cella AB4
Range("E4").Select
' Individuo l'ultima riga che contiene dati della colonna E
lastrow = Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Row
' Aggiungo la formula che fa min da E4 a E<ultimarigapiena>
'Worksheets("Foglio1").Range("E" & lastrow).FormulaLocal = "=MAX(E4:E" & lastrow - 1 & ")"
Worksheets("Formule").Range("AB4").FormulaLocal = "=MIN(E4:E" & lastrow - 1 & ")"
' Mi posiziono alla cella AB5
Range("E4").Select
' Individuo l'ultima riga che contiene dati della colonna E
lastrow = Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Row
' Si assegna valore ultimo giorno periodo considrato
Worksheets("Formule").Range("AB5").Value = "=Value(E" & lastrow - 1 & ")"
' Mi posiziono alla cella AB6
'Set Range1 = Range("AB3:AB5")
'Totale = Application.WorksheetFunction.Sum(Range1) / 3
'Worksheets("Foglio1").Range("AB6").Value = Totale
Worksheets("Formule").Range("AB6").Value = "=AVERAGE(AB3:AB5)"
'Calcola e incolla formula come valore nella cella AB7
'Worksheets("Foglio1").Range("AB7").FormulaLocal = 2 * Range("AB6") - Range("AB4")
'Calcolo Resistenze e Supporti
'Calcola e inserisce formula nella cella AB7
AB6 = Range("AB6").Value
AB4 = Range("AB4").Value
Worksheets("Formule").Range("AB7").FormulaLocal = "=(2*AB6)-AB4"
AB6 = Range("AB6").Value
AB3 = Range("AB3").Value
Worksheets("Formule").Range("AB8").FormulaLocal = "=(2*AB6)-AB3"
Worksheets("Formule").Range("AB9").FormulaLocal = "=AB6+(AB3-AB4)"
Worksheets("Formule").Range("AB10").FormulaLocal = "=AB6-(AB3-AB4)"
Worksheets("Formule").Range("AB11").FormulaLocal = "=AB3+(2*(AB6-AB4))"
Worksheets("Formule").Range("AB12").FormulaLocal = "=AB4-2*(AB3-AB6)"
Worksheets("Formule").Range("H4").Value = Worksheets("Formule").Range("AB7").Value
'Mi posiziono alla cella H4
Range("H4").Select
lastrow = Cells(Rows.Count, 28).End(xlUp).Offset(1, 0).Row
Worksheets("Formule").Range("H4").Value = "=(AB" & lastrow - 6 & ")"
ActiveCell.FormulaR1C1 = "=(R7C28)"
Range("H4").Copy
Range("H5").Select
ActiveSheet.Paste
Range("H5:H500").FillDown
'Mi posiziono alla cella I4
Range("I4").Select
lastrow = Cells(Rows.Count, 28).End(xlUp).Offset(1, 0).Row
Worksheets("Formule").Range("I4").Value = "=(AB" & lastrow - 5 & ")"
ActiveCell.FormulaR1C1 = "=(R8C28)"
Range("I4").Copy
Range("I5").Select
ActiveSheet.Paste
Range("I5:I500").FillDown
'Mi posiziono alla cella J4
Range("J4").Select
lastrow = Cells(Rows.Count, 28).End(xlUp).Offset(1, 0).Row
Worksheets("Formule").Range("J4").Value = "=(AB" & lastrow - 4 & ")"
ActiveCell.FormulaR1C1 = "=(R9C28)"
Range("J4").Copy
Range("J5").Select
ActiveSheet.Paste
Range("J5:J500").FillDown
'Mi posiziono alla cella K4
Range("K4").Select
lastrow = Cells(Rows.Count, 28).End(xlUp).Offset(1, 0).Row
Worksheets("Formule").Range("K4").Value = "=(AB" & lastrow - 3 & ")"
ActiveCell.FormulaR1C1 = "=(R10C28)"
Range("K4").Copy
Range("K5").Select
ActiveSheet.Paste
Range("K5:K500").FillDown
'Mi posiziono alla cella L4
Range("L4").Select
lastrow = Cells(Rows.Count, 28).End(xlUp).Offset(1, 0).Row
Worksheets("Formule").Range("L4").Value = "=(AB" & lastrow - 2 & ")"
ActiveCell.FormulaR1C1 = "=(R11C28)"
Range("L4").Copy
Range("L5").Select
ActiveSheet.Paste
Range("L5:L500").FillDown
'Mi posiziono alla cella M4
Range("M4").Select
lastrow = Cells(Rows.Count, 28).End(xlUp).Offset(1, 0).Row
Worksheets("Formule").Range("M4").Value = "=(AB" & lastrow - 1 & ")"
ActiveCell.FormulaR1C1 = "=(R12C28)"
Range("M4").Copy
Range("M5").Select
ActiveSheet.Paste
Range("M5:M500").FillDown
'Relative Strenght Indicator
'Differenze giornaliere
Worksheets("Formule").Range("N5").FormulaLocal = "=E5-E4"
Range("N5").Copy
Range("N6").Select
ActiveSheet.Paste
Range("N6:N500").FillDown
'Rialzi giornalieri
Worksheets("Formule").Range("O5").FormulaLocal = "=SE(N5>=0;N5;0)"
Range("O5").Copy
Range("O6").Select
ActiveSheet.Paste
Range("O6:O500").FillDown
'Ribassi giornalieri
Worksheets("Formule").Range("P5").FormulaLocal = "=SE(N5<0;ASS(N5);0)"
Range("P5").Copy
Range("P6").Select
ActiveSheet.Paste
Range("P6:P500").FillDown
'SMA 14 Rialzi
Worksheets("Formule").Range("Q18").FormulaLocal = "=MEDIA(O5:O18)"
Range("Q18").Copy
Range("Q19").Select
ActiveSheet.Paste
Range("Q19:Q500").FillDown
'SMA 14 Ribassi
Worksheets("Formule").Range("R18").FormulaLocal = "=MEDIA(P5:P18)"
Range("R18").Copy
Range("R19").Select
ActiveSheet.Paste
Range("R19:R500").FillDown
'RS
Worksheets("Formule").Range("S18").FormulaLocal = "=SE(R18=0;0;Q18/R18)"
Range("S18").Copy
Range("S19").Select
ActiveSheet.Paste
Range("S19:S500").FillDown
'RSI-Relative Strenght Index
Worksheets("Formule").Range("T18").FormulaLocal = "=100-(100/(1+S18))"
Range("T18").Copy
Range("T19").Select
ActiveSheet.Paste
Range("T19:T500").FillDown
'MACD
'EMA12
Worksheets("Formule").Range("U15").FormulaLocal = "=MEDIA(E4:E15)"
Range("U15").Copy
Range("U16").Select
ActiveSheet.Paste
Range("U16:U500").FillDown
'EMA26
Worksheets("Formule").Range("V29").FormulaLocal = "=MEDIA(E4:E29)"
Range("V29").Copy
Range("V30").Select
ActiveSheet.Paste
Range("V30:V500").FillDown
'MACD
Worksheets("Formule").Range("W29").FormulaLocal = "=(+U29-V29)"
Range("W29").Copy
Range("W30").Select
ActiveSheet.Paste
Range("W30:W500").FillDown
'Signal
Worksheets("Formule").Range("X37").FormulaLocal = "=MEDIA(W29:W37)"
Range("X37").Copy
Range("X38").Select
ActiveSheet.Paste
Range("X38:X500").FillDown
'Istogramma
Worksheets("Formule").Range("Y37").FormulaLocal = "=(+W37-X37)"
Range("Y37").Copy
Range("Y38").Select
ActiveSheet.Paste
Range("Y38:Y500").FillDown
'Retta di regressione
'Pendenza e intercetta
Range("AE6").Select
ActiveCell.FormulaR1C1 = _
"=SLOPE(R[-2]C[-26]:R[252]C[-26],R[-2]C[-24]:R[252]C[-24])"
Range("AF6").Select
ActiveCell.FormulaR1C1 = _
"=INTERCEPT(R[-2]C[-27]:R[252]C[-27],R[-2]C[-25]:R[252]C[-25])"
'Regressione funzione regr.lin
Range("AE5:AF5").Select
Selection.FormulaArray = _
"=LINEST(R[-1]C[-26]:R[253]C[-26],R[-1]C[-24]:R[253]C[-24],TRUE,FALSE)"
'Valori retta di regressione su quotazione chiusura Y e data numero X
Range("AH4").Select
ActiveCell.FormulaR1C1 = "=R5C31*RC[-27]+R5C32"
Range("AH4").Copy
Range("AH5").Select
ActiveSheet.Paste
Range("AH5:AH500").FillDown
'Media mobile 20 giorni per calcolo valori previsione
Worksheets("Formule").Range("AG22").FormulaLocal = "=MEDIA(AH3:AH22)"
Range("AG22").Copy
Range("AG23").Select
ActiveSheet.Paste
Range("AG23:AG500").FillDown
End Sub
- Codice: Seleziona tutto
Sub Cancella_righe_vuote_dopo_copia_incolla_formule()
'Cancella righe vuote dopo copia-incolla formule
Dim i As Long, lastrow As Long
lastrow = Range("H" & Rows.Count).End(xlUp).Row
Sheets("Formule").Select
For i = lastrow To 1 Step -1
If LCase(Cells(i, "A").Value) Like "" And _
UCase(Cells(i, "B").Value) Like "" And _
UCase(Cells(i, "C").Value) Like "" And _
UCase(Cells(i, "D").Value) Like "" And _
UCase(Cells(i, "E").Value) Like "" And _
UCase(Cells(i, "F").Value) Like "" Then _
Rows(i).EntireRow.Delete
Next i
End Sub
- Codice: Seleziona tutto
Sub Grafici()
Sheets("Formule").Select
'Cancella grafici sul foglio Formule
xGrafico = Sheets("Formule").ChartObjects.Count
If xGrafico > 0 Then
Sheets("Formule").ChartObjects.Delete
End If
'Seleziona dati per grafico chiusura e regressione
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AN3").Select
ActiveSheet.Paste
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Range("AO3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("AH3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Range("AP3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Nasconde dati
Range("AN3:AP3").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
'Crea grafico Chiusura e regressione
Worksheets("Formule").Activate
Range("AN3:AP258").Select
'ActiveSheet.Shapes.AddChart.Select
ActiveSheet.Shapes.AddChart(xlLine, width:=1000, Height:=400).Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("Formule!$AN$3:$AP$256")
'Formatta grafico
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MinimumScale = 2
ActiveChart.Axes(xlValue).MaximumScale = 6
ActiveChart.Axes(xlValue).MaximumScale = 5.5
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add
ActiveChart.SeriesCollection(1).Trendlines(1).Select
With Selection
.Type = xlPolynomial
.Order = 2
End With
With Selection
.Type = xlPolynomial
.Order = 3
End With
With Selection
.Type = xlPolynomial
.Order = 4
End With
Selection.DisplayEquation = True
Selection.DisplayRSquared = True
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 51.236
Selection.Top = 108.102
ActiveChart.ChartArea.Select
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Chiusura e retta regressione" & " " & Sheets("Dati").Range("A1").Value
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"Chiusura e retta regressione" & " " & Sheets("Dati").Range("A1").Value
With Selection.Format.TextFrame2.TextRange.Characters(1, 28).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 18
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
With Selection.Format.TextFrame2.TextRange.Characters(9, 20).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 18
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 58
Selection.Top = 106.975
'Seleziona dati per grafico chiusura , supporti e resistenze
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AQ3").Select
ActiveSheet.Paste
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
Range("AR3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("H3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AS3").Select
ActiveSheet.Paste
Range("I3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AT3").Select
ActiveSheet.Paste
Range("J3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AU3").Select
ActiveSheet.Paste
Range("K3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AV3").Select
ActiveSheet.Paste
Range("L3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AW3").Select
ActiveSheet.Paste
Range("M3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AX3").Select
ActiveSheet.Paste
'Crea grafico chiusura , supporti e resistenze
Worksheets("Formule").Activate
Range("AQ3:AR258").Select
'ActiveSheet.Shapes.AddChart.Select
ActiveSheet.Shapes.AddChart(xlLine, width:=1000, Height:=400).Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("Formule!$AQ$3:$AX$256")
'Inserisce titolo
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Chiusura, supporti e resistenze" & " " & Sheets("Dati").Range("A1").Value
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"Chiusura, supporti e resistenze" & " " & Sheets("Dati").Range("A1").Value
With Selection.Format.TextFrame2.TextRange.Characters(1, 31).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 31).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 18
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add
ActiveChart.SeriesCollection(1).Trendlines(1).Select
With Selection
.Type = xlPolynomial
.Order = 2
End With
With Selection
.Type = xlPolynomial
.Order = 3
End With
With Selection
.Type = xlPolynomial
.Order = 4
End With
Selection.DisplayEquation = True
Selection.DisplayRSquared = True
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 51.236
Selection.Top = 108.102
'Nasconde dati
Range("AQ3:AX3").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
'Seleziona dati per grafico RSI-Relative Strenght Index
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AY3").Select
ActiveSheet.Paste
Range("T3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AZ3").Select
ActiveSheet.Paste
Range("T18").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AZ18").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Crea grafico RSI-Relative Strenght Index
Worksheets("Formule").Activate
Range("AY3:AZ258").Select
'ActiveSheet.Shapes.AddChart.Select
ActiveSheet.Shapes.AddChart(xlLine, width:=1000, Height:=400).Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("Formule!$AY$3:$AZ$256")
'Formatta grafico
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add
ActiveChart.SeriesCollection(1).Trendlines(1).Select
With Selection
.Type = xlPolynomial
.Order = 2
End With
With Selection
.Type = xlPolynomial
.Order = 3
End With
With Selection
.Type = xlPolynomial
.Order = 4
End With
Selection.DisplayEquation = True
Selection.DisplayRSquared = True
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 51.236
Selection.Top = 108.102
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 10
ActiveChart.Axes(xlValue).MinimumScale = 10
ActiveChart.Axes(xlValue).MaximumScale = 15
ActiveChart.Axes(xlValue).MaximumScale = 90
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "RSI-Relative Strenght Index sulla chusura" & " " & Sheets("Dati").Range("A1").Value
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"RSI-Relative Strenght Index" & " " & Sheets("Dati").Range("A1").Value
'Nasconde dati
Range("AY3:AZ258").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
'Seleziona dati per grafico MACD
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AJ3").Select
ActiveSheet.Paste
Range("W3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AK3").Select
ActiveSheet.Paste
Range("W28").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AK28").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("X3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AL3").Select
ActiveSheet.Paste
Range("X36").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AL37").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("Y3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AM3").Select
ActiveSheet.Paste
Range("Y37").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("AM37").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Crea grafico MACD
Worksheets("Formule").Activate
Range("AJ3:AM258").Select
'ActiveSheet.Shapes.AddChart.Select
ActiveSheet.Shapes.AddChart(xlLine, width:=1000, Height:=400).Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("Formule!$AJ$3:$AM$256")
ActiveChart.SeriesCollection(3).Select
ActiveChart.SeriesCollection(3).ChartType = xlColumnClustered
'Formatta grafico
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 0.1
ActiveChart.Axes(xlValue).MinimumScale = 0.1
ActiveChart.Axes(xlValue).MaximumScale = 0.1
ActiveChart.Axes(xlValue).MaximumScale = 0.2
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "MACD" & " " & Sheets("Dati").Range("A1").Value
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"MACD" & " " & Sheets("Dati").Range("A1").Value
'Nasconde dati
Range("AK2:AM258").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
'Seleziona dati per grafico Dettaglio Previsione
Sheets("Formule").Select
Range("A217:A247").Select
Selection.Copy
Range("BI3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Formule").Select
Range("E217:E247").Select
Selection.Copy
Range("BJ3").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'Nasconde dati
Range("BI2:BJ33").Select
Range(Selection, Selection.End(xlDown)).Select
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
'Crea grafico Dettaglio Previsione
Worksheets("Formule").Activate
Range("BI3:BJ32").Select
'ActiveSheet.Shapes.AddChart.Select
ActiveSheet.Shapes.AddChart(xlLine, width:=1000, Height:=400).Select
ActiveChart.ChartType = xlLine
ActiveChart.SetSourceData Source:=Range("Formule!BI3:BJ39")
'Formatta grafico
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 4.9
ActiveChart.Axes(xlValue).MinimumScale = 4.9
ActiveChart.Axes(xlValue).MaximumScale = 4.9
ActiveChart.Axes(xlValue).MaximumScale = 5.3
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add
ActiveChart.SeriesCollection(1).Trendlines(1).Select
With Selection
.Type = xlPolynomial
.Order = 2
End With
With Selection
.Type = xlPolynomial
.Order = 3
End With
With Selection
.Type = xlPolynomial
.Order = 4
End With
Selection.DisplayEquation = True
Selection.DisplayRSquared = True
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 51.236
Selection.Top = 108.102
ActiveChart.ChartArea.Select
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Dettaglio Previsione" & " " & Sheets("Dati").Range("A1").Value
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"Dettaglio Previsione" & " " & Sheets("Dati").Range("A1").Value
With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 18
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 58
Selection.Top = 106.975
End Sub
- Codice: Seleziona tutto
Option Explicit
Public Sub Grafico_chiusura_Regressione_Foglio()
On Error GoTo Elabora_Grafico
Application.DisplayAlerts = False
Sheets("Chiusura e retta di regressione").Select
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Elabora_Grafico:
Dim aChart As Chart
Set aChart = Charts.Add
With aChart
.Name = "Chiusura e retta di regressione"
.ChartType = xlLine
.SetSourceData Source:=Sheets("Formule").Range("AN3:AP256")
.HasTitle = True
'.ChartTitle.Text = "=Sheet1!R3C1"
End With
ActiveChart.Axes(xlValue).Select
ActiveChart.Axes(xlValue).MinimumScale = 0
ActiveChart.Axes(xlValue).MinimumScale = 2
ActiveChart.Axes(xlValue).MaximumScale = 6
ActiveChart.Axes(xlValue).MaximumScale = 5.5
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add
ActiveChart.SeriesCollection(1).Trendlines(1).Select
With Selection
.Type = xlPolynomial
.Order = 2
End With
With Selection
.Type = xlPolynomial
.Order = 3
End With
With Selection
.Type = xlPolynomial
.Order = 4
End With
Selection.DisplayEquation = True
Selection.DisplayRSquared = True
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 51.236
Selection.Top = 108.102
ActiveChart.ChartArea.Select
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Text = "Chiusura e retta regressione" & " " & Sheets("Dati").Range("A1").Value
Selection.Format.TextFrame2.TextRange.Characters.Text = _
"Chiusura e retta regressione" & " " & Sheets("Dati").Range("A1").Value
With Selection.Format.TextFrame2.TextRange.Characters(1, 28).ParagraphFormat
.TextDirection = msoTextDirectionLeftToRight
.Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 18
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
With Selection.Format.TextFrame2.TextRange.Characters(9, 20).Font
.BaselineOffset = 0
.Bold = msoTrue
.NameComplexScript = "+mn-cs"
.NameFarEast = "+mn-ea"
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 18
.Italic = msoFalse
.Kerning = 12
.Name = "+mn-lt"
.UnderlineStyle = msoNoUnderline
.Strike = msoNoStrike
End With
ActiveChart.ChartArea.Select
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 58
Selection.Top = 106.975
End Sub