PDA

View Full Version : [VBA] Domanda da niubbo su macro


Salvatopo
31-07-2008, 17:52
Ciao a tutti.
Premetto che è la prima volta in assoluto che utilizzo il VBA
Detto questo: mi trovavo nella necessità di dover, in automatico, eliminare le etichette a valore 0 (zero) in una serie di fogli di excel con dei grafici a torta e mi sono imbattuto, sul sito microsoft (http://support.microsoft.com/kb/142132/it), in questa macro:

Sub Auto_Open()
' Change the "Sheet1" to the sheet with your data on it.
Worksheets("Sheet1").OnCalculate = "ClearLabels"
End Sub

Sub Auto_Close()
Worksheets("Sheet1").OnCalculate = ""
End Sub

Sub ClearLabels()
' Change the "Sheet1" to the Sheet with your chart on it and
' "Chart 1" to the name of your chart.
Worksheets("Sheet1").ChartObjects("Chart 1").Chart.ApplyDataLabels _
Type:=xlShowPercent
' Note: In preceding statement, use Type:=xlShowPercent for charts
' with just percents, if there are none. Use
' Type:=xlShowLabelAndPercent instead for charts with both, Labels
' and Percents, if there are none.
For Each X In Worksheets("Sheet1").ChartObjects("Chart 1"). _
Chart.SeriesCollection(1).Points
' Use the following two lines for charts with just percents.
If InStr(X.DataLabel.Text, "0%") > 0 _
And Len(X.DataLabel.Text) = 2 Then
' Use the following line instead for charts with labels and
' percents.
' If InStr(x.DataLabel.Text, Chr(10) & "0%") > 0 Then
X.DataLabel.Delete
End If
Next
End Sub


Ora: ogni file contiene 3 fogli, chiamati rispettivamente "Torte IF", "Torte IG" e "Torte IDM" i quali a loro volta contengono ognuno 6 grafici, chiamati "grafico 1", "grafico 2", ecc...
Il mio problema è: come faccio a
1) Modificare la macro affinchè processi i 3 fogli e i 18 grafici in automatico
2) Processare tutti i 47 files che contengono le stesse torte, ma con dati diversi?

P.S.
Ovviamente se sostituisco a mano "Sheet 1" con, ad es. "Torte IG" e "Chart 1" con "Grafico 1" tutto funziona, ma il problema è, appunto, farlo fare in automatico, magari mettendo un tastino sulla barra di excel.

john_revelator
31-07-2008, 23:25
Prova un codice di questo genere:


Sub grafici()
For Each foglio In Application.Worksheets
foglio.Activate
For Each grafico In ActiveSheet.ChartObjects
ActiveSheet.ChartObjects(grafico.Name).Activate
ActiveChart.SeriesCollection(1).DataLabels.Select
Selection.NumberFormat = " 0.0%;;"
Next grafico
Next foglio
End Sub

Salvatopo
01-08-2008, 09:19
Prova un codice di questo genere:


Sub grafici()
For Each foglio In Application.Worksheets
foglio.Activate
For Each grafico In ActiveSheet.ChartObjects
ActiveSheet.ChartObjects(grafico.Name).Activate
ActiveChart.SeriesCollection(1).DataLabels.Select
Selection.NumberFormat = " 0.0%;;"
Next grafico
Next foglio
End Sub

Grazie: l'ho fatto... cancella gli zeri, ma mi trasforma i dati in percentuali assurde.

Credo che l'errore sia in
Selection.NumberFormat = " 0.0%;;"

Salvatopo
01-08-2008, 09:28
Dimenticavo, i valori delle etichette sono dei valori adimensionali, non percentuali.

john_revelator
01-08-2008, 10:16
Se i valori sono assoluti e non percentuali sostituisci come hai giustamente intuito

Selection.NumberFormat = " 0.0%;;"

con

Selection.NumberFormat = "*??"

:)

Salvatopo
25-08-2008, 13:48
Ora provo. :)

Salvatopo
25-08-2008, 13:50
Non va. :(
Perchè, penso, ho spesso valori tipo 0.65 o 0.04 e simili...