
Sub prova_ordinamento2()
'il primo passo è copiare sul foglio2 il risultato della moltiplicazione delle colonne L e M del foglio1
Set sh1 = Workbooks("esempio.xlsm").Worksheets("foglio1")
Set sh2 = Workbooks("esempio.xlsm").Worksheets("foglio2")
Set SourceRange = sh2.Range("C3")
Sheets("Foglio2").Activate
sh2.Range("C3").Select
ActiveCell.FormulaR1C1 = "=Foglio1!R[6]C[9]*Foglio1!R[6]C[10]"
SourceRange.AutoFill Destination:=sh2.Range("C3:C5"), Type:=xlFillDefault
'il secondo passo è sistemare le date nella colonna H del foglio 1 e copiarle nel foglio 2, tramite ciclo for
Dim oneRange As Range
Dim aCell As Range
Set oneRange = Range("H9:H11")
Set aCell = Range("H9")
'seleziono la cella da cui deve iniziare a copiare il ciclo
sh2.Range("B3").Select
For Each aCell In oneRange
nuovadata = Evaluate("=SOSTITUISCI(TESTO(aCell;"gg/mm/aaaa");ANNO(aCell);(SE(MESE(aCell)>MESE(H3);2013;2014)))")
ActiveCell.Value = nuovadata
ActiveCell.Offset(1, 0).Select
Next
'il terzo passo è ordinare la tabella così creata
'lo farò dopo che ho tutti i dati corretti
End Sub
|
?activecell.Formula
nuovadata = Evaluate("=SUBSTITUTE(TEXT(" & aCell & Chr(34) & "," & Chr(34) & "gg/mm/aaaa" & Chr(34) & ");YEAR(" & aCell & ");(IF(MONTH(" & aCell & ")>MONTH(H3);2013;2014)))") |
aCell.address |
nuovadata = Evaluate("=SUBSTITUTE(TEXT(" & aCell.Address & "," & Chr(34) & "gg/mm/aaaa" & Chr(34) & "),YEAR(" & aCell.Address & "),(IF(MONTH(" & aCell.Address & ")>MONTH(H3),2013,2014)))") |
nuovadata = Evaluate("=SUBSTITUTE(TEXT(" & aCell.Address & "," & Chr(34) & "dd/mm/yyyy" & Chr(34) & "),YEAR(" & aCell.Address & "),(IF(MONTH(" & aCell.Address & ")>MONTH(H3),2013,2014)))") |
Sub prova_ordinamento2()
'il primo passo è copiare sul foglio2 il risultato della moltiplicazione delle colonne L e M del foglio1
Set sh1 = Workbooks("esempio.xlsm").Worksheets("foglio1")
Set sh2 = Workbooks("esempio.xlsm").Worksheets("foglio2")
Set SourceRange = sh2.Range("C3")
Sheets("Foglio2").Activate
sh2.Range("C3").Select
ActiveCell.FormulaR1C1 = "=Foglio1!R[6]C[9]*Foglio1!R[6]C[10]"
SourceRange.AutoFill Destination:=sh2.Range("C3:C5"), Type:=xlFillDefault
'il secondo passo è sistemare le date nella colonna H del foglio 1 e copiarle nel foglio 2, tramite ciclo for
Dim oneRange As Range
Dim aCell As Range
Set oneRange = sh1.Range("H9:H11")
Set aCell = sh1.Range("H9")
'seleziono la cella da cui deve iniziare a copiare il ciclo
sh2.Range("B3").Select
For Each aCell In oneRange
x = CDbl(aCell)
If Month(x) > Month(sh1.[H3].Value) Then
nuovadata = DateSerial(2013, Month(x), Day(x))
Else
nuovadata = DateSerial(2014, Month(x), Day(x))
End If
ActiveCell.Value = nuovadata
ActiveCell.Offset(1, 0).Select
Next
'il terzo passo è ordinare la tabella così creata
'lo farò dopo che ho tutti i dati corretti
UltimaRiga = Range("A65536").End(xlUp).Row
sh2.Sort.SortFields.Add Key:=Range("B3:B" & UltimaRiga) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End Sub |
nuovadata = Evaluate("=DATE(IF(MONTH(Foglio1!" & aCell.Address & ")>MONTH(Foglio1!" & aCell.Address & "),2013,2014 ),MONTH(Foglio1!" & aCell.Address & "),Day(Foglio1!" & aCell.Address & "))") |
nuovadata = Evaluate("=DATE(IF(MONTH(Foglio1!" & aCell.Address & ")>MONTH(Foglio1!H3),2013,2014 ),MONTH(Foglio1!" & aCell.Address & "),Day(Foglio1!" & aCell.Address & "))")
oppure
nuovadata = Evaluate("=SUBSTITUTE(TEXT(Foglio1!" & aCell.Address & "," & Chr(34) & "dd/mm/yyyy" & Chr(34) & "),YEAR(Foglio1!" & aCell.Address & "),(IF(MONTH(Foglio1!" & aCell.Address & ")>MONTH(Foglio1!H3),2013,2014)))") |
'il terzo passo è ordinare la tabella così creata
'lo farò dopo che ho tutti i dati corretti
UltimaRiga = Range("A65536").End(xlUp).Row
sh2.Sort.SortFields.Add Key:=Range("B3:B" & UltimaRiga) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers _
, Orientation:=xlTopToBottom 'SortMethod:=xlPinYin
|
nuovadata = Evaluate("=DATE(IF(MONTH(Foglio1!" & aCell.Address & ")>MONTH(Foglio1!H3),2013,2014 ),MONTH(Foglio1!" & aCell.Address & "),Day(Foglio1!" & aCell.Address & "))")
'MACRO registrata
Sub ordinamento_finale()
'
' ordinamento_finale Macro
'
'
Range("B3:C5").Select
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Foglio2").Sort.SortFields.Add Key:=Range("B3:B5") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Foglio2").Sort
.SetRange Range("B3:C5")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
|
anno = iif (month(acell) > month([h3]), 2013, 2014)
nuovadata = dateserial(anno, month(acell), day(acell))
ActiveWorkbook.Worksheets("Foglio2").[B3:B5].Sort Key1:=[B3], Order1:=xlAscending, Header:=xlGuess
Sub prova_ordinamento_multianno()
Set sh1 = Workbooks("copia di esempio.xlsm").Worksheets("foglio1")
Set sh2 = Workbooks("copia di esempio.xlsm").Worksheets("foglio2")
Set SourceRange = sh2.Range("C3")
Dim oneRange As Range
Dim aCell As Range
UltimaRigafoglio1 = sh1.Range("H65536").End(xlUp).Row
Set oneRange = sh1.Range("H9:H" & UltimaRigafoglio1)
Set aCell = sh1.Range("H9")
'seleziono la prima cella in cui deve copiare delle date
Mese = Month(sh1.Range("H3"))
Sheets("Foglio2").Activate
sh2.Range("B3").Select
For Each aCell In oneRange
'non funzionano
'nuovadata = Evaluate("=DATE(IF(MONTH(Foglio1!" & aCell.Address & ")>MONTH(Foglio1!H3),2013,2014 ),MONTH(Foglio1!" & aCell.Address & "),Day(Foglio1!" & aCell.Address & "))")
'nuovadata = Evaluate("=DATE(IF(MONTH(Foglio1!" & aCell.Address & ")>MONTH(Foglio1!H3),2013,2014 ),MONTH(Foglio1!" & aCell.Address & "),Day(Foglio1!" & aCell.Address & "))")
'non funziona neanche così
anno = IIf(Month(aCell) > Mese, 2013, 2014)
nuovadata = DateSerial(anno, Month(aCell), Day(aCell))
primo_flusso = Evaluate("=EDATE(nuovadata;-6)")
ActiveCell.Value = primo_flusso
ActiveCell.Offset(1, 0).Select
Next
End Sub |
nuovo_mese = Month(aCell) - 6 primo_flusso = DateSerial(anno, nuovo_mese, Day(aCell)) |
'1)
oggi = Evaluate("=today(sh1.Range(""H3""))")
Range("N10").Value = oggi
'restituisce il solito #valore
'2)
g = Day(sh1.Range("H3"))
m = Month(sh1.Range("H3"))
a = Year(sh1.Range("H3"))
oggi = DateSerial(a, m, g)
Range("N10").Value = oggi
'funziona.
'3)
oggi = Today(sh1.Range("H3"))
Range("N10").Value = oggi
'da errore-->sub o function non definita!
|
In VBA: oggi = [NOW] restituisce "28/11/2013 21:20:28" oggi = [NOW()] restituisce " 41606,8895671296" oggi = [TODAY] restituisce "Errore 2029" oggi = [TODAY()] restituisce "41606" |
Sub macro()
Set sh1 = Workbooks("allegato_date.xlsm").Worksheets("foglio1")
Set sh2 = Workbooks("allegato_date.xlsm").Worksheets("foglio2")
Set SourceRange = sh2.Range("C3")
Dim oneRange As Range
Dim aCell As Range
'definisco il range delle date
UltimaRigafoglio1 = sh1.Range("H65536").End(xlUp).Row
Set oneRange = sh1.Range("H9:H" & UltimaRigafoglio1)
Set aCell = sh1.Range("H9")
Mese = Month(sh1.Range("H3"))
'seleziono la prima cella in cui deve copiare delle date
Sheets("Foglio2").Activate
sh2.Range("B3").Select
For Each aCell In oneRange
anno = IIf(Month(aCell) > Mese, 2013, 2014)
nuovadata = DateSerial(anno, Month(aCell), Day(aCell))
oggi = Date
nuovo_mese = Month(aCell) - 6
altro_flusso = DateSerial(anno, nuovo_mese, Day(aCell))
If (altro_flusso < oggi) Then
nuovo_mese = Month(aCell) + 6
altro_flusso = DateSerial(anno, nuovo_mese, Day(aCell))
End If
ActiveCell.Value = nuovadata
a = Selection(aCell).Cells(0, 3).Value
b = Selection(aCell).Cells(0, 4).Value
'mi sposto a destra
ActiveCell.Offset(0, 1).Select
'ora devo inserire il risultato del prodotto relativo alla data di interesse
ActiveCell.Formula = a * b
'mi sposto sotto e a sx
ActiveCell.Offset(1, -1).Select
'copio la data differita di 6 mesi
ActiveCell.Value = altro_flusso
ActiveCell.Offset(1, 0).Select
Next
End Sub
|
Option Explicit
Sub Calcola()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim oneRange As Range, aCell As Range
Dim A As Double, B As Double, UltimaRiga As Long, Riga As Long
Dim mese As Integer, nuovo_mese As Integer, anno As Integer, nuovadata As Date, altro_flusso As Date, oggi As Date
Set sh1 = Workbooks("allegato_date.xlsm").Worksheets("foglio1")
Set sh2 = Workbooks("allegato_date.xlsm").Worksheets("foglio2")
'UltimaRiga = sh2.Range("B" & Rows.Count).End(xlUp).Row
'sh2.Range("B3:C" & UltimaRiga).ClearContents
UltimaRiga = sh1.Range("H" & Rows.Count).End(xlUp).Row
Set oneRange = sh1.Range("H9:H" & UltimaRiga)
mese = Month(Date)
Riga = 3
For Each aCell In oneRange
anno = IIf(Month(aCell) > mese, 2013, 2014)
nuovadata = DateSerial(anno, Month(aCell), Day(aCell))
oggi = Date
nuovo_mese = Month(aCell) - 6
altro_flusso = DateSerial(anno, nuovo_mese, Day(aCell))
If (altro_flusso < oggi) Then
nuovo_mese = Month(aCell) + 6
altro_flusso = DateSerial(anno, nuovo_mese, Day(aCell))
End If
sh2.Cells(Riga, 2) = nuovadata
A = aCell.Offset(0, 4).Value
B = aCell.Offset(0, 5).Value
sh2.Cells(Riga, 3) = A * B
sh2.Cells(Riga + 1, 2) = altro_flusso
Riga = Riga + 2
Next
Set sh1 = Nothing
Set sh2 = Nothing
Set oneRange = Nothing
End Sub |
Sub Calcola()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim oneRange As Range, aCell As Range
Dim A As Double, B As Double, UltimaRiga As Long, Riga As Long
Dim mese As Integer, anno As Integer, nuovadata As Date, altro_flusso As Date, oggi As Date
Set sh1 = ThisWorkbook.Worksheets("foglio1")
Set sh2 = ThisWorkbook.Worksheets("foglio2")
'UltimaRiga = sh2.Range("B" & Rows.Count).End(xlUp).Row
'sh2.Range("B3:C" & UltimaRiga).ClearContents
UltimaRiga = sh1.Range("H" & Rows.Count).End(xlUp).Row
Set oneRange = sh1.Range("H9:H" & UltimaRiga)
mese = Month(Date)
Riga = 3
For Each aCell In oneRange
anno = IIf(Month(aCell) > mese, 2013, 2014)
nuovadata = DateSerial(anno, Month(aCell), Day(aCell))
oggi = Date
'--- modifica -----
altro_flusso = DateAdd("m", -6, nuovadata)
If (altro_flusso < oggi) Then altro_flusso = DateAdd("m", 6, nuovadata)
'--- fine modifica -----
sh2.Cells(Riga, 2) = nuovadata
A = aCell.Offset(0, 4).Value
B = aCell.Offset(0, 5).Value
sh2.Cells(Riga, 3) = A * B
sh2.Cells(Riga + 1, 2) = altro_flusso
Riga = Riga + 2
Next
Set sh1 = Nothing
Set sh2 = Nothing
Set oneRange = Nothing
End Sub |
