È soltanto un Pokémon con le armi o è un qualcosa di più? Vieni a parlarne su Award & Oscar!
 
Pagina precedente | 1 | Pagina successiva
Vota | Stampa | Notifica email    
Autore

modifica macro per calcolo date

Ultimo Aggiornamento: 03/09/2017 19:16
Post: 735
Registrato il: 28/12/2009
Città: CITTADELLA
Età: 62
Utente Senior
excel 2007/365
OFFLINE
01/09/2017 21:45

Ciao a tutti.
La macro allegata è in inglese e non so come cambiare in questo punto:

If Grammar = True Then

    'sGrammar(0) = "s" 'inglese
    sGrammar(0) = "i" 'taliano
    
End If


e poi qui:

'italiano
If ShowAll Or iYears > 0 Then
    sYears = iYears & " anno" & sGrammar((iYears = 1)) & ", "
End If
If ShowAll Or iYears > 0 Or iMonths > 0 Then
    sMonths = iMonths & " mese" & sGrammar((iMonths = 1)) & ", "
End If
sDays = iDays & " giorno" & sGrammar((iDays = 1))


perchè poi in B1 esca (esempio)

4 anni, 3 mesi, 20 giorni

e non:

4 annoi, 3 mesei, 20 giornoi

Max



____________________________
versione excel 365 ufficio
versione excel 2007 casa
Post: 3.939
Registrato il: 13/03/2012
Città: LIVORNO
Età: 78
Utente Master
2010
OFFLINE
02/09/2017 07:53

Re:
prova questa
Function YearsMonthsDays(Date1 As Date, Date2 As Date) As String
Dim dTempDate As Date
Dim iYears As Integer
Dim iMonths As Integer
Dim iDays As Integer
Dim sYears As String
Dim sMonths As String
Dim sDays As String
Dim sGrammar As String
If Date1 > Date2 Then
    dTempDate = Date1
    Date1 = Date2
    Date2 = dTempDate
End If

iYears = DateDiff("yyyy", Date1, Date2)
Date1 = DateAdd("yyyy", iYears, Date1)
If Date1 > Date2 Then
    iYears = iYears - 1
    Date1 = DateAdd("yyyy", -1, Date1)
End If

iMonths = DateDiff("M", Date1, Date2)
Date1 = DateAdd("M", iMonths, Date1)
If Date1 > Date2 Then
    iMonths = iMonths - 1
    Date1 = DateAdd("m", -1, Date1)
End If

iDays = DateDiff("d", Date1, Date2)

If iYears > 0 Then
    If iYears = 1 Then sGrammar = "o" Else sGrammar = "i" ' <<<<<<
    sYears = iYears & " ann" & sGrammar & ", "
End If
If iYears > 0 Or iMonths > 0 Then
    If iMonths = 1 Then sGrammar = "e" Else sGrammar = "i" ' <<<<<<
    sMonths = iMonths & " mes" & sGrammar & ", "
End If
sDays = iDays & " giorni" 
YearsMonthsDays = sYears & sMonths & sDays
End Function



[Modificato da patel45 02/09/2017 09:41]

----------
Win 10 - Excel 2010
allega un file di esempio, guadagnerai tempo tu e lo farai risparmiare a chi ti aiuta
Post: 735
Registrato il: 28/12/2009
Città: CITTADELLA
Età: 62
Utente Senior
excel 2007/365
OFFLINE
02/09/2017 08:57

Ciao patel,
grazie dell'aiuto.
La tua modifica non è tanto esatta, non esce il nome in plurale.
In più nel mio file allegato in post#1 nelle celle dove c'è la funzione:

=YearsMonthsDays(A1;A2;FALSO;VERO)
=YearsMonthsDays(A1;A2;VERO;FALSO)
=YearsMonthsDays(A1;A2;VERO;VERO)
=YearsMonthsDays(A1;A2;FALSO;FALSO)

si visualizza in modo diverso il formato delle date.
Con la tua macro ora in queste celle dà errore di #VALORE!.
max
____________________________
versione excel 365 ufficio
versione excel 2007 casa
Post: 3.940
Registrato il: 13/03/2012
Città: LIVORNO
Età: 78
Utente Master
2010
OFFLINE
02/09/2017 09:36

nell'allegato c'è una sola formula, io ho eliminato il vero e falso quindi le formule devono riportare solo le date
Ho fatto qualche correzione alla macro precedente
[Modificato da patel45 02/09/2017 09:45]

