
Option Explicit
Sub barre_dati()
Dim riga As Long, campo1 As Range, campo2 As Range, soprazero As Double
Dim i As Long, numero As Double, dati As Range, minimo As Double
riga = Range("A1").End(xlDown).Row
soprazero = Application.WorksheetFunction.CountIf(Range("A1:A" & riga), ">0")
Set dati = Range("A1:A" & riga)
Set campo1 = Range("A1:A" & soprazero)
Set campo2 = Range("A" & soprazero + 1 & ":A" & riga)
minimo = Application.WorksheetFunction.Small(dati, 1)
ActiveWorkbook.Worksheets(1).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(1).Sort.SortFields.Add Key:=Range("A1") _
, Order:=xlDescending
With ActiveWorkbook.Worksheets(1).Sort
.SetRange Range("A1:A" & riga)
.Header = xlNo
.MatchCase = False
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets(1).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(1).Sort.SortFields.Add Key:=Range("A1") _
, Order:=xlAscending
With ActiveWorkbook.Worksheets(1).Sort
.SetRange Range("A1:A" & soprazero)
.Header = xlNo
.MatchCase = False
.SortMethod = xlPinYin
.Apply
End With
campo1.FormatConditions.AddDatabar
campo1.FormatConditions(campo1.FormatConditions.Count).ShowValue = True
campo1.FormatConditions(campo1.FormatConditions.Count).SetFirstPriority
With campo1.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueAutomaticMin
.MaxPoint.Modify newtype:=xlConditionValueAutomaticMax
End With
With campo1.FormatConditions(1).BarColor
.ColorIndex = 3
.TintAndShade = 0
End With
campo1.FormatConditions(1).BarFillType = xlDataBarFillSolid
campo2.FormatConditions.AddDatabar
campo2.FormatConditions(campo2.FormatConditions.Count).ShowValue = True
campo2.FormatConditions(campo2.FormatConditions.Count).SetFirstPriority
With campo2.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=minimo
End With
With campo2.FormatConditions(1).BarColor
.ColorIndex = 4
.TintAndShade = 0
End With
campo2.FormatConditions(1).BarFillType = xlDataBarFillSolid
Set dati = Nothing
Set campo1 = Nothing
Set campo2 = Nothing
End Sub
|
Option Explicit
Sub barre_dati_2()
Dim riga As Long, cella As Range
Dim dati As Range, minimo As Double, massimo As Double
riga = Range("A1").End(xlDown).Row
Set dati = Range("A1:A" & riga)
minimo = Application.WorksheetFunction.Small(dati, 1)
massimo = Application.WorksheetFunction.Large(dati, 1)
For Each cella In dati
cella.FormatConditions.AddDatabar
cella.FormatConditions(cella.FormatConditions.Count).ShowValue = True
cella.FormatConditions(cella.FormatConditions.Count).SetFirstPriority
If cella.Value > 0 Then
With cella.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=massimo
End With
With cella.FormatConditions(1).BarColor
.ColorIndex = 3
.TintAndShade = 0
End With
cella.FormatConditions(1).BarFillType = xlDataBarFillSolid
Else
With cella.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=minimo
End With
With cella.FormatConditions(1).BarColor
.ColorIndex = 4
.TintAndShade = 0
End With
cella.FormatConditions(1).BarFillType = xlDataBarFillSolid
End If
Next
Set dati = Nothing
End Sub
|
Sub RipristinaDue()
Dim pos(1 To 25) As Double, neg(1 To 25) As Double, cella As Range
Dim riga As Long, i As Long, a As Long, b As Long, elenco As Range
Dim valore As Double, minneg As Double, maxneg As Double, minpos As Double, maxpos As Double
riga = Range("A1").End(xlDown).Row
a = 1: b = 1
For i = 1 To riga
valore = Cells(i, 1).Value
If valore > 0 Then 'valori positivi
pos(a) = valore: a = a + 1
ElseIf valore <= 0 Then 'valor negativi
neg(b) = valore: b = b + 1
End If
Next i
minneg = Application.WorksheetFunction.Small(neg(), 1)
maxneg = Application.WorksheetFunction.Large(neg(), 1)
minpos = Application.WorksheetFunction.Small(pos(), 1)
maxpos = Application.WorksheetFunction.Large(pos(), 1)
Set elenco = Range("A1:A" & riga)
For Each cella In elenco
If cella.Value > 0 Then
cella.FormatConditions.AddDatabar
cella.FormatConditions(cella.FormatConditions.Count).ShowValue = True
cella.FormatConditions(cella.FormatConditions.Count).SetFirstPriority
With cella.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=minpos
.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=maxpos
End With
With cella.FormatConditions(1).BarColor
.ColorIndex = 4
.TintAndShade = 0
End With
ElseIf cella.Value <= 0 Then
cella.FormatConditions.AddDatabar
cella.FormatConditions(cella.FormatConditions.Count).ShowValue = True
cella.FormatConditions(cella.FormatConditions.Count).SetFirstPriority
With cella.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=maxneg
.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=minneg
End With
With cella.FormatConditions(1).BarColor
.ColorIndex = 3
.TintAndShade = 0
End With
End If
Next
Set elenco = Nothing
End Sub
|
Per l'evento Change del Foglio1:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim riga As Long, dati As Range
riga = Range("A1").End(xlDown).Row
Set dati = Range("A1:A" & riga)
If Range("Z1").Value = 1 Then Exit Sub
If Not Intersect(dati, Target) Is Nothing Then
Call barre_dati_2
End If
End Sub
Per la sfumatura inversa:
Option Explicit
Sub barre_dati_2()
Dim riga As Long, cella As Range
Dim dati As Range, minimo As Double, massimo As Double
Dim objColorStop As ColorStop
Dim colore2 As Double, colore3 As Double
riga = Range("A1").End(xlDown).Row
Set dati = Range("A1:A" & riga)
minimo = Application.WorksheetFunction.Small(dati, 1)
massimo = Application.WorksheetFunction.Large(dati, 1)
For Each cella In dati
If cella.Value > 0 Then
colore2 = cella.Value / massimo
colore3 = colore2 + 0.0001
cella.Interior.Pattern = xlPatternLinearGradient
cella.Interior.Gradient.Degree = 0
cella.Interior.Gradient.ColorStops.Clear
If colore2 = 1 Then
'creates a colorstop object with the position 0
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(0)
'changes its color to the first color
objColorStop.Color = vbWhite
'creates a colorstop object with the position 1
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(1)
'changes its color to the second color
objColorStop.Color = vbRed
Else
'creates a colorstop object with the position 0
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(0)
'changes its color to the first color
objColorStop.Color = vbWhite
'creates a colorstop object with the position 1
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(colore2)
'changes its color to the second color
objColorStop.Color = vbRed
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(colore3)
'changes its color to the second color
objColorStop.Color = vbWhite
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(1)
'changes its color to the second color
objColorStop.Color = vbWhite
End If
Else
colore2 = -cella.Value / -minimo
colore3 = colore2 + 0.0001
cella.Interior.Pattern = xlPatternLinearGradient
cella.Interior.Gradient.Degree = 0
cella.Interior.Gradient.ColorStops.Clear
If colore2 = 1 Then
'creates a colorstop object with the position 0
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(0)
'changes its color to the first color
objColorStop.Color = vbWhite
'creates a colorstop object with the position 1
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(1)
'changes its color to the second color
objColorStop.Color = vbGreen
Else
'creates a colorstop object with the position 0
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(0)
'changes its color to the first color
objColorStop.Color = vbWhite
'creates a colorstop object with the position 1
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(colore2)
'changes its color to the second color
objColorStop.Color = vbGreen
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(colore3)
'changes its color to the second color
objColorStop.Color = vbWhite
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(1)
'changes its color to the second color
objColorStop.Color = vbWhite
End If
End If
Next
Set dati = Nothing
Set objColorStop = Nothing
End Sub
|
Private Sub Worksheet_Change(ByVal Target As Range)
Dim riga As Long, dati As Range
riga = Range("C5").End(xlDown).Row
Set dati = Range("C5:C" & riga)
If Range("Z1").Value = 1 Then Exit Sub
If Not Intersect(dati, Target) Is Nothing Then
Call barredati
End If
Rem COPIA I RISULTATI DELLA COLONNA "K" NELLA COLONNA "L" ALLA MODIFICA DI UNA CELLA POSTA NELLE COLONNE "C" E "G" SOLO NELLA RIGA DOVE "C" E "G" SONO SCRITTE
Dim i As Long
Dim ur As Long
ur = Sheets("Foglio1").Range("K" & Rows.Count).End(xlUp).Row
If Not Intersect(Target, Range("c5:c" & ur, "g5:g" & ur)) Is Nothing Then
For i = 5 To ur
Range("L" & i).Value = Range("K" & i).Value
Next i
End If
End Sub
--------------------------------------------------
Sub barredati()
Rem VISUALIZZA LA FORMATTAZIONE CONDIZIONALE NELLA COLONNA "L" IN BASE AI VALORI SCRITTI (VERDE NEGATIVI, ROSSO POSITIVI) CON PARTENZA DA SINISTRA E LUNGHEZZA VARIABILE IN BASE ALLA GRANDEZZA DEL SOLO NUMERO (SENZA SEGNO -)
Dim riga As Long, cella As Range
Dim dati As Range, minimo As Double, massimo As Double
riga = Range("L5").End(xlDown).Row
Set dati = Range("L5:L" & riga)
minimo = Application.WorksheetFunction.Small(dati, 1)
massimo = Application.WorksheetFunction.Large(dati, 1)
For Each cella In dati
cella.FormatConditions.AddDatabar
cella.FormatConditions(cella.FormatConditions.Count).ShowValue = True
cella.FormatConditions(cella.FormatConditions.Count).SetFirstPriority
If cella.Value > 0 Then
With cella.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=massimo
End With
With cella.FormatConditions(1).BarColor
.ColorIndex = 3
.TintAndShade = 0
End With
cella.FormatConditions(1).BarFillType = xlDataBarFillSolid
Else
With cella.FormatConditions(1)
.MinPoint.Modify newtype:=xlConditionValueNumber, newvalue:=0
.MaxPoint.Modify newtype:=xlConditionValueNumber, newvalue:=minimo
End With
With cella.FormatConditions(1).BarColor
.ColorIndex = 4
.TintAndShade = 0
End With
cella.FormatConditions(1).BarFillType = xlDataBarFillSolid
End If
Next
Set dati = Nothing
End Sub |
'changes its color to the second color
objColorStop.Color = vbWhite
Set objColorStop = cella.Interior.Gradient.ColorStops.Add(1)
'changes its color to the second color
objColorStop.Color = vbWhite
End If
ElseIf cella.Value = 0 Then
cella.Interior.Color = vbWhite |
Sub cambio()
Dim campo As Range, cella As Range, negativo As Integer, positivo As Integer
Set campo = Range("A1:A23")
Range("Z1").Value = 1
campo.Clear
For Each cella In campo
negativo = Rnd() * (600)
positivo = Rnd() * (600)
cella.Value = positivo - negativo
Next
Range("Z1").Value = 0
Set campo = Nothing
End Sub |
Sub cambio()
Dim campo As Range, cella As Range, negativo As Integer, positivo As Integer
Set campo = Range("A1:A23")
Range("Z1").Value = 1
campo.Clear
For Each cella In campo
negativo = Rnd() * (600)
positivo = Rnd() * (600)
cella.Value = positivo - negativo
Next
Range("Z1").Value = 0
Set campo = Nothing
End Sub |
Private Sub Workbook_Open()
Rem SALVATAGGIO COPIA BACKUP SU CARTELLA SCRITTA SU NOMEDESTINAZIONE
Dim prova As Boolean
Dim DataIniziale As Date
Dim DataFinale As Date
prova = False
DataIniziale = Now
Do Until prova
DoEvents
DataFinale = Now
If Minute(DataFinale - DataIniziale) = 1 Then
DataIniziale = Now
Dim NomeOrigine As String
Dim NomeDestinazione As String
NomeOrigine = Left(ActiveWorkbook.Name, (Len(ActiveWorkbook.Name) - 4))
NomeDestinazione = "C:Backup da ExcelBackup file per cromagno" & "_" & Day(Date) & "-" & Format(Month(Date), "00") & "-" & Year(Date) & "_" & Format(Hour(Time), "00") & "." & Format(Minute(Time), "00") & ".xls"
ActiveWorkbook.SaveCopyAs NomeDestinazione
End If
Loop
End Sub |
Private Sub Workbook_Open() Call Macro1 Call Macro2 End Sub |
