<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Philben - Ms Access &#187; Algorithme</title>
	<atom:link href="https://blog.developpez.com/philben/ptag/algorithme/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/philben</link>
	<description></description>
	<lastBuildDate>Thu, 26 Sep 2013 19:43:53 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>VBA : Date de fin d&#8217;une période</title>
		<link>https://blog.developpez.com/philben/p12245/vba-access/vba-date-de-fin-dune-periode</link>
		<comments>https://blog.developpez.com/philben/p12245/vba-access/vba-date-de-fin-dune-periode#comments</comments>
		<pubDate>Sat, 21 Sep 2013 09:24:50 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Date]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=817</guid>
		<description><![CDATA[Pour faire suite au billet précédent, cette fonction retourne la date de fin de la période choisie (mois, bimestre, trimestre, quadrimestre, semestre et année). La fonction Public Function DateFinPeriode(ByVal DateRef As Date, ByVal Periode As Integer) As Date &#160; &#160;Select &#8230; <a href="https://blog.developpez.com/philben/p12245/vba-access/vba-date-de-fin-dune-periode">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Pour faire suite au <a href="http://blog.developpez.com/philben/p12244/vba-access/vba-date-de-debut-dune-periode" title="VBA : Date de début d’une période" target="_blank">billet précédent</a>, cette fonction retourne la date de fin de la période choisie (mois, bimestre, trimestre, quadrimestre, semestre et année).<br />
<span id="more-817"></span><br />
<strong>La fonction</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> DateFinPeriode(<span style="color: #151B8D; font-weight: bold;">ByVal</span> DateRef <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Periode <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Select</span> <span style="color: #8D38C9; font-weight: bold;">Case</span> Periode<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Case</span> 1, 2, 3, 4, 6, 12<br />
&nbsp; &nbsp; &nbsp; DateFinPeriode = DateSerial(Year(DateRef), Int((Month(DateRef) - 1) / Periode) * Periode + Periode + 1, 0)<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">Select</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Exemples</strong><br />
Des exemples à exécuter dans la fenêtre ‘Exécution’ (Ctrl+G) de l’éditeur VBA (Alt+F11).</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">?DateFinPeriode(#2013/09/20#,1) <span style="color: #008000;">'Mois -&gt; 30/09/2013 <br />
</span>?DateFinPeriode(#2013/09/20#,2) <span style="color: #008000;">'Bimestre -&gt; 31/10/2013 <br />
</span>?DateFinPeriode(#2013/09/20#,3) <span style="color: #008000;">'Trimestre -&gt; 30/09/2013<br />
</span>?DateFinPeriode(#2013/09/20#,4) <span style="color: #008000;">'Quadrimestre -&gt; 31/12/2013 <br />
</span>?DateFinPeriode(#2013/09/20#,6) <span style="color: #008000;">'Semestre -&gt; 31/12/2013 <br />
</span>?DateFinPeriode(#2013/09/20#,12) 'Annuelle -&gt; 31/12/2013</div></div>
<p>&nbsp;<br />
<strong>Vérification</strong><br />
Résultats dans la fenêtre &lsquo;Exécution&rsquo;</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestFinPeriode()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, y <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, s <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, c <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, r <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> v <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span>, aDerDate(0 <span style="color: #8D38C9; font-weight: bold;">To</span> 5) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, aCpt(0 <span style="color: #8D38C9; font-weight: bold;">To</span> 5) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, bErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #008000;">'vba.Array retourne toujours un tableau de base zéro (indépendance vis à vis de Option Base) contrairement à Array()<br />
</span> &nbsp; <span style="color: #008000;">'Mais en règle générale, on utilise Array()<br />
</span> &nbsp; v = VBA.Array(1, 2, 3, 4, 6, 12)<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;Randomize<br />
&nbsp; &nbsp;y = Int(3000 * Rnd) + 100<br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Année aléatoire : &quot;</span> &amp; y<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Mois&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot; Mensuel(1)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot;Bimestre(2)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot; Trimes.(3)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot; Quadri.(4)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot;Semestre(6)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot; Année(12)&quot;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008000;">'affecte une date initiale non interférente<br />
</span> &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = <span style="color: #151B8D; font-weight: bold;">LBound</span>(aDerDate) <span style="color: #8D38C9; font-weight: bold;">To</span> <span style="color: #151B8D; font-weight: bold;">UBound</span>(aDerDate): aDerDate(j) = #5/15/2000#: <span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 12<br />
&nbsp; &nbsp; &nbsp; r = DateSerial(y, i, Int(28 * Rnd) + 1) &nbsp; &nbsp;<span style="color: #008000;">'28 maxi pour février...<br />
</span> &nbsp; &nbsp; &nbsp;s = Format(r, <span style="color: #800000;">&quot;mm &nbsp;&quot;</span>) &amp; vbTab<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = <span style="color: #151B8D; font-weight: bold;">LBound</span>(v) <span style="color: #8D38C9; font-weight: bold;">To</span> <span style="color: #151B8D; font-weight: bold;">UBound</span>(v)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d = DateFinPeriode(r, v(j))<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> d &lt;&gt; aDerDate(j) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = <span style="color: #800000;">&quot;*&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> Month(d) &lt;&gt; Month(aDerDate(j)) <span style="color: #8D38C9; font-weight: bold;">And</span> Year(d) = y <span style="color: #8D38C9; font-weight: bold;">Then</span> aCpt(j) = aCpt(j) + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = <span style="color: #800000;">&quot; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s = s &amp; c &amp; d &amp; vbTab<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;aDerDate(j) = d<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> s<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = <span style="color: #151B8D; font-weight: bold;">LBound</span>(v) <span style="color: #8D38C9; font-weight: bold;">To</span> <span style="color: #151B8D; font-weight: bold;">UBound</span>(v)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> aCpt(i) &lt;&gt; 12 \ v(i) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Erreur détectée pour la période n°&quot;</span> &amp; (i + 1)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bErr = <span style="color: #00C2FF; font-weight: bold;">True</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> bErr <span style="color: #8D38C9; font-weight: bold;">Then</span> Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Aucune erreur détectée...&quot;</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;</p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VBA : Date de début d&#8217;une période</title>
		<link>https://blog.developpez.com/philben/p12244/vba-access/vba-date-de-debut-dune-periode</link>
		<comments>https://blog.developpez.com/philben/p12244/vba-access/vba-date-de-debut-dune-periode#comments</comments>
		<pubDate>Fri, 20 Sep 2013 19:11:39 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Date]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=807</guid>
		<description><![CDATA[Je vous présente une fonction qui retourne, en fonction de la date passée en paramètre et la période choisie (Mois, Bimestre, Trimestre, Quadrimestre, Semestre ou Année), la première date de cette période. La fonction Public Function DateDebutPeriode(ByVal DateRef As Date, &#8230; <a href="https://blog.developpez.com/philben/p12244/vba-access/vba-date-de-debut-dune-periode">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Je vous présente une fonction qui retourne, en fonction de la date passée en paramètre et la période choisie (Mois, Bimestre, Trimestre, Quadrimestre, Semestre ou Année), la première date de cette période.<br />
<span id="more-807"></span><br />
<strong>La fonction</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> DateDebutPeriode(<span style="color: #151B8D; font-weight: bold;">ByVal</span> DateRef <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Periode <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
<span style="color: #008000;">'Philben - v1.0 - Free to use<br />
</span> &nbsp; <span style="color: #8D38C9; font-weight: bold;">Select</span> <span style="color: #8D38C9; font-weight: bold;">Case</span> Periode<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Case</span> 1, 2, 3, 4, 6, 12<br />
&nbsp; &nbsp; &nbsp; DateDebutPeriode = DateSerial(Year(DateRef), Int((Month(DateRef) - 1) / Periode) * Periode + 1, 1)<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">Select</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Paramètres</strong><br />
Le premier paramètre est la date de référence de la période, le deuxième indique le type de période : 1 pour mensuelle, 2 pour bimestrielle, 3 pour trimestrielle, 4 pour quadrimestrielle, 6 pour semestrielle et 12 pour annuelle.</p>
<p>Il y a donc dans une année, 12 périodes mensuelles, 6 bimestres, 4 trimestres, 3 quadrimestres, 2 semestres.</p>
<p><strong>Exemples</strong><br />
Des exemples à exécuter dans fenêtre &lsquo;Exécution&rsquo; (Ctrl+G) de l&rsquo;éditeur VBA (Alt+F11).</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">?DateDebutPeriode(#2013/09/20#,1) <span style="color: #008000;">'Mois -&gt; 01/09/2013 <br />
</span><br />
?DateDebutPeriode(#2013/08/20#,2) <span style="color: #008000;">'Bimestre -&gt; 01/07/2013 <br />
</span>?DateDebutPeriode(#2013/09/20#,2) <span style="color: #008000;">'Bimestre -&gt; 01/09/2013 <br />
</span><br />
?DateDebutPeriode(#2013/09/20#,3) <span style="color: #008000;">'Trimestre -&gt; 01/07/2013<br />
</span>?DateDebutPeriode(#2013/08/20#,4) <span style="color: #008000;">'Quadrimestre -&gt; 01/05/2013 <br />
</span>?DateDebutPeriode(#2013/09/20#,6) <span style="color: #008000;">'Semestre -&gt; 01/07/2013 <br />
</span>?DateDebutPeriode(#2013/09/20#,12) 'Annuelle -&gt; 01/01/2013</div></div>
<p>&nbsp;<br />
<strong>Vérification</strong><br />
Cette fonction teste chaque période pour chaque mois d&rsquo;une année choisie aléatoirement.<br />
Un astérisque avant une date indique un changement de période.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestDebutPeriode()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, y <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, s <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, c <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, r <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> v <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span>, aDerDate(0 <span style="color: #8D38C9; font-weight: bold;">To</span> 5) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, aCpt(0 <span style="color: #8D38C9; font-weight: bold;">To</span> 5) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, bErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span><br />
<br />
&nbsp; &nbsp;v = VBA.Array(1, 2, 3, 4, 6, 12)<br />
&nbsp; &nbsp;Randomize<br />
&nbsp; &nbsp;y = Int(3000 * Rnd) + 100<br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Année aléatoire : &quot;</span> &amp; y<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Mois&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot; Mensuel(1)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot;Bimestre(2)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot; Trimes.(3)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot; Quadri.(4)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot;Semestre(6)&quot;</span> &amp; vbTab &amp; <span style="color: #800000;">&quot; Année(12)&quot;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 12<br />
&nbsp; &nbsp; &nbsp; r = DateSerial(y, i, Int(28 * Rnd) + 1) &nbsp; <span style="color: #008000;">'28 maxi pour février...<br />
</span> &nbsp; &nbsp; &nbsp;s = Format(r, <span style="color: #800000;">&quot;mm &nbsp;&quot;</span>) &amp; vbTab<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = <span style="color: #151B8D; font-weight: bold;">LBound</span>(v) <span style="color: #8D38C9; font-weight: bold;">To</span> <span style="color: #151B8D; font-weight: bold;">UBound</span>(v)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d = DateDebutPeriode(r, v(j))<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> d &lt;&gt; aDerDate(j) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = <span style="color: #800000;">&quot;*&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> Day(d) = 1 <span style="color: #8D38C9; font-weight: bold;">And</span> Year(d) = y <span style="color: #8D38C9; font-weight: bold;">Then</span> aCpt(j) = aCpt(j) + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c = <span style="color: #800000;">&quot; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s = s &amp; c &amp; d &amp; vbTab<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;aDerDate(j) = d<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> s<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = <span style="color: #151B8D; font-weight: bold;">LBound</span>(v) <span style="color: #8D38C9; font-weight: bold;">To</span> <span style="color: #151B8D; font-weight: bold;">UBound</span>(v)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> aCpt(i) &lt;&gt; 12 \ v(i) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Erreur détectée pour la période n°&quot;</span> &amp; (i + 1)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bErr = <span style="color: #00C2FF; font-weight: bold;">True</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> bErr <span style="color: #8D38C9; font-weight: bold;">Then</span> Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Aucune erreur détectée...&quot;</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VBA : Exemple comparatif des indices de similarité</title>
		<link>https://blog.developpez.com/philben/p12219/vba-access/vba-exemple-comparatif-des-indices-de-similarite</link>
		<comments>https://blog.developpez.com/philben/p12219/vba-access/vba-exemple-comparatif-des-indices-de-similarite#comments</comments>
		<pubDate>Sat, 07 Sep 2013 17:38:26 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[SQL - Ms Access]]></category>
		<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Chaîne de caractères]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Similarité]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=772</guid>
		<description><![CDATA[Après avoir vu différents algorithmes de similarité dans des billets précédents, je vous propose un petit comparatif à travers un exemple qui consiste à trouver le doublon de restaurants par leur nom, adresse, téléphone et type de cusine. Les données &#8230; <a href="https://blog.developpez.com/philben/p12219/vba-access/vba-exemple-comparatif-des-indices-de-similarite">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Après avoir vu différents algorithmes de similarité dans des billets précédents, je vous propose un petit comparatif à travers un exemple qui consiste à trouver le doublon de restaurants par leur nom, adresse, téléphone et type de cusine.<br />
<span id="more-772"></span><br />
<strong>Les données</strong><br />
Les données sont issues d&rsquo;un exemple connu de recherche de doublons et sont présentées ici dans une seule colonne où nom, adresse, téléphone et type de cuisine sont fusionnés. <a href="http://philben.developpez.com/restos.txt" title="Fichier texte des restaurants" target="_blank">Le fichier de données</a> contient 224 lignes et chaque resto possède un et un seul doublon. Cet exemple reste donc plus simple qu&rsquo;un cas général de recherche de doublons&#8230;<br />
Extrait du fichier :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Katsu 1972 Hillhurst Ave. Los Feliz 213-665-1891 Japanese<br />
Restaurant Katsu 1972 N. Hillhurst Ave. Los Angeles 213/665-1891 Asian<br />
Les Celebrites 155 W. 58th St. New York City 212-484-5113 French (Classic)<br />
Les C&amp;eacute;l&amp;eacute;brit&amp;eacute;s 160 Central Park S New York 212/484-5113 French</div></div>
<p>Une difficulté s&rsquo;ajoute pour le dernier restaurant car les &lsquo;é&rsquo; de Célébrités sont convertis en HTML (&amp; eacute;)&#8230;</p>
<p>Le fichier source original est <a href="http://www.cs.utexas.edu/users/ml/riddle/data/restaurant.tar.gz" title="Fichier source des restaurants" target="_blank">ici</a>.</p>
<p><strong>Principe</strong><br />
Le principe est donc de rechercher pour chaque ligne, le restaurant qui a la similarité la plus forte soit 49 952 mesures (224 x 223) après conversion des chaînes en majuscule.<br />
Pour chaque restaurant, je considère que le bon doublon a été correctement trouvé s&rsquo;il arrive avec le plus fort indice par rapport aux 222 autres restaurants.<br />
J&rsquo;ai comparé les 3 algorithmes <a href="http://blog.developpez.com/philben/p12207/vba-access/vba-distance-de-jaro-winkler" title="VBA : Distance de Jaro-Winkler" target="_blank">Jaro-Winkler (JW)</a>, <a href="http://blog.developpez.com/philben/p11268/vba-access/similarite_entre_deux_chaines_de_caracte" title="Similarité entre deux chaînes de caractères" target="_blank">Damerau-Levenshtein (DL)</a> et les <a href="http://blog.developpez.com/philben/p11340/vba-access/indices-de-similarite-entre-deux-chaines-de-caracteres" title="Indices de similarité entre deux chaînes de caractères" target="_blank">6 indices Cosinus/Dice/Jaccard/&#8230; (IS)</a>, en recherchant pour chacun la meilleure configuration.</p>
<p><strong>Résultats</strong><br />
Concernant DL, pas de problème pour trouver la meilleure configuration car il suffit de passer les 2 chaînes en argument de la fonction.<br />
<strong>Damerau-Levenshtein a permis de détecter <strong>185 doublons sur 224 soit 82,6%</strong> de réussite.</strong></p>
<p>Concernant Jaro-Winkler, on peut jouer sur la longueur du préfixe entre 0 et 4. Les résultats obtenus sont :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Longueur préfixe &nbsp; &nbsp; &nbsp; &nbsp;Doublons détectés<br />
&nbsp; &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 206<br />
&nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 209<br />
&nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 214<br />
&nbsp; &nbsp; &nbsp; &nbsp; 3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 213<br />
&nbsp; &nbsp; &nbsp; &nbsp; 4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 212</div></div>
<p>Dans cet exemple, la <strong>meilleure longueur de préfixe est 2 avec 214 doublons détectés soit un taux de réussite de 95,5%</strong>, la distance de Jaro seule (0) donne le moins bon résultat.</p>
<p>Enfin pour l&rsquo;algorithme IS, le choix du paramétrage est plus complexe car on peut choisir entre 6 indices de similarités, la longueur des Grammes (0 = mot à 5), l&rsquo;inversion ou non des grammes et une distance maximum de recherche des grammes communs.<br />
Pour la distance maxi, j&rsquo;ai laissé à -1 (pas de limite de distance) car elle fournit les meilleurs résultats dans cet exemple et la recherche de grammes inversés donne ici de moins bons résultats.</p>
<p>J&rsquo;ai donc joué sur le reste des paramètres (type d&rsquo;indice et longueur des grammes) et les résultats sont synthétisés ici :<br />
<img src="http://philben.developpez.com/restos.png" alt="Paramétrage des indices de similarité et résultats" /><br />
Les unigrammes (1) ont les moins bon résultats, la variabilité des résultats entre les indices diminue avec la longueur des grammes, les trigrammes et Simpson ont en moyenne les meilleurs résultats.</p>
<p>Pourcentage de réussite des indices en fonction de la longueur des grammes :<br />
<img src="http://philben.developpez.com/restosGraphe.png" alt="Pourcentage de réussites des indices en fonction de la longueur des grammes" /><br />
<strong>Finalement, l&rsquo;indice de Simpson avec des bigrammes donne ici le meilleur résultat : 219 doublons détectés soit 97,8% de réussite !</strong></p>
<p>Le tableau suivant indique le nombre de doublons détectés par position des résultats hormis le Top 1. Par exemple, 18 nouvelles bonnes détections faites par DL dans les Top 2 des résultats et <strong>Simpson en détecte 4 soit 223 détections cumulées (Top 1 et Top 2) sur 224 !</strong>.<br />
<img src="http://philben.developpez.com/restosPositions.png" alt="Nombre de détections en fonction de la position des résultats" /></p>
<p>Après avoir vu rapidement la capacité des indices à détecter les vrais doublons, il faut vérifier leur capacité à être discriminant entre les restaurants et compter les éventuels ex aequo.<br />
Imaginez un algo qui retourne un indice identique pour tous les restos : Ses résultats seraient parfaits mais pour chaque restaurant on aurait 223 restaurants détectés dans le Top 1 !</p>
<p>Via une requête, j&rsquo;ai donc calculé la position absolue du vrai doublon pour chaque algorithme et chaque ligne. Des ex aequo sont présents si la position absolue est supérieure au classement.</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Indice&nbsp; Classement &nbsp; Position absolue<br />
---------------------------------------<br />
JW&nbsp; &nbsp; &nbsp; 28 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;29<br />
---------------------------------------<br />
IS&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2<br />
---------------------------------------<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 9<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;6 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;7 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;12<br />
DL&nbsp; &nbsp; &nbsp; &nbsp;9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;11<br />
DL&nbsp; &nbsp; &nbsp; 12 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;20<br />
DL&nbsp; &nbsp; &nbsp; 17 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;21<br />
DL&nbsp; &nbsp; &nbsp; 24 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;66</div></div>
<p>JW n&rsquo;est affecté qu&rsquo;une fois par un ex aequo et ceci pour un classement du vrai doublon en 28ème position.<br />
IS en a obtenu un seul sur un Top 1 et DL a eu 13 fois un ou plusieurs ex aequo.</p>
<p><strong>Conclusion</strong><br />
Dans cet exemple, l&rsquo;indice de Simpson a détecté le plus de doublons dans le Top 1 (219 sur 224) et 223 dans le Top 2 des résultats.<br />
Si vous le souhaitez, vous pouvez ajouter <strong>vos propres résultats</strong> de vos algorithmes en commentaire du billet.</p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VBA : Distance de Jaro-Winkler</title>
		<link>https://blog.developpez.com/philben/p12207/vba-access/vba-distance-de-jaro-winkler</link>
		<comments>https://blog.developpez.com/philben/p12207/vba-access/vba-distance-de-jaro-winkler#comments</comments>
		<pubDate>Sat, 31 Aug 2013 03:01:16 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[SQL - Ms Access]]></category>
		<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Chaîne de caractères]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Similarité]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=703</guid>
		<description><![CDATA[Cet algorithme mesure la similarité entre deux chaînes de caractères pour la recherche de doublons par exemple. Tout comme les algorithmes de Damerau-Levenshtein et les indices de similarité Cosinus, Dice, Jaccard&#8230;, la distance est normalisée entre 0 et 1. Plus &#8230; <a href="https://blog.developpez.com/philben/p12207/vba-access/vba-distance-de-jaro-winkler">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Cet algorithme mesure la similarité entre deux chaînes de caractères pour la recherche de doublons par exemple. Tout comme les algorithmes de <a href="http://blog.developpez.com/philben/p11268/vba-access/similarite_entre_deux_chaines_de_caracte" title="Similarité entre deux chaînes de caractères" target="_blank">Damerau-Levenshtein</a> et les <a href="http://blog.developpez.com/philben/p11340/vba-access/indices-de-similarite-entre-deux-chaines-de-caracteres" title="Indices de similarité entre deux chaînes de caractères" target="_blank">indices de similarité Cosinus, Dice, Jaccard&#8230;</a>, la distance est normalisée entre 0 et 1. Plus la similarité est forte plus la distance tend vers 1.<br />
<span id="more-703"></span><br />
<strong>La fonction VBA</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> JaroWinkler(<span style="color: #151B8D; font-weight: bold;">ByVal</span> s1 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> s2 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> LongueurPrefixe <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Byte</span> = 3, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> TypeComparaison <span style="color: #151B8D; font-weight: bold;">As</span> VbCompareMethod = vbBinaryCompare) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Single</span><br />
<span style="color: #008000;">'La distance de jaro_Winkler mesure la similarité entre deux chaines de caractères (s1 et s2)<br />
</span><span style="color: #008000;">'La distance est normalisée entre 0 et 1 (1 = similarité maximale)<br />
</span><span style="color: #008000;">'si LongueurPrefixe [0,4] = 0 =&gt; Distance de Jaro<br />
</span><span style="color: #008000;">'Référence : http://fr.wikipedia.org/wiki/Distance_de_Jaro-Winkler et références<br />
</span><span style="color: #008000;">'Remarques : Préparation parfois nécessaire des chaînes : ucase(),trim(),diacritiques<br />
</span><span style="color: #008000;">' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Quelques libertés prises pour le calcul de la fenêtre (dMax) / Wikipédia<br />
</span><span style="color: #008000;">'Auteur : Philben v1.0 - Free to use<br />
</span> &nbsp; Const cMaxLongueurPrefixe <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Byte</span> = 4, cCoeffPrefixe <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Single</span> = 0.1<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> l1 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, l2 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, dMax <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, k <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> jMin <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, jMax <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, m <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, t <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, p <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, lam1 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> c1 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, r <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Single</span>, aUsed() <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span>, ac2() <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, am1() <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<br />
&nbsp; &nbsp;l1 = Len(s1): l2 = Len(s2)<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> l1 &gt; 0 <span style="color: #8D38C9; font-weight: bold;">And</span> l2 &gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">ReDim</span> aUsed(1 <span style="color: #8D38C9; font-weight: bold;">To</span> l2): <span style="color: #151B8D; font-weight: bold;">ReDim</span> ac2(1 <span style="color: #8D38C9; font-weight: bold;">To</span> l2)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> l2: ac2(i) = Mid$(s2, i, 1): <span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> l1 &gt;= l2 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">'dMax (1/2 fenêtre) calculée avec la + courte chaîne et non la + longue ! (voir blog)<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; dMax = (l2 + 1) \ 2 - 1 &nbsp; <span style="color: #008000;">'arrondi sup. de dMax (l=1 =&gt; 0, l=2 =&gt; 0, l=3 =&gt; 1)<br />
</span> &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dMax = (l1 + 1) \ 2 - 1<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> LongueurPrefixe &gt; cMaxLongueurPrefixe <span style="color: #8D38C9; font-weight: bold;">Then</span> LongueurPrefixe = cMaxLongueurPrefixe<br />
<br />
&nbsp; &nbsp; &nbsp; jMin = 1<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> dMax &lt; l2 <span style="color: #8D38C9; font-weight: bold;">Then</span> jMax = dMax <span style="color: #8D38C9; font-weight: bold;">Else</span>: jMax = l2<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> l1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c1 = Mid$(s1, i, 1)<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> i &gt; dMax + 1 <span style="color: #8D38C9; font-weight: bold;">Then</span> jMin = jMin + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> jMax &lt; l2 <span style="color: #8D38C9; font-weight: bold;">Then</span> jMax = jMax + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> jMin &gt; jMax <span style="color: #8D38C9; font-weight: bold;">Then</span> <span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #8D38C9; font-weight: bold;">For</span> &nbsp; <span style="color: #008000;">'définitivement hors fenêtre =&gt; Fin<br />
</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> j = jMin <span style="color: #8D38C9; font-weight: bold;">To</span> jMax<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> aUsed(j) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> StrComp(c1, ac2(j), TypeComparaison) = 0 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m = m + 1 &nbsp; <span style="color: #008000;">'Match<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> i = j <span style="color: #8D38C9; font-weight: bold;">And</span> i &lt;= LongueurPrefixe <span style="color: #8D38C9; font-weight: bold;">And</span> p = i - 1 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p = p + 1 &nbsp;<span style="color: #008000;">'Préfixe commun<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lam1 = lam1 + 1: <span style="color: #151B8D; font-weight: bold;">ReDim</span> <span style="color: #151B8D; font-weight: bold;">Preserve</span> am1(1 <span style="color: #8D38C9; font-weight: bold;">To</span> lam1): am1(lam1) = i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aUsed(j) = <span style="color: #00C2FF; font-weight: bold;">True</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #8D38C9; font-weight: bold;">For</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">'Compte les tranpositions<br />
</span> &nbsp; &nbsp; &nbsp;k = p + 1<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> lam1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> j = k <span style="color: #8D38C9; font-weight: bold;">To</span> l2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> aUsed(j) <span style="color: #8D38C9; font-weight: bold;">Then</span> <span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #8D38C9; font-weight: bold;">For</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> StrComp(Mid$(s1, am1(i), 1), ac2(j), TypeComparaison) &lt;&gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> t = t + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k = j + 1<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
&nbsp; &nbsp; &nbsp; t = t \ 2<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> m &gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;r = (m / l1 + m / l2 + (m - t) / m) / 3 &nbsp; <span style="color: #008000;">'Distance de Jaro<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; r = r + (p * cCoeffPrefixe * (1 - r)) &nbsp; <span style="color: #008000;">'Extension de Winkler<br />
</span> &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<br />
&nbsp; &nbsp;JaroWinkler = r<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Paramètres de la fonction</strong><br />
<strong>&lsquo;S1&prime; et &lsquo;S2&prime;</strong> correspondent aux deux chaînes de caractères à étudier.<br />
<strong>&lsquo;LongueurPrefixe&rsquo;</strong> permet de choisir la longueur du préfixe commun entre les deux chaînes. Cette longueur permet de calculer l&rsquo;apport de Winkler à l&rsquo;algorithme de Jaro. Cette longueur est comprise entre 0 et 4 (3 par défaut). La distance de Jaro est retournée lorsque ce paramètre est égal à 0.<br />
<strong>&lsquo;TypeComparaison&rsquo;</strong> permet de choisir le type de comparaison entre les deux chaînes : Soit une comparaison binaire (par défaut) soit une comparaison textuelle (moins restrictive car ne tient pas compte de la casse des caractères mais généralement moins performante).<br />
Il sera souvent nécessaire de prétraiter les chaînes par UCase()/LCase() (casse uniforme des caractères), Trim() (suppression des espaces en début et fin de chaîne) et remplacer les caractères diacritiques pour ne pas &lsquo;parasiter&rsquo; l&rsquo;algorithme (voir <a href="http://blog.developpez.com/philben/p11217/vba-access/remplacer_les_caracteres_accentues_d_une" title="Remplacer les caractères accentués d’une chaîne" target="_blank">ici</a>).</p>
<p><strong>Tests</strong><br />
J&rsquo;ai récolté dans une publication de W. E. Winkler (voir référence plus bas) et sur le Web des exemples pour tester les résultats de mon code. La longueur du préfixe est fixée à 4.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> testJW()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, r <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Single</span>, v() <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span><br />
<br />
&nbsp; &nbsp;v = Array(<span style="color: #800000;">&quot;SHACKLEFORD&quot;</span>, <span style="color: #800000;">&quot;SHACKELFORD&quot;</span>, 0.982, <span style="color: #800000;">&quot;DUNNINGHAM&quot;</span>, <span style="color: #800000;">&quot;CUNNIGHAM&quot;</span>, 0.896, <span style="color: #800000;">&quot;NICHLESON&quot;</span>, <span style="color: #800000;">&quot;NICHULSON&quot;</span>, 0.956, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;JONES&quot;</span>, <span style="color: #800000;">&quot;JOHNSON&quot;</span>, 0.832, <span style="color: #800000;">&quot;MASSEY&quot;</span>, <span style="color: #800000;">&quot;MASSIE&quot;</span>, 0.933, <span style="color: #800000;">&quot;ABROMS&quot;</span>, <span style="color: #800000;">&quot;ABRAMS&quot;</span>, 0.922, <span style="color: #800000;">&quot;HARDIN&quot;</span>, <span style="color: #800000;">&quot;MARTINEZ&quot;</span>, 0#, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;ITMAN&quot;</span>, <span style="color: #800000;">&quot;SMITH&quot;</span>, 0#, <span style="color: #800000;">&quot;JERALDINE&quot;</span>, <span style="color: #800000;">&quot;GERALDINE&quot;</span>, 0.926, <span style="color: #800000;">&quot;MARHTA&quot;</span>, <span style="color: #800000;">&quot;MARTHA&quot;</span>, 0.961, <span style="color: #800000;">&quot;MICHELLE&quot;</span>, <span style="color: #800000;">&quot;MICHAEL&quot;</span>, 0.921, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;JULIES&quot;</span>, <span style="color: #800000;">&quot;JULIUS&quot;</span>, 0.933, <span style="color: #800000;">&quot;TANYA&quot;</span>, <span style="color: #800000;">&quot;TONYA&quot;</span>, 0.88, <span style="color: #800000;">&quot;DWAYNE&quot;</span>, <span style="color: #800000;">&quot;DUANE&quot;</span>, 0.84, <span style="color: #800000;">&quot;SEAN&quot;</span>, <span style="color: #800000;">&quot;SUSAN&quot;</span>, 0.805, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;JON&quot;</span>, <span style="color: #800000;">&quot;JOHN&quot;</span>, 0.933, <span style="color: #800000;">&quot;JON&quot;</span>, <span style="color: #800000;">&quot;JAN&quot;</span>, 0#, <span style="color: #800000;">&quot;DIXON&quot;</span>, <span style="color: #800000;">&quot;DICKSONX&quot;</span>, 0.813, <span style="color: #800000;">&quot;VOYAGE&quot;</span>, <span style="color: #800000;">&quot;AGENDA&quot;</span>, 0.444, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;TRAVEL&quot;</span>, <span style="color: #800000;">&quot;TRAIN&quot;</span>, 0.9, <span style="color: #800000;">&quot;ABCVWXYZ&quot;</span>, <span style="color: #800000;">&quot;CABVWXYZ&quot;</span>, 0.958)<br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Chaîne 1&quot;</span>, <span style="color: #800000;">&quot;Chaîne 2&quot;</span>, <span style="color: #800000;">&quot;Attendue&quot;</span>, <span style="color: #800000;">&quot;Obtenue&quot;</span><br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;--------&quot;</span>, <span style="color: #800000;">&quot;--------&quot;</span>, <span style="color: #800000;">&quot;---------&quot;</span>, <span style="color: #800000;">&quot;-------&quot;</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = <span style="color: #151B8D; font-weight: bold;">LBound</span>(v) <span style="color: #8D38C9; font-weight: bold;">To</span> <span style="color: #151B8D; font-weight: bold;">UBound</span>(v) <span style="color: #8D38C9; font-weight: bold;">Step</span> 3<br />
&nbsp; &nbsp; &nbsp; r = JaroWinkler(v(i), v(i + 1), 4) &nbsp; <span style="color: #008000;">'Longueur Prefixe = 4<br />
</span> &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> v(i), v(i + 1), v(i + 2), Round(r, 3), IIf(Abs(r - v(i + 2)) &gt; 0.001, <span style="color: #800000;">&quot;*** Ecart ***&quot;</span>, <span style="color: #800000;">&quot;&quot;</span>)<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Résultats et commentaires</strong></p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Chaîne 1 &nbsp; &nbsp; &nbsp;Chaîne 2 &nbsp; &nbsp; &nbsp;Attendue &nbsp; &nbsp; &nbsp;Obtenue<br />
-------- &nbsp; &nbsp; &nbsp;-------- &nbsp; &nbsp; &nbsp;--------- &nbsp; &nbsp; -------<br />
SHACKLEFORD &nbsp; SHACKELFORD &nbsp; &nbsp;0,982 &nbsp; &nbsp; &nbsp; &nbsp; 0,982 &nbsp; &nbsp; &nbsp; &nbsp;<br />
DUNNINGHAM &nbsp; &nbsp;CUNNIGHAM &nbsp; &nbsp; &nbsp;0,896 &nbsp; &nbsp; &nbsp; &nbsp; 0,896 &nbsp; &nbsp; &nbsp; &nbsp;<br />
NICHLESON &nbsp; &nbsp; NICHULSON &nbsp; &nbsp; &nbsp;0,956 &nbsp; &nbsp; &nbsp; &nbsp; 0,956 &nbsp; &nbsp; &nbsp; &nbsp;<br />
JONES &nbsp; &nbsp; &nbsp; &nbsp; JOHNSON &nbsp; &nbsp; &nbsp; &nbsp;0,832 &nbsp; &nbsp; &nbsp; &nbsp; 0,832 &nbsp; &nbsp; &nbsp; &nbsp;<br />
MASSEY &nbsp; &nbsp; &nbsp; &nbsp;MASSIE &nbsp; &nbsp; &nbsp; &nbsp; 0,933 &nbsp; &nbsp; &nbsp; &nbsp; 0,933 &nbsp; &nbsp; &nbsp; &nbsp;<br />
ABROMS &nbsp; &nbsp; &nbsp; &nbsp;ABRAMS &nbsp; &nbsp; &nbsp; &nbsp; 0,922 &nbsp; &nbsp; &nbsp; &nbsp; 0,922 &nbsp; &nbsp; &nbsp; &nbsp;<br />
HARDIN &nbsp; &nbsp; &nbsp; &nbsp;MARTINEZ &nbsp; &nbsp; &nbsp; 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,722 &nbsp; &nbsp; &nbsp; &nbsp;*** Ecart ***<br />
ITMAN &nbsp; &nbsp; &nbsp; &nbsp; SMITH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,622 &nbsp; &nbsp; &nbsp; &nbsp;*** Ecart ***<br />
JERALDINE &nbsp; &nbsp; GERALDINE &nbsp; &nbsp; &nbsp;0,926 &nbsp; &nbsp; &nbsp; &nbsp; 0,926 &nbsp; &nbsp; &nbsp; &nbsp;<br />
MARHTA &nbsp; &nbsp; &nbsp; &nbsp;MARTHA &nbsp; &nbsp; &nbsp; &nbsp; 0,961 &nbsp; &nbsp; &nbsp; &nbsp; 0,961 &nbsp; &nbsp; &nbsp; &nbsp;<br />
MICHELLE &nbsp; &nbsp; &nbsp;MICHAEL &nbsp; &nbsp; &nbsp; &nbsp;0,921 &nbsp; &nbsp; &nbsp; &nbsp; 0,921 &nbsp; &nbsp; &nbsp; &nbsp;<br />
JULIES &nbsp; &nbsp; &nbsp; &nbsp;JULIUS &nbsp; &nbsp; &nbsp; &nbsp; 0,933 &nbsp; &nbsp; &nbsp; &nbsp; 0,933 &nbsp; &nbsp; &nbsp; &nbsp;<br />
TANYA &nbsp; &nbsp; &nbsp; &nbsp; TONYA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,88 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,88 &nbsp; &nbsp; &nbsp; &nbsp; <br />
DWAYNE &nbsp; &nbsp; &nbsp; &nbsp;DUANE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,84 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,84 &nbsp; &nbsp; &nbsp; &nbsp; <br />
SEAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SUSAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,805 &nbsp; &nbsp; &nbsp; &nbsp; 0,805 &nbsp; &nbsp; &nbsp; &nbsp;<br />
JON &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOHN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,933 &nbsp; &nbsp; &nbsp; &nbsp; 0,933 &nbsp; &nbsp; &nbsp; &nbsp;<br />
JON &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JAN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,8 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*** Ecart ***<br />
DIXON &nbsp; &nbsp; &nbsp; &nbsp; DICKSONX &nbsp; &nbsp; &nbsp; 0,813 &nbsp; &nbsp; &nbsp; &nbsp; 0,813 &nbsp; &nbsp; &nbsp; &nbsp;<br />
VOYAGE &nbsp; &nbsp; &nbsp; &nbsp;AGENDA &nbsp; &nbsp; &nbsp; &nbsp; 0,444 &nbsp; &nbsp; &nbsp; &nbsp; 0,444 &nbsp; &nbsp; &nbsp; &nbsp;<br />
TRAVEL &nbsp; &nbsp; &nbsp; &nbsp;TRAIN &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0,9 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0,79 &nbsp; &nbsp; &nbsp; &nbsp; *** Ecart ***<br />
ABCVWXYZ &nbsp; &nbsp; &nbsp;CABVWXYZ &nbsp; &nbsp; &nbsp; 0,958 &nbsp; &nbsp; &nbsp; &nbsp; 0,958</div></div>
<p>&nbsp;<br />
Les distances obtenues sont parfois différentes des distances attendues (4 écarts constatés).<br />
Concernant les couples <strong>HARDIN/MARTINEZ, ITMAN/SMITH, JON/JAN</strong>, ils sont fournis par W. E. Winkler qui devait utiliser un algo un peu différent de celui décrit dans Wikipédia (en effet, sa fonction strcomp.c est plus complexe&#8230;)</p>
<p>Concernant le couple <strong>TRAVEL/TRAIN</strong> et la distance attendue de 0,9, je pense qu&rsquo;il s&rsquo;agit d&rsquo;une coquille de l&rsquo;auteur car la distance de Jaro dans la même publication était seulement de 0,7.</p>
<p>De plus, l&rsquo;applet java de <a href="http://arantxa.ii.uam.es/~dcamacho/StringDistance/StringDistance.htm" title="String Distance" target="_blank">ce site</a> donne des résultats proches ou identiques aux miens.</p>
<p><strong>Remarque</strong><br />
L&rsquo;éloignement maximal (variable dMax dans mon code) devrait être calculé à l&rsquo;aide de la plus grande longueur de chaîne. J&rsquo;ai préféré utiliser la plus courte pour éviter des résultats &lsquo;étonnants&rsquo; avec certaines chaînes de caractères.<br />
Par exemple, en allongeant la seconde chaîne &lsquo;cba&rsquo; et en utilisant la plus grande longueur des deux chaînes de caractères, on obtient :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">?JaroWinkler(<span style="color: #800000;">&quot;abc&quot;</span>,<span style="color: #800000;">&quot;cba&quot;</span>) <span style="color: #008000;">'0,556<br />
</span>?JaroWinkler(<span style="color: #800000;">&quot;abc&quot;</span>,<span style="color: #800000;">&quot;cbau&quot;</span>) <span style="color: #008000;">'0,528<br />
</span>?JaroWinkler(<span style="color: #800000;">&quot;abc&quot;</span>,<span style="color: #800000;">&quot;cbauu&quot;</span>) <span style="color: #008000;">'0,756<br />
</span>?JaroWinkler(<span style="color: #800000;">&quot;abc&quot;</span>,<span style="color: #800000;">&quot;cbauuu&quot;</span>) '0,722</div></div>
<p>La similarité diminue avec 4 caractères (0,528) puis augmente paradoxalement avec 5 caractères (&lsquo;cbauu&rsquo; donne 0,756)!<br />
<strong>Pourquoi ?</strong><br />
<strong>Dans le 1er exemple</strong>, l&rsquo;éloignement maximal (variable dMax dans le code) est égal à 1 et on obtient une seule concordance (lettre &lsquo;b&rsquo;) entre les 2 chaînes.<br />
<strong>Dans le 2ème exemple</strong>, la similarité chute légèrement car la longueur de la chaîne n°2 est de 4 au lieu de 3 et celle-ci intervient en tant que dénominateur dans le calcul de la distance de Jaro. L&rsquo;éloignement maximal reste égal à 1.<br />
<strong>Dans le 3ème exemple</strong>, l&rsquo;éloignement maximal est égal à <strong>2</strong> car la longueur de la chaîne n°2 est de 5 caractères (arrondi supérieur de 5/2 &#8211; 1 = 2). Cet éloignement permet d&rsquo;obtenir 3 correspondances (&lsquo;a&rsquo;, &lsquo;b&rsquo;, &lsquo;c&rsquo;) et une transposition (&lsquo;a&rsquo; et &lsquo;c&rsquo;). Ces 3 correspondances au lieu d&rsquo;une seule augmentent la similarité malgré la transposition et la longueur de la chaîne n°2.</p>
<p><strong>Pour éviter ce problème</strong>, une solution consiste donc à utiliser la plus petite longueur des deux chaînes :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">?JaroWinkler(<span style="color: #800000;">&quot;abc&quot;</span>,<span style="color: #800000;">&quot;cba&quot;</span>) <span style="color: #008000;">'0,556<br />
</span>?JaroWinkler(<span style="color: #800000;">&quot;abc&quot;</span>,<span style="color: #800000;">&quot;cbau&quot;</span>) <span style="color: #008000;">'0,528<br />
</span>?JaroWinkler(<span style="color: #800000;">&quot;abc&quot;</span>,<span style="color: #800000;">&quot;cbauu&quot;</span>) <span style="color: #008000;">'0,511<br />
</span>?JaroWinkler(<span style="color: #800000;">&quot;abc&quot;</span>,<span style="color: #800000;">&quot;cbauuu&quot;</span>) '0,5</div></div>
<p>&nbsp;<br />
<strong>Conclusion</strong><br />
Algorithme rapide et efficace sur des chaînes relativement courtes.</p>
<p><strong>Références</strong><br />
Wikipédia: <a href="http://fr.wikipedia.org/wiki/Distance_de_Jaro-Winkler" title="Jaro_Winkler sur Wikipédia" target="_blank">Distance de Jaro-Winkler</a><br />
Publication de <a href="http://www.census.gov/srd/papers/pdf/rrs2006-02.pdf" title="Overview of Record Linkage and Current Research Directions" target="_blank">W. E. Winkler</a>.</p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date d&#8217;un jour de la semaine courante</title>
		<link>https://blog.developpez.com/philben/p11481/vba-access/date-dun-jour-de-la-semaine-courante</link>
		<comments>https://blog.developpez.com/philben/p11481/vba-access/date-dun-jour-de-la-semaine-courante#comments</comments>
		<pubDate>Thu, 08 Nov 2012 18:17:01 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Jour de semaine]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=590</guid>
		<description><![CDATA[Episode V de la saga &#8216;Weekday&#8217;. Cette fois-ci, on introduit la notion de semaine puisque l&#8217;on souhaite obtenir la date d&#8217;un jour quelconque de la semaine de la date de référence. Quelle est la date du dimanche de cette semaine &#8230; <a href="https://blog.developpez.com/philben/p11481/vba-access/date-dun-jour-de-la-semaine-courante">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Episode <strong>V</strong> de la saga &lsquo;Weekday&rsquo;. Cette fois-ci, on introduit la notion de <strong>semaine</strong> puisque l&rsquo;on souhaite obtenir la date d&rsquo;un jour quelconque de la semaine de la date de référence. Quelle est la date du dimanche de cette semaine ? La date du mardi ?<br />
<span id="more-590"></span><br />
<strong>Préambule</strong><br />
Je vous conseille de lire le <a href="http://blog.developpez.com/philben/p11417/vba-access/date-du-jour-de-semaine-suivant" title="Date du jour de semaine suivant" target="_blank">premier billet de &lsquo;Weekday&rsquo;</a> pour comprendre le principe des calculs.<br />
Voir aussi le <a href="http://blog.developpez.com/philben/p12177/vba-access/vba-recapitulatif-des-fonctions-jour-de-semaine" title="VBA : Récapitulatif des fonctions ‘jour de semaine’" target="_blank">récapitulatif</a> des fonctions.</p>
<p><strong>Les matrices</strong><br />
On souhaite donc passer de la matrice &lsquo;Weekday&rsquo; à la matrice &lsquo;Jour de la semaine courante&rsquo;. Si le premier jour de semaine est un lundi (qui peut être le dimanche comme aux U.S.A.), la matrice finale est :</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Weekday(Date,VbDayOfWeek) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Jour de la semaine (1er jour : lundi)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VbDayOfWeek &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VbDayOfWeek &nbsp; &nbsp; &nbsp; &nbsp;<br />
Jour Date &nbsp;L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D &nbsp; &nbsp; &nbsp;Jour date &nbsp;L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D <br />
Lundi &nbsp; &nbsp; &nbsp;1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; &nbsp; &nbsp;Lundi &nbsp; &nbsp; &nbsp;0 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 &nbsp; 5 &nbsp; 6 <br />
Mardi &nbsp; &nbsp; &nbsp;2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; &nbsp; &nbsp;Mardi &nbsp; &nbsp; -1 &nbsp; 0 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 &nbsp; 5 <br />
Mercredi &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 vers Mercredi &nbsp;-2 &nbsp;-1 &nbsp; 0 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 <br />
Jeudi &nbsp; &nbsp; &nbsp;4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; &nbsp; &nbsp;Jeudi &nbsp; &nbsp; -3 &nbsp;-2 &nbsp;-1 &nbsp; 0 &nbsp; 1 &nbsp; 2 &nbsp; 3 <br />
Vendredi &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; &nbsp; &nbsp;Vendredi &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp; 0 &nbsp; 1 &nbsp; 2 <br />
Samedi &nbsp; &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; &nbsp; &nbsp;Samedi &nbsp; &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp; 0 &nbsp; 1 <br />
Dimanche &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; &nbsp; &nbsp;Dimanche &nbsp;-6 &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp; 0</div></div>
<p>&nbsp;<br />
On remarque que les valeurs sont symétriquement opposées par rapport à la diagonale. Pour pouvoir gérer tous les cas quelque soit le premier jour de semaine, le plus simple est de se servir de ce premier jour de semaine comme pivot.</p>
<p><strong>La fonction VBA</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> DateJourSemaineEnCours(<span style="color: #151B8D; font-weight: bold;">ByVal</span> DateRef <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Jour <span style="color: #151B8D; font-weight: bold;">As</span> VbDayOfWeek, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> PremierJourDeSemaine <span style="color: #151B8D; font-weight: bold;">As</span> VbDayOfWeek = vbMonday) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;DateJourSemaineEnCours = DateRef + (Jour - CInt(Weekday(DateRef, PremierJourDeSemaine)) - PremierJourDeSemaine + 1)<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> Jour &lt; PremierJourDeSemaine <span style="color: #8D38C9; font-weight: bold;">Then</span> DateJourSemaineEnCours = DateJourSemaineEnCours + 7<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
La fonction admet trois paramètres : La date de référence et le jour de semaine recherché. Le troisième paramètre est optionnel (PremierJourDeSemaine) et contient sous forme de constante le nom du 1er jour de semaine. La valeur par défaut de ce paramètre est <strong>lundi</strong>.</p>
<p><strong>Tester la fonction</strong><br />
La fonction suivante teste les 49 x 7 possibilités des matrices et affiche les résultats dans la fenêtre ‘Exécution’ de l’éditeur VB.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestDateJourSemaineEnCours()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> s <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, dates(1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, k <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, l <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, NbErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, SumErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
<br />
&nbsp; &nbsp;d = Int(Now())<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Test DateJourSemaineEnCours()&quot;</span> &amp; vbCrLf &amp; <span style="color: #800000;">&quot;----------------------&quot;</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> l = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7<br />
&nbsp; &nbsp; &nbsp; d = Int(Now())<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">While</span> Weekday(d) &lt;&gt; l: d = d + 1: Wend<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> k = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> 6: dates(Weekday(d + k)) = d + k: <span style="color: #8D38C9; font-weight: bold;">Next</span> k<br />
<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;* Semaine du &quot;</span> &amp; Format(d, <span style="color: #800000;">&quot;dddd dd/mm/yyyy&quot;</span>) &amp; <span style="color: #800000;">&quot; au &quot;</span> &amp; Format(d + 6, <span style="color: #800000;">&quot;dddd dd/mm/yyyy&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NbErr = 0<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s = vbTab &amp; <span style="color: #800000;">&quot;-&gt; Jour recherché : &quot;</span> &amp; Format(d + i - 1, <span style="color: #800000;">&quot;ddd dd/mm/yyyy&quot;</span>) &amp; <span style="color: #800000;">&quot; : &quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> j = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> 6<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> DateJourSemaineEnCours(d + j, i, l) &lt;&gt; dates(i) <span style="color: #8D38C9; font-weight: bold;">Then</span> NbErr = NbErr + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> s &amp; IIf(NbErr &gt; 0, NbErr, <span style="color: #800000;">&quot; Aucune&quot;</span>) &amp; <span style="color: #800000;">&quot; erreur(s)&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SumErr = SumErr + NbErr<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> l<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;***&quot;</span> &amp; IIf(SumErr &gt; 0, SumErr, <span style="color: #800000;">&quot; Aucune&quot;</span>) &amp; <span style="color: #800000;">&quot; erreur(s) commise(s) ***&quot;</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Exemples</strong><br />
A coller dans la fenêtre &lsquo;Exécution&rsquo; du VBE.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Date du dimanche pour la date donnée et pour une semaine commençant le lundi<br />
</span>?DateJourSemaineEnCours(#2012/11/8#,vbSunday) <span style="color: #008000;">'11/11/2012<br />
</span><span style="color: #008000;">'Date du lundi pour la date donnée et pour une semaine commençant le lundi<br />
</span>?DateJourSemaineEnCours(#2012/11/8#,vbMonday) <span style="color: #008000;">'05/11/2012<br />
</span><span style="color: #008000;">'Date du dimanche pour la date donnée et pour une semaine commençant le dimanche<br />
</span>?DateJourSemaineEnCours(#2012/11/8#,vbSunday,vbSunday) '04/11/2012</div></div>
<p>&nbsp;<br />
@+ pour la suite des épisodes ‘Weekday’.</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date du jour-même ou précédent</title>
		<link>https://blog.developpez.com/philben/p11470/vba-access/date-du-jour-meme-ou-precedent</link>
		<comments>https://blog.developpez.com/philben/p11470/vba-access/date-du-jour-meme-ou-precedent#comments</comments>
		<pubDate>Fri, 02 Nov 2012 11:53:31 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Jour de semaine]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=585</guid>
		<description><![CDATA[Episode IV de la saga &#8216;Weekday&#8217;. On souhaite obtenir cette fois-ci, la date d&#8217;un jour de semaine qui précède ou NON la date de référence. Préambule Je vous conseille de lire le premier billet de &#8216;Weekday&#8217; pour comprendre le principe &#8230; <a href="https://blog.developpez.com/philben/p11470/vba-access/date-du-jour-meme-ou-precedent">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Episode <strong>IV</strong> de la saga &lsquo;Weekday&rsquo;. On souhaite obtenir cette fois-ci, la date d&rsquo;un jour de semaine qui précède ou <strong>NON</strong> la date de référence.<br />
<span id="more-585"></span><br />
<strong>Préambule</strong><br />
Je vous conseille de lire le <a href="http://blog.developpez.com/philben/p11417/vba-access/date-du-jour-de-semaine-suivant" title="Date du jour de semaine suivant" target="_blank">premier billet de &lsquo;Weekday&rsquo;</a> pour comprendre le principe des calculs.<br />
Voir aussi le <a href="http://blog.developpez.com/philben/p12177/vba-access/vba-recapitulatif-des-fonctions-jour-de-semaine" title="VBA : Récapitulatif des fonctions ‘jour de semaine’" target="_blank">récapitulatif</a> des fonctions.</p>
<p><strong>Les matrices</strong><br />
On souhaite donc passer de la matrice &lsquo;Weekday&rsquo; à la matrice &lsquo;Jour courant ou précédent&rsquo;</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Weekday(Date,VbDayOfWeek) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Jour courant ou précédent &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VbDayOfWeek &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VbDayOfWeek &nbsp; &nbsp; &nbsp; <br />
Jour Date &nbsp;L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D &nbsp; &nbsp; &nbsp; &nbsp;Jour date &nbsp;L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D<br />
Lundi &nbsp; &nbsp; &nbsp;1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; &nbsp; &nbsp; &nbsp;Lundi &nbsp; &nbsp; &nbsp;0 &nbsp;-6 &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1<br />
Mardi &nbsp; &nbsp; &nbsp;2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; &nbsp; &nbsp; &nbsp;Mardi &nbsp; &nbsp; -1 &nbsp; 0 &nbsp;-6 &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2<br />
Mercredi &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp;vers &nbsp;Mercredi &nbsp;-2 &nbsp;-1 &nbsp; 0 &nbsp;-6 &nbsp;-5 &nbsp;-4 &nbsp;-3<br />
Jeudi &nbsp; &nbsp; &nbsp;4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; &nbsp; &nbsp; &nbsp;Jeudi &nbsp; &nbsp; -3 &nbsp;-2 &nbsp;-1 &nbsp; 0 &nbsp;-6 &nbsp;-5 &nbsp;-4<br />
Vendredi &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; &nbsp; &nbsp; &nbsp;Vendredi &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp; 0 &nbsp;-6 &nbsp;-5<br />
Samedi &nbsp; &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; &nbsp; &nbsp; &nbsp;Samedi &nbsp; &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp; 0 &nbsp;-6<br />
Dimanche &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp;Dimanche &nbsp;-6 &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp; 0</div></div>
<p>&nbsp;<br />
On remarque que toutes les valeurs de la matrice finale sont les opposés + 1 de la matrice &lsquo;Weekday&rsquo;. Par exemple, si la date de référence est un lundi, le mardi sera 1 &#8211; 7 = -6, le dimanche sera 1 &#8211; 2 = -1 et pour les valeurs de la diagonale, la formule est 1 &#8211; 1 = 0.</p>
<p><strong>La fonction VBA</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> DateJourMemeOuPrecedent(<span style="color: #151B8D; font-weight: bold;">ByVal</span> DateRef <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Jour <span style="color: #151B8D; font-weight: bold;">As</span> VbDayOfWeek) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;DateJourMemeOuPrecedent = DateRef - CInt(Weekday(DateRef, Jour)) + 1<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Tester la fonction</strong><br />
La fonction suivante teste les 49 possibilités de la matrice et affiche les résultats dans la fenêtre ‘Exécution’ de l’éditeur VB.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestDateJourMemeOuPrecedent()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, r <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, dates(1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, k <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, NbErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> s <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, Jours <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span><br />
<br />
&nbsp; &nbsp;d = Int(Now())<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">While</span> Weekday(d) &lt;&gt; vbMonday: d = d + 1: Wend<br />
&nbsp; &nbsp;Jours = VBA.Array(<span style="color: #800000;">&quot;Dimanche&quot;</span>, <span style="color: #800000;">&quot;Lundi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Mardi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Mercredi&quot;</span>, <span style="color: #800000;">&quot;Jeudi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Vendredi&quot;</span>, <span style="color: #800000;">&quot;Samedi &nbsp;&quot;</span>)<br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Test DateJourMêmeOuPrecedent()&quot;</span> &amp; vbCrLf &amp; <span style="color: #800000;">&quot;----------------------&quot;</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7<br />
&nbsp; &nbsp; &nbsp; s = vbNullString<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;* Date de référence : &quot;</span> &amp; Format(d, <span style="color: #800000;">&quot;dddd dd/mm/yyyy&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> vbTab &amp; <span style="color: #800000;">&quot;Jour cherché&quot;</span> &amp; vbTab &amp; vbTab &amp; <span style="color: #800000;">&quot;Jour trouvé&quot;</span> &amp; vbTab &amp; vbTab &amp; <span style="color: #800000;">&quot;Conforme ?&quot;</span> &amp; vbCrLf &amp; _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vbTab &amp; <span style="color: #800000;">&quot;----------------------------------------------&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> k = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 6: dates(Weekday(d - k)) = d - k: <span style="color: #8D38C9; font-weight: bold;">Next</span> k<br />
&nbsp; &nbsp; &nbsp; dates(Weekday(d)) = d<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;r = DateJourMemeOuPrecedent(d, j)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s = s &amp; vbTab &amp; Jours(j - 1) &amp; vbTab &amp; vbTab &amp; Format(r, <span style="color: #800000;">&quot;ddd dd/mm/yyyy&quot;</span>) &amp; vbTab &amp; vbTab<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> r &lt;&gt; dates(j) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NbErr = NbErr + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = s &amp; <span style="color: #800000;">&quot;Erreur !&quot;</span> &amp; vbCrLf<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = s &amp; <span style="color: #800000;">&quot;Ok&quot;</span> &amp; vbCrLf<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> s<br />
&nbsp; &nbsp; &nbsp; d = d + 1<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;***&quot;</span> &amp; IIf(NbErr &gt; 0, NbErr, <span style="color: #800000;">&quot; Aucune&quot;</span>) &amp; <span style="color: #800000;">&quot; erreur(s) commise(s) ***&quot;</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
@+ pour la suite des épisodes ‘Weekday’.</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Date du jour de semaine précédent</title>
		<link>https://blog.developpez.com/philben/p11454/vba-access/date-du-jour-de-semaine-precedent</link>
		<comments>https://blog.developpez.com/philben/p11454/vba-access/date-du-jour-de-semaine-precedent#comments</comments>
		<pubDate>Fri, 26 Oct 2012 03:40:25 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Jour de semaine]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=551</guid>
		<description><![CDATA[Episode III de la saga &#8216;Weekday&#8217;. On souhaite obtenir cette fois-ci, la date d&#8217;un jour de semaine qui précède la date de référence. Préambule Je vous conseille de lire le premier billet de &#8216;Weekday&#8217; pour comprendre le principe des calculs. &#8230; <a href="https://blog.developpez.com/philben/p11454/vba-access/date-du-jour-de-semaine-precedent">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Episode <strong>III</strong> de la saga &lsquo;Weekday&rsquo;. On souhaite obtenir cette fois-ci, la date d&rsquo;un jour de semaine qui précède la date de référence.<br />
<span id="more-551"></span><br />
<strong>Préambule</strong><br />
Je vous conseille de lire le <a href="http://blog.developpez.com/philben/p11417/vba-access/date-du-jour-de-semaine-suivant" title="Date du jour de semaine suivant" target="_blank">premier billet de &lsquo;Weekday&rsquo;</a> pour comprendre le principe des calculs.<br />
Voir aussi le <a href="http://blog.developpez.com/philben/p12177/vba-access/vba-recapitulatif-des-fonctions-jour-de-semaine" title="VBA : Récapitulatif des fonctions ‘jour de semaine’" target="_blank">récapitulatif</a> des fonctions.</p>
<p><strong>Les matrices</strong><br />
On souhaite donc passer de la matrice &lsquo;Weekday&rsquo; à la matrice &lsquo;Jour précédent&rsquo;</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Weekday(Date,VbDayOfWeek) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Jour précédent &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VbDayOfWeek &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VbDayOfWeek &nbsp; &nbsp; &nbsp; <br />
Jour Date &nbsp; L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D &nbsp; &nbsp; &nbsp;Jour date &nbsp; L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D<br />
Lundi &nbsp; &nbsp; &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; &nbsp; &nbsp;Lundi &nbsp; &nbsp; &nbsp;-7 &nbsp;-6 &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1<br />
Mardi &nbsp; &nbsp; &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; &nbsp; &nbsp;Mardi &nbsp; &nbsp; &nbsp;-1 &nbsp;-7 &nbsp;-6 &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2<br />
Mercredi &nbsp; &nbsp;3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 vers Mercredi &nbsp; -2 &nbsp;-1 &nbsp;-7 &nbsp;-6 &nbsp;-5 &nbsp;-4 &nbsp;-3<br />
Jeudi &nbsp; &nbsp; &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; &nbsp; &nbsp;Jeudi &nbsp; &nbsp; &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp;-7 &nbsp;-6 &nbsp;-5 &nbsp;-4<br />
Vendredi &nbsp; &nbsp;5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; &nbsp; &nbsp;Vendredi &nbsp; -4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp;-7 &nbsp;-6 &nbsp;-5<br />
Samedi &nbsp; &nbsp; &nbsp;6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; &nbsp; &nbsp;Samedi &nbsp; &nbsp; -5 &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp;-7 &nbsp;-6<br />
Dimanche &nbsp; &nbsp;7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; &nbsp; &nbsp;Dimanche &nbsp; -6 &nbsp;-5 &nbsp;-4 &nbsp;-3 &nbsp;-2 &nbsp;-1 &nbsp;-7</div></div>
<p>&nbsp;<br />
On remarque que les lignes de la matrice &lsquo;Jour précédent&rsquo; correspondent aux lignes de la matrice &lsquo;Weekday&rsquo; avec un décalage de 1 jour.<br />
Par exemple, la ligne du lundi de &lsquo;Jour précédent&rsquo; est identique en valeur absolue à celle du dimanche de &lsquo;Weekday&rsquo;, celle du mardi est liée à lundi, etc&#8230;</p>
<p><strong>La solution</strong><br />
Il faut donc retrancher un jour à la date de référence avant de la passer à la fonction Weekday() : <code class="codecolorer vb blackboard"><span class="vb">Weekday(DateRef - 1, Jour)</span></code><br />
&nbsp;<br />
<strong>La fonction VBA</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> DateJourPrecedent(<span style="color: #151B8D; font-weight: bold;">ByVal</span> DateRef <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Jour <span style="color: #151B8D; font-weight: bold;">As</span> VbDayOfWeek) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;DateJourPrecedent = DateRef - Weekday(DateRef - 1, Jour)<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Tester la fonction</strong><br />
La fonction suivante teste les 49 possibilités de la matrice et affiche les résultats dans la fenêtre ‘Exécution’ de l’éditeur VB.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestDateJourPrecedent()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, r <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, dates(1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, k <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, NbErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> s <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, Jours <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span><br />
<br />
&nbsp; &nbsp;d = Int(Now())<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">While</span> Weekday(d) &lt;&gt; vbMonday: d = d + 1: Wend<br />
&nbsp; &nbsp;Jours = VBA.Array(<span style="color: #800000;">&quot;Dimanche&quot;</span>, <span style="color: #800000;">&quot;Lundi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Mardi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Mercredi&quot;</span>, <span style="color: #800000;">&quot;Jeudi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Vendredi&quot;</span>, <span style="color: #800000;">&quot;Samedi &nbsp;&quot;</span>)<br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Test DateJourPrecedent()&quot;</span> &amp; vbCrLf &amp; <span style="color: #800000;">&quot;----------------------&quot;</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7<br />
&nbsp; &nbsp; &nbsp; s = vbNullString<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;* Date de référence : &quot;</span> &amp; Format(d, <span style="color: #800000;">&quot;dddd dd/mm/yyyy&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> vbTab &amp; <span style="color: #800000;">&quot;Jour cherché&quot;</span> &amp; vbTab &amp; vbTab &amp; <span style="color: #800000;">&quot;Jour trouvé&quot;</span> &amp; vbTab &amp; vbTab &amp; <span style="color: #800000;">&quot;Conforme ?&quot;</span> &amp; vbCrLf &amp; _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vbTab &amp; <span style="color: #800000;">&quot;----------------------------------------------&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> k = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7: dates(Weekday(d - k)) = d - k: <span style="color: #8D38C9; font-weight: bold;">Next</span> k<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;r = DateJourPrecedent(d, j)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s = s &amp; vbTab &amp; Jours(j - 1) &amp; vbTab &amp; vbTab &amp; Format(r, <span style="color: #800000;">&quot;ddd dd/mm/yyyy&quot;</span>) &amp; vbTab &amp; vbTab<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> r &lt;&gt; dates(j) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NbErr = NbErr + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = s &amp; <span style="color: #800000;">&quot;Erreur !&quot;</span> &amp; vbCrLf<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = s &amp; <span style="color: #800000;">&quot;Ok&quot;</span> &amp; vbCrLf<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> s<br />
&nbsp; &nbsp; &nbsp; d = d + 1<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;***&quot;</span> &amp; IIf(NbErr &gt; 0, NbErr, <span style="color: #800000;">&quot; Aucune&quot;</span>) &amp; <span style="color: #800000;">&quot; erreur(s) commise(s) ***&quot;</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
@+ pour la suite des épisodes ‘Weekday’.</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Date du jour-même ou suivant</title>
		<link>https://blog.developpez.com/philben/p11438/vba-access/date-du-jour-meme-ou-suivant</link>
		<comments>https://blog.developpez.com/philben/p11438/vba-access/date-du-jour-meme-ou-suivant#comments</comments>
		<pubDate>Wed, 17 Oct 2012 20:19:34 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Jour de semaine]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=532</guid>
		<description><![CDATA[Episode II de la saga &#8216;Weekday&#8217;. On souhaite obtenir cette fois-ci, soit la date de référence si elle correspond au jour de semaine recherché soit la date suivante. La date retournée est donc ici supérieure ou égale à la date &#8230; <a href="https://blog.developpez.com/philben/p11438/vba-access/date-du-jour-meme-ou-suivant">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Episode <strong>II</strong> de la saga &lsquo;Weekday&rsquo;. On souhaite obtenir cette fois-ci, soit la date de référence si elle correspond au jour de semaine recherché soit la date suivante. La date retournée est donc ici <strong>supérieure ou égale</strong> à la date de référence contrairement au <a href="http://blog.developpez.com/philben/p11417/vba-access/date-du-jour-de-semaine-suivant" title="Date du jour de semaine suivant" target="_blank">premier billet de &lsquo;Weekday&rsquo;</a> où la date retournée était <strong>strictement supérieure</strong> à la date de référence.<br />
<span id="more-532"></span><br />
<strong>Préambule</strong><br />
Je vous conseille de lire le premier billet &lsquo;Weekday&rsquo; pour comprendre le principe des calculs.<br />
Voir aussi le <a href="http://blog.developpez.com/philben/p12177/vba-access/vba-recapitulatif-des-fonctions-jour-de-semaine" title="VBA : Récapitulatif des fonctions ‘jour de semaine’" target="_blank">récapitulatif</a> des fonctions.</p>
<p><strong>Les matrices</strong><br />
On souhaite donc passer de la matrice &lsquo;Weekday&rsquo; à la matrice &lsquo;Jour courant ou suivant&rsquo;</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Weekday(Date,VbDayOfWeek) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Jour courant ou suivant &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VbDayOfWeek &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; VbDayOfWeek &nbsp; &nbsp; &nbsp; &nbsp;<br />
Jour Date &nbsp; L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D &nbsp; &nbsp; &nbsp; Jour date &nbsp; L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D <br />
Lundi &nbsp; &nbsp; &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; &nbsp; &nbsp; Lundi &nbsp; &nbsp; &nbsp; 0 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 &nbsp; 5 &nbsp; 6 <br />
Mardi &nbsp; &nbsp; &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; &nbsp; &nbsp; Mardi &nbsp; &nbsp; &nbsp; 6 &nbsp; 0 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 &nbsp; 5 <br />
Mercredi &nbsp; &nbsp;3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp;vers Mercredi &nbsp; &nbsp;5 &nbsp; 6 &nbsp; 0 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 <br />
Jeudi &nbsp; &nbsp; &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; &nbsp; &nbsp; Jeudi &nbsp; &nbsp; &nbsp; 4 &nbsp; 5 &nbsp; 6 &nbsp; 0 &nbsp; 1 &nbsp; 2 &nbsp; 3 <br />
Vendredi &nbsp; &nbsp;5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; &nbsp; &nbsp; Vendredi &nbsp; &nbsp;3 &nbsp; 4 &nbsp; 5 &nbsp; 6 &nbsp; 0 &nbsp; 1 &nbsp; 2 <br />
Samedi &nbsp; &nbsp; &nbsp;6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; &nbsp; &nbsp; Samedi &nbsp; &nbsp; &nbsp;2 &nbsp; 3 &nbsp; 4 &nbsp; 5 &nbsp; 6 &nbsp; 0 &nbsp; 1 <br />
Dimanche &nbsp; &nbsp;7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; &nbsp; &nbsp; Dimanche &nbsp; &nbsp;1 &nbsp; 2 &nbsp; 3 &nbsp; 4 &nbsp; 5 &nbsp; 6 &nbsp; 0</div></div>
<p>&nbsp;<br />
On remarque que toutes les valeurs de la diagonale de la matrice finale sont égales à zéro alors qu&rsquo;elles étaient égales à 7 dans le premier billet. La matrice pivot reste donc la même sauf que le résultat de cette diagonale est égal à 7 (8 &#8211; 1) au lieu de zéro&#8230;<br />
Il faut donc réaliser une opération supplémentaire qui transformera la valeur 7 en zéro.</p>
<p><strong>La solution</strong><br />
Il existe plusieurs solutions mais j&rsquo;ai choisi d&rsquo;utiliser l&rsquo;opérateur modulo (MOD). Cet opérateur retourne le reste de la division de deux nombres. Exemples : 5 MOD 4 = 1 et 3 MOD 4 = 3.<br />
Dans notre cas, <strong>(8 &#8211; 1) MOD 7 = 0</strong> ce qui correspond bien à la valeur attendue dans la diagonale sans modifier les autres valeurs puisqu&rsquo;elles sont toutes inférieures à 7.</p>
<p><strong>La fonction VBA</strong></p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> DateJourMemeOuSuivant(<span style="color: #151B8D; font-weight: bold;">ByVal</span> DateRef <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Jour <span style="color: #151B8D; font-weight: bold;">As</span> VbDayOfWeek) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;DateJourMemeOuSuivant = DateRef + ((8 - CInt(Weekday(DateRef, Jour))) <span style="color: #151B8D; font-weight: bold;">Mod</span> 7)<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Tester la fonction</strong><br />
La fonction suivante teste les 49 possibilités de la matrice et affiche les résultats dans la fenêtre ‘Exécution’ de l’éditeur VB.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestDateJourMemeOuSuivant()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, r <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, dates(1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, k <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, NbErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> s <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, Jours <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span><br />
<br />
&nbsp; &nbsp;d = Int(Now())<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">While</span> Weekday(d) &lt;&gt; vbMonday: d = d + 1: Wend<br />
&nbsp; &nbsp;Jours = VBA.Array(<span style="color: #800000;">&quot;Dimanche&quot;</span>, <span style="color: #800000;">&quot;Lundi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Mardi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Mercredi&quot;</span>, <span style="color: #800000;">&quot;Jeudi &nbsp; &quot;</span>, <span style="color: #800000;">&quot;Vendredi&quot;</span>, <span style="color: #800000;">&quot;Samedi &nbsp;&quot;</span>)<br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Test DateJourMemeOuSuivant()&quot;</span> &amp; vbCrLf &amp; <span style="color: #800000;">&quot;----------------------------&quot;</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7<br />
&nbsp; &nbsp; &nbsp; s = vbNullString<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;* Date de référence : &quot;</span> &amp; Format(d, <span style="color: #800000;">&quot;dddd dd/mm/yyyy&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> vbTab &amp; <span style="color: #800000;">&quot;Jour cherché&quot;</span> &amp; vbTab &amp; vbTab &amp; <span style="color: #800000;">&quot;Jour trouvé&quot;</span> &amp; vbTab &amp; vbTab &amp; <span style="color: #800000;">&quot;Conforme ?&quot;</span> &amp; vbCrLf &amp; _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vbTab &amp; <span style="color: #800000;">&quot;----------------------------------------------&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> k = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 6: dates(Weekday(d + k)) = d + k: <span style="color: #8D38C9; font-weight: bold;">Next</span> k<br />
&nbsp; &nbsp; &nbsp; dates(Weekday(d)) = d<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> 7<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;r = DateJourMemeOuSuivant(d, j)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s = s &amp; vbTab &amp; Jours(j - 1) &amp; vbTab &amp; vbTab &amp; Format(r, <span style="color: #800000;">&quot;ddd dd/mm/yyyy&quot;</span>) &amp; vbTab &amp; vbTab<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> r &lt;&gt; dates(j) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NbErr = NbErr + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = s &amp; <span style="color: #800000;">&quot;Erreur !&quot;</span> &amp; vbCrLf<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s = s &amp; <span style="color: #800000;">&quot;Ok&quot;</span> &amp; vbCrLf<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> s<br />
&nbsp; &nbsp; &nbsp; d = d + 1<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;***&quot;</span> &amp; IIf(NbErr &gt; 0, NbErr, <span style="color: #800000;">&quot; Aucune&quot;</span>) &amp; <span style="color: #800000;">&quot; erreur(s) commise(s) ***&quot;</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
@+ pour la suite des épisodes ‘Weekday’.</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calculer la date de Pâques</title>
		<link>https://blog.developpez.com/philben/p11431/vba-access/calculer-la-date-de-paques</link>
		<comments>https://blog.developpez.com/philben/p11431/vba-access/calculer-la-date-de-paques#comments</comments>
		<pubDate>Sun, 14 Oct 2012 08:31:50 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Date]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=497</guid>
		<description><![CDATA[Je vous propose une fonction VBA qui détermine la date du Dimanche de Pâques pour toute année (de 325 à 9999) dont découlent les jours fériés mobiles en France. Elle complète également la fonction fournie dans la FAQ Access qui &#8230; <a href="https://blog.developpez.com/philben/p11431/vba-access/calculer-la-date-de-paques">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Je vous propose une fonction VBA qui détermine la date du Dimanche de Pâques pour toute année (de 325 à 9999) dont découlent les jours fériés mobiles en France.<br />
Elle complète également la <a href="http://demco.developpez.com/faqtmp/?page=dates#NbJourSsFeries" title="Comment savoir si un jour est ouvré ?" target="_blank">fonction fournie dans la FAQ Access</a> qui n&rsquo;est valide qu&rsquo;entre <strong>1982 et 2048</strong>.<br />
<span id="more-497"></span><br />
<strong>La fonction VBA</strong><br />
L&rsquo;objectif est la performance et non l&rsquo;aspect didactique des calculs&#8230;</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> Paques(<span style="color: #151B8D; font-weight: bold;">ByVal</span> an <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
<span style="color: #008000;">'Calcul de la date du dimanche de Pâques à partir de l'année 325<br />
</span><span style="color: #008000;">'Performance par million d'appel :<br />
</span><span style="color: #008000;">' &nbsp; - Entre 325 et 1582 et entre 1900 et 2099 &nbsp; =&gt; 1/4 de seconde<br />
</span><span style="color: #008000;">' &nbsp; - Année supérieure à 1582 hors 1900 - 2099 =&gt; 1/2 de seconde<br />
</span><span style="color: #008000;">'Philben - v1.0 - Free to use<br />
</span> &nbsp; <span style="color: #151B8D; font-weight: bold;">Dim</span> a <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, b <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, c <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, e <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, f <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> an &lt; 10000 <span style="color: #8D38C9; font-weight: bold;">Then</span> &nbsp; &nbsp;<span style="color: #008000;">'Limite supérieure des dates sous Access (31 décembre 9999)<br />
</span> &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Select</span> <span style="color: #8D38C9; font-weight: bold;">Case</span> an<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> 1900 <span style="color: #8D38C9; font-weight: bold;">To</span> 2099 &nbsp; &nbsp;<span style="color: #008000;">'Algorithme de Carter<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; a = (204 - 11 * (an <span style="color: #151B8D; font-weight: bold;">Mod</span> 19)) <span style="color: #151B8D; font-weight: bold;">Mod</span> 30 + 22<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Paques = DateSerial(an, 3, a + 6 + (a &gt; 49) - (an + an \ 4 + a + (a &gt; 49)) <span style="color: #151B8D; font-weight: bold;">Mod</span> 7)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> <span style="color: #8D38C9; font-weight: bold;">Is</span> &gt; 1582 &nbsp; &nbsp;<span style="color: #008000;">'Proposé en 1876 dans la revue Nature (dérivé de l'algorithme de Delambre)<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; a = an <span style="color: #151B8D; font-weight: bold;">Mod</span> 19: b = an \ 100: c = an <span style="color: #151B8D; font-weight: bold;">Mod</span> 100<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d = (19 * a + b - b \ 4 - (b - (b + 8) \ 25 + 1) \ 3 + 15) <span style="color: #151B8D; font-weight: bold;">Mod</span> 30<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e = (32 + 2 * (b <span style="color: #151B8D; font-weight: bold;">Mod</span> 4) + 2 * (c \ 4) - d - c <span style="color: #151B8D; font-weight: bold;">Mod</span> 4) <span style="color: #151B8D; font-weight: bold;">Mod</span> 7<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f = d + e - 7 * ((a + 11 * d + 22 * e) \ 451) + 114<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Paques = DateSerial(an, f \ 31, f <span style="color: #151B8D; font-weight: bold;">Mod</span> 31 + 1)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Case</span> <span style="color: #8D38C9; font-weight: bold;">Is</span> &gt; 324 &nbsp; &nbsp;<span style="color: #008000;">'Algorithme de Oudin pour les dates juliennes &lt; 1583 décrit par Claus Tondering<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp; a = (19 * (an <span style="color: #151B8D; font-weight: bold;">Mod</span> 19) + 15) <span style="color: #151B8D; font-weight: bold;">Mod</span> 30<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Paques = DateSerial(an, 3, 28 + a - (an + an \ 4 + a) <span style="color: #151B8D; font-weight: bold;">Mod</span> 7)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">Select</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
<strong>Pourquoi trois périodes de temps ?</strong><br />
La date de Pâques (Easter en anglais) a été fixée au concile de Nicée en 325 ce qui explique l&rsquo;année de départ de la fonction. De l&rsquo;an 325 à 1582, on utilise l&rsquo;algorithme de Oudin adapté au calendrier julien puis l&rsquo;algorithme de la revue <em>Nature</em> pour le calendrier grégorien actuel (réforme du 15 octobre 1582). Enfin, l&rsquo;algorithme de Carter est utilisé pour la période actuelle (1900 à 2099) compte tenu de sa rapidité de calcul.</p>
<p><strong>Performance</strong><br />
Pour <strong>un million d&rsquo;appels </strong>de la fonction :</p>
<ul>
<li>Compter 1/4 de seconde pour la période entre 325 et 1582 et entre 1900 et 2099;</li>
<li>Compter 1/2 seconde pour les années supérieures à 1582 (hors 1900 à 2099).</li>
</ul>
<p><strong>Vérification des résultats</strong><br />
La méthode retenue est de comparer les résultats avec d&rsquo;autres algorithmes.<br />
Concernant le calendrier julien j&rsquo;ai utilisé l&rsquo;algorithme de Delambre versus Oudin :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestPaquesJulien()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> a <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, b <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, c <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, e <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, an <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, bErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> d1 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, d2 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Test calendrier julien...&quot;</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> an = 325 <span style="color: #8D38C9; font-weight: bold;">To</span> 1582<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">'Delambre<br />
</span> &nbsp; &nbsp; &nbsp;a = an <span style="color: #151B8D; font-weight: bold;">Mod</span> 19<br />
&nbsp; &nbsp; &nbsp; b = an <span style="color: #151B8D; font-weight: bold;">Mod</span> 7<br />
&nbsp; &nbsp; &nbsp; c = an <span style="color: #151B8D; font-weight: bold;">Mod</span> 4<br />
&nbsp; &nbsp; &nbsp; d = (19 * a + 15) <span style="color: #151B8D; font-weight: bold;">Mod</span> 30<br />
&nbsp; &nbsp; &nbsp; e = (2 * c + 4 * b - d + 34) <span style="color: #151B8D; font-weight: bold;">Mod</span> 7<br />
&nbsp; &nbsp; &nbsp; d1 = DateSerial(an, 3, 22 + d + e)<br />
<br />
&nbsp; &nbsp; &nbsp; d2 = Paques(an)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> d1 &lt;&gt; d2 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> vbTab &amp; <span style="color: #800000;">&quot;-&gt; Date différente pour l'année &quot;</span> &amp; an &amp; <span style="color: #800000;">&quot; entre Delambre (&quot;</span> &amp; d1 &amp; <span style="color: #800000;">&quot;) et la fonction Paques (&quot;</span> &amp; d2 &amp; <span style="color: #800000;">&quot;)&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bErr = <span style="color: #00C2FF; font-weight: bold;">True</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #8D38C9; font-weight: bold;">For</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> an<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Fin - &quot;</span> &amp; IIf(bErr, <span style="color: #800000;">&quot;Ecart constaté !&quot;</span>, <span style="color: #800000;">&quot;Pas d'écart constaté&quot;</span>)<br />
<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
Pour le calendrier grégorien, j&rsquo;ai utilisé l&rsquo;algorithme de Oudin versus nos deux algorithmes (Carter et <em>Nature</em>) :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestPaquesGregorien()<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> c <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, g <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, k <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, l <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, m <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> an <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, bErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span>, d1 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, d2 <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span><br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Test calendrier grégorien...&quot;</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> an = 1583 <span style="color: #8D38C9; font-weight: bold;">To</span> 9999<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">'Oudin - http://www.smart.net/~mmontes/oudin.html<br />
</span> &nbsp; &nbsp; &nbsp;g = an <span style="color: #151B8D; font-weight: bold;">Mod</span> 19<br />
&nbsp; &nbsp; &nbsp; c = an \ 100<br />
&nbsp; &nbsp; &nbsp; k = (c - 17) \ 25<br />
&nbsp; &nbsp; &nbsp; i = (c - c \ 4 - (c - k) \ 3 + 19 * g + 15) <span style="color: #151B8D; font-weight: bold;">Mod</span> 30<br />
&nbsp; &nbsp; &nbsp; i = i - (i \ 28) * (1 - (i \ 28) * (29 \ (i + 1)) * ((21 - g) \ 11))<br />
&nbsp; &nbsp; &nbsp; j = (an + an \ 4 + i + 2 - c + c \ 4) <span style="color: #151B8D; font-weight: bold;">Mod</span> 7<br />
&nbsp; &nbsp; &nbsp; l = i - j<br />
&nbsp; &nbsp; &nbsp; m = 3 + (l + 40) \ 44<br />
&nbsp; &nbsp; &nbsp; d1 = DateSerial(an, m, l + 28 - 31 * (m \ 4))<br />
<br />
&nbsp; &nbsp; &nbsp; d2 = Paques(an)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> d1 &lt;&gt; d2 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> vbTab &amp; <span style="color: #800000;">&quot;-&gt; Date différente pour l'année &quot;</span> &amp; an &amp; <span style="color: #800000;">&quot; entre Oudin (&quot;</span> &amp; d1 &amp; <span style="color: #800000;">&quot;) et la fonction Paques (&quot;</span> &amp; d2 &amp; <span style="color: #800000;">&quot;)&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bErr = <span style="color: #00C2FF; font-weight: bold;">True</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #8D38C9; font-weight: bold;">For</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> an<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Fin - &quot;</span> &amp; IIf(bErr, <span style="color: #800000;">&quot;Ecart constaté !&quot;</span>, <span style="color: #800000;">&quot;Pas d'écart constaté&quot;</span>)<br />
<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
On peut également vérifier que la date de Pâques calculée soit comprise <strong>entre le 22 mars et le 25 avril</strong> et qu&rsquo;elle soit bien un <strong>dimanche</strong>. Comme la fonction VBA Weekday() n&rsquo;est adaptée qu&rsquo;au calendrier grégorien, il faut donc ajouter un algorithme qui détermine les jours de semaine du comput julien (années &lt; 1583 dans notre cas).<br />
La fonction de test et JourSemaineJulien() :</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> TestPaques()<br />
<span style="color: #008000;">'Quelque soit l'année, vérifier que la date de Pâques est bien :<br />
</span><span style="color: #008000;">' &nbsp; - comprise entre le 22 mars et 25 avril<br />
</span><span style="color: #008000;">' &nbsp; - un dimanche<br />
</span> &nbsp; Const cMinAn <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = 325<br />
&nbsp; &nbsp;Const cMaxAn <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = 9999<br />
&nbsp; &nbsp;Const cMaxMoisJour <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = 425<br />
&nbsp; &nbsp;Const cMinMoisJour <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = 322<br />
&nbsp; &nbsp;Const cMinCalendrierGregorien <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span> = 1583<br />
&nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Dim</span> p <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, md <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = cMinAn <span style="color: #8D38C9; font-weight: bold;">To</span> cMaxAn<br />
&nbsp; &nbsp; &nbsp; p = Paques(i)<br />
&nbsp; &nbsp; &nbsp; md = Month(p) * 100 + Day(p)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> i &lt; cMinCalendrierGregorien <span style="color: #8D38C9; font-weight: bold;">Then</span> j = JourSemaineJulien(p) <span style="color: #8D38C9; font-weight: bold;">Else</span> j = Weekday(p)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> j &lt;&gt; vbSunday <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Erreur ! Une date de Pâques n'est pas un dimanche : &quot;</span>, p<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #8D38C9; font-weight: bold;">For</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> md &lt; cMinMoisJour <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Erreur ! Une date de Pâques est inférieure au 22 mars : &quot;</span>, p<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #8D38C9; font-weight: bold;">For</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">ElseIf</span> md &gt; cMaxMoisJour <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Erreur ! Une date de Pâques est supérieure au 25 avril : &quot;</span>, p<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #8D38C9; font-weight: bold;">For</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> i &gt; cMaxAn <span style="color: #8D38C9; font-weight: bold;">Then</span> Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Aucune erreur détectée entre &quot;</span> &amp; cMinAn &amp; <span style="color: #800000;">&quot; et &quot;</span> &amp; cMaxAn<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span><br />
<br />
<span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Function</span> JourSemaineJulien(<span style="color: #151B8D; font-weight: bold;">ByVal</span> LaDate <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>) <span style="color: #151B8D; font-weight: bold;">As</span> VbDayOfWeek<br />
<span style="color: #008000;">'Formule de Zeller - http://www.vendredi13.us/ZellerMethode.html<br />
</span> &nbsp; <span style="color: #151B8D; font-weight: bold;">Dim</span> a <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, m <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, s <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, y <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>, z <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
<br />
&nbsp; &nbsp;y = Year(LaDate): m = Month(LaDate)<br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> m &lt; 3 <span style="color: #8D38C9; font-weight: bold;">Then</span> m = 12 + m: y = y - 1<br />
&nbsp; &nbsp;s = y \ 100: a = y - s * 100<br />
<br />
&nbsp; &nbsp;<span style="color: #008000;">'Comput julien : de 100 (1ère date Access : 1/1/100) à 1582<br />
</span> &nbsp; z = (Day(LaDate) + Int(2.6 * (m + 1)) + a + a \ 4 + 5 - s) <span style="color: #151B8D; font-weight: bold;">Mod</span> 7<br />
<br />
&nbsp; &nbsp;<span style="color: #008000;">'comput grégorien : &gt; 1582<br />
</span> &nbsp; <span style="color: #008000;">'z = (Day(LaDate) + Int(2.6 * (m + 1)) + a + a \ 4 + s \ 4 - 2 * s) Mod 7<br />
</span> &nbsp; <span style="color: #008000;">'If z &lt; 0 Then z = 7 + (z Mod 7) 'inutile entre 100 et 1582<br />
</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> z = 0 <span style="color: #8D38C9; font-weight: bold;">Then</span> z = vbSaturday &nbsp; <span style="color: #008000;">'alignement sur VbDayOfWeek<br />
</span><br />
&nbsp; &nbsp;JourSemaineJulien = z<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>&nbsp;<br />
J&#039;ai également comparé les résultats d&#039;un échantillon d&#039;années avec ceux de ce <a href="http://pgj.pagesperso-orange.fr/paques.htm" title="Calcul de la date de PAQUES et Eléments du Comput ecclésiastique" target="_blank">site web très intéressant</a> sans constater d&rsquo;erreur.</p>
<p><strong>Exemples</strong><br />
A copier dans la fenêtre &lsquo;Exécution&rsquo; de VBE puis mettre le focus sur la ligne à exécuter et taper sur &lsquo;Entrée&rsquo;.</p>
<div class="codecolorer-container vb blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008000;">'Dimanche de Pâques pour l'année en cours<br />
</span>?Paques(Year(<span style="color: #F660AB; font-weight: bold;">Date</span>())) <span style="color: #008000;">'pour 2012 -&gt; 08/04/2012<br />
</span><span style="color: #008000;">'An 1000 - Calendrier julien<br />
</span>?Paques(1000) <span style="color: #008000;">'31/03/1000<br />
</span>?Paques(3000) '13/04/3000</div></div>
<p>&nbsp;<br />
<strong>Remarque</strong><br />
Bien que l&rsquo;année grégorienne (365.2425 jours en moyenne) soit plus courte que l&rsquo;année julienne (365.25 jours en moyenne) , elle est encore trop longue par rapport à l&rsquo;année tropique (365.2422 jours en moyenne). De plus, le raccourcissement de l&rsquo;année tropique et l&rsquo;allongement  du jour au fil des millénaires nécessiteront des ajustements qui rendront caduques les algorithmes utilisés&#8230;</p>
<p><strong>Références</strong><br />
Cette <a href="http://www.imcce.fr/~procher/Presentations/lesdatesdepaques.pdf" title="Les calculs des dates de Pâques" target="_blank">présentation en français au format PDF</a> de P. Rocher (Observatoire de Paris) fait un point détaillé sur les dates de Pâques, les méthodes de calcul et présente l&rsquo;algorithme de la revue <em>Nature</em> (page 15).<br />
Le site <a href="http://www.vendredi13.us/A5_fr.html" title="Méthodes de calcul du jour de Pâques" target="_blank">vendredi13.us</a> dont le design pique un peu les yeux (!) est d&rsquo;une richesse exceptionnelle sur le vendredi 13 et les dates de Pâques, le tout en français ! Il expose très clairement de nombreuses méthodes de calcul du jour de Pâques dont Gauss, Delambre, Oudin, Reints, Zeller&#8230;</p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calcul direct de la date de Pâques en SQL</title>
		<link>https://blog.developpez.com/philben/p11430/sql-access/calcul-direct-de-la-date-de-paques-en-sql</link>
		<comments>https://blog.developpez.com/philben/p11430/sql-access/calcul-direct-de-la-date-de-paques-en-sql#comments</comments>
		<pubDate>Sat, 13 Oct 2012 19:44:07 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[SQL - Ms Access]]></category>
		<category><![CDATA[Algorithme]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Requête SQL]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=469</guid>
		<description><![CDATA[La requête paramétrée suivante calcule directement la date du Dimanche de Pâques entre 1900 et 2099 sans faire intervenir une fonction VBA personnelle grâce à une formule dérivée de l&#8217;algorithme de Carter. La date des 3 jours fériés mobiles français &#8230; <a href="https://blog.developpez.com/philben/p11430/sql-access/calcul-direct-de-la-date-de-paques-en-sql">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>La requête paramétrée suivante calcule directement la date du Dimanche de Pâques entre <strong>1900 et 2099</strong> sans faire intervenir une fonction VBA personnelle grâce à une formule dérivée de l&rsquo;algorithme de <strong>Carter</strong>.<br />
La date des 3 jours fériés mobiles français (Lundi de Pâques, Ascension et Lundi de Pentecôte) est également formulée, bien que le lundi de  Pentecôte ne soit plus chômé depuis 2005 (journée de solidarité).<br />
<span id="more-469"></span><br />
<strong>La requête SQL pour ACCESS</strong></p>
<div class="codecolorer-container sql blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">PARAMETERS Annee Short;<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> IIF<span style="color: #66cc66;">&#40;</span>Annee <span style="color: #993333; font-weight: bold;">BETWEEN</span> <span style="color: #cc66cc;">1900</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #cc66cc;">2099</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DateSerial<span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Annee<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">204</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">11</span> <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>Annee <span style="color: #993333; font-weight: bold;">MOD</span> <span style="color: #cc66cc;">19</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">MOD</span> <span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">28</span> <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>Annee <span style="color: #993333; font-weight: bold;">MOD</span> <span style="color: #cc66cc;">19</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #66cc66;">&#40;</span>Annee <span style="color: #66cc66;">+</span> Annee \ <span style="color: #cc66cc;">4</span> <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">204</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">11</span> <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span>Annee <span style="color: #993333; font-weight: bold;">MOD</span> <span style="color: #cc66cc;">19</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">MOD</span> <span style="color: #cc66cc;">30</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">22</span> <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>Annee <span style="color: #993333; font-weight: bold;">MOD</span> <span style="color: #cc66cc;">19</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">MOD</span> <span style="color: #cc66cc;">7</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Dimanche de Pâques<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Dimanche de Pâques<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Lundi de Pâques<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Dimanche de Pâques<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">39</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Ascension<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span>Dimanche de Pâques<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">50</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>Lundi de Pentecôte<span style="color: #66cc66;">&#93;</span></div></div>
<p>&nbsp;<br />
<strong>Remarques</strong><br />
Si vous souhaitez extrapoler la formule à d&rsquo;autres SQL, il faut savoir que sous Access :</p>
<ul>
<li>True = -1 donc <code class="codecolorer sql blackboard"><span class="sql"><span style="color: #66cc66;">&#40;</span>Annee <span style="color: #993333; font-weight: bold;">MOD</span> <span style="color: #cc66cc;">19</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></span></code> retourne -1 si vrai;</li>
<li>L&rsquo;opérateur &lsquo;\&rsquo; de <code class="codecolorer sql blackboard"><span class="sql">Annee \ <span style="color: #cc66cc;">4</span></span></code> retourne seulement la partie entière de la division;</li>
<li>Attention aussi à la priorité des opérateurs&#8230;
</ul>
<p>Il n&rsquo;est pas possible non plus de copier directement la formule dans une fonction VBA car elle utilise des opérateurs spécifiques SQL (Between et In).<br />
&nbsp;<br />
<strong>Exemples</strong><br />
Au lancement de la requête, un popup demande de saisir l&rsquo;année souhaitée.</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Dimanche &nbsp; &nbsp;Lundi de <br />
de Pâques &nbsp; Pâques &nbsp; &nbsp; &nbsp;Ascension &nbsp; Lundi de Pentecôte<br />
15/04/1900 &nbsp;16/04/1900 &nbsp;24/05/1900 &nbsp;04/06/1900<br />
18/04/1954 &nbsp;19/04/1954 &nbsp;27/05/1954 &nbsp;07/06/1954<br />
08/04/2012 &nbsp;09/04/2012 &nbsp;17/05/2012 &nbsp;28/05/2012<br />
21/04/2019 &nbsp;22/04/2019 &nbsp;30/05/2019 &nbsp;10/06/2019<br />
12/04/2099 &nbsp;13/04/2099 &nbsp;21/05/2099 &nbsp;01/06/2099</div></div>
<p>Si l&rsquo;année est en dehors de la plage 1900 &#8211; 2099, NULL est renvoyé pour chaque colonne.</p>
<p><strong>A noter</strong><br />
La formule pour le <a href="http://access.developpez.com/faq/?page=TAManip#NbJourOuvr" title="Comment calculer le nombre de jours ouvrables entre deux dates ?" target="_blank">calcul des jours ouvrables</a> de la <strong>FAQ Access</strong> n&rsquo;est valable que pour la période comprise entre <strong>1982 et 2048.</strong></p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