----------
Win 10 - Excel 2010
allega un file di esempio, guadagnerai tempo tu e lo farai risparmiare a chi ti aiuta
Post: 736
Registrato il: 28/12/2009
Città: CITTADELLA
Età: 62
Utente Senior
excel 2007/365
OFFLINE
02/09/2017 09:46

E' vero patel, nel post#1 ho allegato il file non esatto.
In questo c'è quello esatto.
max
____________________________
versione excel 365 ufficio
versione excel 2007 casa
Post: 563
Registrato il: 02/08/2015
Utente Senior
Excel 2013
OFFLINE
02/09/2017 09:59

Ciao a tutti,

@max
la sintassi in inglese ed in italiano è completamente diversa...
in inglese basta aggiungere una "s" per il plurale, mentre in italiano devi SOSTITUIRE l'ultima lettera.
Quindi l'argomento "grammar" della funzione (così come la variabile "sGrammar") perde la sua utilità.

Potresti usare questa:
Function YearsMonthsDays(Date1 As Date, _
    Date2 As Date, Optional ShowAll As Boolean = False, _
    Optional MinusText As String = "Minus ") As String
    
    
Dim dTempDate As Date
Dim iYears As Integer
Dim iMonths As Integer
Dim iDays As Integer
Dim sYears As String
Dim sMonths As String
Dim sDays As String
Dim sGrammar(-1 To 0) As String
Dim sMinusText As String

If Date1 > Date2 Then
    dTempDate = Date1
    Date1 = Date2
    Date2 = dTempDate
    sMinusText = MinusText
End If

iYears = DateDiff("yyyy", Date1, Date2)
Date1 = DateAdd("yyyy", iYears, Date1)
If Date1 > Date2 Then
    iYears = iYears - 1
    Date1 = DateAdd("yyyy", -1, Date1)
End If

iMonths = DateDiff("M", Date1, Date2)
Date1 = DateAdd("M", iMonths, Date1)
If Date1 > Date2 Then
    iMonths = iMonths - 1
    Date1 = DateAdd("m", -1, Date1)
End If

iDays = DateDiff("d", Date1, Date2)


If iYears > 0 Then
    If iYears = 1 Then
        sYears = iYears & "anno" & ", "
    Else
        sYears = iYears & " anni" & ", "
    End If
End If
If iMonths > 0 Then
    If iMonths = 1 Then
        sMonths = iMonths & " mese" & ", "
    Else
        sMonths = iMonths & " mesi" & ", "
    End If
End If
If iDays = 1 Then
    sDays = iDays & " giorno"
Else
    sDays = iDays & " giorni"
End If

YearsMonthsDays = sMinusText & sYears & sMonths & sDays

End Function


Ciao
Tore
[Modificato da cromagno 02/09/2017 10:00]


"Sono le persone che nessuno immagina che possano fare certe cose, quelle che fanno cose che nessuno può immaginare."
Post: 530
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
OFFLINE
02/09/2017 16:15

Un saluto a tutti.

Volevo segnalare a maxma62 che già nella Function originale c'è un errore di base: es. utilizzando come data iniziale 31/01/2017 e finale 01/02/2017 vengono segnalati ben 4 giorni di differenza mentre con altri mesi da 31 giorni la differenza con il primo del mese successivo indica 2 giorni. Non ho fatto prove con differenze negative tipo inizio 01/01/2017 fine 29/02/2016.

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 737
Registrato il: 28/12/2009
Città: CITTADELLA
Età: 62
Utente Senior
excel 2007/365
OFFLINE
02/09/2017 16:42

Ah non ero accorto [SM=g27826]
Vediamo di trovare un'altra soluzione.
Grazie rollis della segnalazione.
max
____________________________
versione excel 365 ufficio
versione excel 2007 casa
Post: 1.585
Registrato il: 06/04/2013
Utente Veteran
2010
OFFLINE
02/09/2017 16:54

eh..eh
andrebbe detto a quelli di stackoverflow.com che hanno proposto la soluzione.
(Comunque il problema di quella udf è legato al mese successivo che se è di 31 calcola correttamente, altrimenti....

cari saluti


Domenico
Win 10 - Excel 2016
Post: 1.586
Registrato il: 06/04/2013
Utente Veteran
2010
OFFLINE
02/09/2017 17:20

ciao
proposta:
Public Function GMA(Date1 As Date, Date2 As Date) As String
Dim intYears As Integer, intMonths As Integer, intDays As Integer
Dim iG As String, iM As String, iA As String
  intMonths = DateDiff("m", Date1, Date2)
  intDays = DateDiff("d", DateAdd("m", intMonths, Date1), Date2)
  If intDays < 0 Then
    intMonths = intMonths - 1
    intDays = DateDiff("d", DateAdd("m", intMonths, Date1), Date2)
  End If
  intYears = intMonths \ 12
  intMonths = intMonths Mod 12
  If intDays > 1 Or intDays = 0 Then iG = "giorni" Else iG = "giorno"
  If intMonths > 1 Or intMonths = 0 Then iM = "mesi" Else iM = "mese"
  If intYears > 1 Or intYears = 0 Then iA = "anni" Else iA = "anno"
  GMA = intYears & " " & iA & " " & intMonths & " " & iM & " " & intDays & " " & iG
End Function


Saluti

(nota: non è prevista la scritta corretta giorno/i mese/i anno/i per differenze negative in quanto nella tua convalida stabilisci che a1 deve essere minore di a2)


[Modificato da dodo47 02/09/2017 17:24]
Domenico
Win 10 - Excel 2016
Post: 564
Registrato il: 02/08/2015
Utente Senior
Excel 2013
OFFLINE
02/09/2017 17:22

Re:
maxma62, 02/09/2017 16.42:

Ah non ero accorto [SM=g27826]
Vediamo di trovare un'altra soluzione.
Grazie rollis della segnalazione.
max



Ciao a tutti,

@max
Non ho ancora capito se hai visto quello che ti ho proposto al post #6
[SM=g27833] [SM=g27833] [SM=g27833] [SM=g27833] [SM=g27833]


"Sono le persone che nessuno immagina che possano fare certe cose, quelle che fanno cose che nessuno può immaginare."
Post: 1.587
Registrato il: 06/04/2013
Utente Veteran
2010
OFFLINE
02/09/2017 17:29

Tore
sbaglio o anche la tua versione con date 31/01/2017 01/02/2017 restituisce 4 giorni? (il problema è sempre quello del mese successivo che se di 31 è ok, altrimenti...)

cari saluti


Domenico
Win 10 - Excel 2016
Post: 565
Registrato il: 02/08/2015
Utente Senior
Excel 2013
OFFLINE
02/09/2017 17:33

Re:
dodo47, 02/09/2017 17.29:

Tore
sbaglio o anche la tua versione con date 31/01/2017 01/02/2017 restituisce 4 giorni? (il problema è sempre quello del mese successivo che se di 31 è ok, altrimenti...)

cari saluti



Ciao Domenico,
sicuramente.
Quello proposto era solo la modifica per la "conversione" in italiano...i bug natii non li ho presi in considerazione.

Ciao
Tore



"Sono le persone che nessuno immagina che possano fare certe cose, quelle che fanno cose che nessuno può immaginare."
Post: 738
Registrato il: 28/12/2009
Città: CITTADELLA
Età: 62
Utente Senior
excel 2007/365
OFFLINE
02/09/2017 19:01

Ciao dodo47.
La tua macro mi sembra esatta.
Sto tentando di fare una piccola modifica alla tua macro, per visualizzare esempio se restano "2 mesi, 1 giorno" oppure "10 giorni"
aggiungendo una serie di else.
L'esempio per visualizzare solo "1 giorno" non risulta esatto.
Allego esempi.
max
____________________________
versione excel 365 ufficio
versione excel 2007 casa
Post: 739
Registrato il: 28/12/2009
Città: CITTADELLA
Età: 62
Utente Senior
excel 2007/365
OFFLINE
02/09/2017 22:48

Ciao,
ho risolto in parte aggiungendo una serie di if-elseif else end if.
Una data non riesco capire perché esce così:

data inizio 31/03/2017
data fine 01/04/2017
esce: 0 anni, 0 mesi, 1 giorno
dovrebbe uscire: 1 giorno

invece:

data inizio 31/03/2017
data fine 02/04/2017
esce: 2 giorni

allego esempio.
max

[Modificato da maxma62 02/09/2017 22:50]
____________________________
versione excel 365 ufficio
versione excel 2007 casa
Post: 532
Registrato il: 16/08/2015
Città: CORDENONS
Età: 67
Utente Senior
Excel 2016-32bit Win11
OFFLINE
02/09/2017 23:49

Per il caso specifico che hai evidenziato il confronto in queste righe di codice:
If intYears = 0 And intMonths = 0 And intDays > 1 Then
     GMA = intDays & " " & iG
va scritto così:
If intYears = 0 And intMonths = 0 And intDays >= 1 Then
     GMA = intDays & " " & iG

______________________________________________________________
C'è chi fa le COSE a CASO e chi fa CASO alle COSE (Ignoto)
Post: 740
Registrato il: 28/12/2009
Città: CITTADELLA
Età: 62
Utente Senior
excel 2007/365
OFFLINE
03/09/2017 08:50

Ciao rollis è O.K. [SM=g27811]
Ora in altri if else end if ci sono altri " > 1 " cambio anche in questi così " >=1 " ?

ElseIf intYears = 0 And intMonths >= 1 And intDays = 0 Then
GMA = intMonths & " " & iM

ElseIf intYears >= 1 And intMonths = 0 And intDays = 0 Then
GMA = intYears & " " & iA



Una curiosità se voglio cambiare i riferimenti della funzione:
Public Function GMA(Date1 As Date, Date2 As Date) As String
nella macro:

GMA = GMA_2    

If intYears = 0 And intMonths = 0 And intDays >= 1 Then
GMA_2 = intDays & " " & iG


ho provato ad aggiungere all'inizio:
Dim GMA_2 As String
ma non si visualizza niente.
Dove sbaglio?
max
____________________________
versione excel 365 ufficio
versione excel 2007 casa
Post: 1.590
Registrato il: 06/04/2013
Utente Veteran
2010
OFFLINE
03/09/2017 17:39

Ciao
a posto di quell'ambaradam, puoi fare così:
Public Function GMA(Date1 As Date, Date2 As Date) As String
Dim intYears As Variant, intMonths As Variant, intDays As Variant
Dim iG As String, iM As String, iA As String
  intMonths = DateDiff("m", Date1, Date2)
  intDays = DateDiff("d", DateAdd("m", intMonths, Date1), Date2)
  If intDays < 0 Then
    intMonths = intMonths - 1
    intDays = DateDiff("d", DateAdd("m", intMonths, Date1), Date2)
  End If
  intYears = intMonths \ 12
  intMonths = intMonths Mod 12
  If intDays > 1 Or intDays = 0 Then iG = "giorni" Else iG = "giorno"
  If intMonths > 1 Or intMonths = 0 Then iM = "mesi" Else iM = "mese"
  If intYears > 1 Or intYears = 0 Then iA = "anni" Else iA = "anno"
  If intDays = 0 Then
    iG = ""
    intDays = ""
  End If
  If intMonths = 0 Then
    iM = ""
    intMonths = ""
  End If
  If intYears = 0 Then
    iA = ""
    intYears = ""
  End If
  GMA = intYears & " " & iA & " " & intMonths & " " & iM & " " & intDays & " " & iG
End Function


Nota che ho variato le dim di intYears intMonths intDays da integer a variant per potergli assegnare = "".

Non ho capito che vuoi quando dici: "...cambiare i riferimenti della funzione..."

saluti
Domenico
Win 10 - Excel 2016
Post: 741
Registrato il: 28/12/2009
Città: CITTADELLA
Età: 62
Utente Senior
excel 2007/365
OFFLINE
03/09/2017 19:16

Ottimo dodo47,
ancora meglio. [SM=g27811]
Per il resto lasciamo tutto com'è.
Un saluto a tutti e grazie.
max
____________________________
versione excel 365 ufficio
versione excel 2007 casa
Vota:
Amministra Discussione: | Chiudi | Sposta | Cancella | Modifica | Notifica email Pagina precedente | 1 | Pagina successiva
Nuova Discussione
 | 
Rispondi
Cerca nel forum
Tag discussione
Discussioni Simili   [vedi tutte]
Feed | Forum | Bacheca | Album | Utenti | Cerca | Login | Registrati | Amministra
Tutti gli orari sono GMT+01:00. Adesso sono le 22:03. Versione: Stampabile | Mobile | Regolamento | Privacy
FreeForumZone [v.6.1] - Copyright © 2000-2024 FFZ srl - www.freeforumzone.com