<?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; Jour de semaine</title>
	<atom:link href="https://blog.developpez.com/philben/ptag/joursemaine/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 : S&#8217;agit-il d&#8217;un jour ouvré non chômé ?</title>
		<link>https://blog.developpez.com/philben/p12194/vba-access/sagit-il-dun-jour-ouvre-non-chome</link>
		<comments>https://blog.developpez.com/philben/p12194/vba-access/sagit-il-dun-jour-ouvre-non-chome#comments</comments>
		<pubDate>Tue, 27 Aug 2013 21:12:21 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Code VBA]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Jour de semaine]]></category>
		<category><![CDATA[Jour férié]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=683</guid>
		<description><![CDATA[La fonction VBA présentée dans ce billet permet de savoir si la date passée en argument correspond à un jour ouvré (du lundi au vendredi) non chômé (non férié) en France. Dépendances de la fonction Nécessite la fonction EstJourFerie() et &#8230; <a href="https://blog.developpez.com/philben/p12194/vba-access/sagit-il-dun-jour-ouvre-non-chome">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>La fonction VBA présentée dans ce billet permet de savoir si la date passée en argument correspond à un jour ouvré (du lundi au vendredi) non chômé (non férié) en France.<br />
<span id="more-683"></span><br />
<strong>Dépendances de la fonction</strong><br />
Nécessite la fonction <a href="http://blog.developpez.com/philben/p11458/vba-access/sagit-il-dun-jour-ferie" title="S’agit-il d’un jour férié ?" target="_blank">EstJourFerie()</a> et la fonction <a href="http://blog.developpez.com/philben/p11431/vba-access/calculer-la-date-de-paques" title="Calculer la date de Pâques" target="_blank">Paques()</a>.</p>
<p><strong>Le code de 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> EstJourOuvreNonChome(<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;">Optional</span> <span style="color: #151B8D; font-weight: bold;">ByVal</span> EstPentecoteChome <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span> = <span style="color: #00C2FF; font-weight: bold;">True</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span><br />
<span style="color: #008000;">'Retourne Vrai si la date est un jour ouvré (du lundi au vendredi) NON férié (non chômé)<br />
</span><span style="color: #008000;">'philben - free to use<br />
</span> &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> Weekday(laDate, vbMonday) &lt; 6 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> EstJourFerie(laDate, EstPentecoteChome) <span style="color: #8D38C9; font-weight: bold;">Then</span> EstJourOuvreNonChome = <span style="color: #00C2FF; font-weight: bold;">True</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>Explications</strong><br />
La fonction retourne <strong>Vrai</strong> si la date passée en argument correspond à un jour ouvré non chômé.<br />
Cette fonction admet un deuxième paramètre qui permet de définir si le jour de la Pentecôte est chômé (Vrai par défaut) ou non.</p>
<p>La ligne de code <strong>&lsquo;If Weekday(&#8230;)&rsquo;</strong> filtre les jours de semaine pour ne conserver que les jours ouvrés (du lundi au vendredi). On utilise ensuite la fonction EstJourFerie(&#8230;) qui retourne Vrai s&rsquo;il s&rsquo;agit d&rsquo;un jour férié.</p>
<p><strong>Vérification des résultats</strong><br />
La fonction suivante compte les jours ouvrés non chômés pour différentes années puis compare le résultat avec la valeur attendue (données issues de <a href="http://www.joursouvres.fr/" target="_blank">ce site</a>).</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> TestEstJourOuvreNonChome()<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>, d <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, l <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>, NbErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
<br />
&nbsp; &nbsp;v = Array(2010, 254, 2011, 253, 2012, 253, 2013, 251, 2014, 251, 2015, 252, 2016, 253, 2021, 254)<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> 2<br />
&nbsp; &nbsp; &nbsp; d = DateSerial(v(i), 1, 1)<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">While</span> Year(d) = v(i)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> EstJourOuvreNonChome(d) <span style="color: #8D38C9; font-weight: bold;">Then</span> l = l + 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d = d + 1<br />
&nbsp; &nbsp; &nbsp; Wend<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> l &lt;&gt; v(i + 1) <span style="color: #8D38C9; font-weight: bold;">Then</span> NbErr = NbErr + 1<br />
&nbsp; &nbsp; &nbsp; Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Nb jours ouvrés non chômés en &quot;</span> &amp; v(i), <span style="color: #800000;">&quot; -&gt; Attendu : &quot;</span> &amp; v(i + 1), <span style="color: #800000;">&quot; -&gt; Obtenu : &quot;</span> &amp; l, IIf(l &lt;&gt; v(i + 1), <span style="color: #800000;">&quot;*** ERREUR ***&quot;</span>, <span style="color: #800000;">&quot;&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; l = 0<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;Nombre d'erreur(s) : &quot;</span> &amp; NbErr<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>Remarque : Le lundi de Pentecôte est considéré comme chômé ici.</p>
<p>@+</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>VBA : Date d&#8217;un premier jour de semaine du mois</title>
		<link>https://blog.developpez.com/philben/p12179/vba-access/vba-date-dun-premier-jour-de-semaine-du-mois</link>
		<comments>https://blog.developpez.com/philben/p12179/vba-access/vba-date-dun-premier-jour-de-semaine-du-mois#comments</comments>
		<pubDate>Thu, 15 Aug 2013 13:56:40 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></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=654</guid>
		<description><![CDATA[Episode VI de la saga &#8216;Weekday&#8217;. Cette fois-ci, on introduit la notion de mois puisque l&#8217;on souhaite obtenir la date d&#8217;un premier jour de semaine du mois de la date de référence. Quelle est la date du premier dimanche du &#8230; <a href="https://blog.developpez.com/philben/p12179/vba-access/vba-date-dun-premier-jour-de-semaine-du-mois">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Episode <strong>VI</strong> de la saga &lsquo;Weekday&rsquo;. Cette fois-ci, on introduit la notion de <strong>mois</strong> puisque l&rsquo;on souhaite obtenir la date d&rsquo;un premier jour de semaine du mois de la date de référence. Quelle est la date du premier dimanche du mois ? La date du premier lundi du mois ?<br />
<span id="more-654"></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>La fonction VBA</strong><br />
Nécessite la fonction <a href="http://blog.developpez.com/philben/p11417/vba-access/date-du-jour-de-semaine-suivant" title="Date du jour de semaine suivant">DateJourSuivant()</a></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> DateJourDebutMois(<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;DateJourDebutMois = DateJourSuivant(DateRef - Day(DateRef), Jour)<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>Pour atteindre rapidement le dernier jour du mois précédent, on soustrait à la date de référence son jour du mois. Par exemple le 15 août &#8211; 15 donnera le 31 juillet.<br />
On recherche ensuite le jour de semaine souhaité qui suit le dernier jour du mois précédent grâce à notre fonction DateJourSuivant().</p>
<p><strong>Tester la fonction</strong><br />
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> TestDateJourDebutMois()<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>, dm <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Date</span>, djdm <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>, NbErr <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span><br />
<br />
&nbsp; &nbsp;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;Test DateJourDebutMois()&quot;</span> &amp; vbCrLf &amp; <span style="color: #800000;">&quot;----------------------&quot;</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;Debug.<span style="color: #151B8D; font-weight: bold;">Print</span> <span style="color: #800000;">&quot;* Semaine de référence 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 />
<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; dm = DateSerial(Year(d), Month(d), 1)<br />
&nbsp; &nbsp; &nbsp; j = i <span style="color: #151B8D; font-weight: bold;">Mod</span> 7 + 1<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">While</span> Weekday(dm) &lt;&gt; j: dm = dm + 1: Wend<br />
<br />
&nbsp; &nbsp; &nbsp; djdm = DateJourDebutMois(d, Weekday(d))<br />
&nbsp; &nbsp; &nbsp; s = s &amp; vbTab &amp; <span style="color: #800000;">&quot;-&gt; Jour recherché : &quot;</span> &amp; Format(dm, <span style="color: #800000;">&quot;ddd dd/mm/yyyy&quot;</span>) &amp; <span style="color: #800000;">&quot; - Jour trouvé : &quot;</span> &amp; Format(djdm, <span style="color: #800000;">&quot;ddd dd/mm/yyyy&quot;</span>) &amp; vbCrLf<br />
&nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> djdm &lt;&gt; dm <span style="color: #8D38C9; font-weight: bold;">Then</span> NbErr = NbErr + 1<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> s &amp; <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>VBA : S&#8217;agit-il d&#8217;un jour de week-end ?</title>
		<link>https://blog.developpez.com/philben/p12178/vba-access/vba-sagit-il-dun-jour-de-week-end</link>
		<comments>https://blog.developpez.com/philben/p12178/vba-access/vba-sagit-il-dun-jour-de-week-end#comments</comments>
		<pubDate>Thu, 15 Aug 2013 11:55:58 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></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=637</guid>
		<description><![CDATA[Une petite fonction qui permet de savoir si la date passée en argument correspond à une jour de week-end ou non. La fonction VBA Public Function EstWeekEnd(ByVal dDate As Date) As Boolean &#160; &#160;If Weekday(dDate, vbMonday) &#62; 5 Then EstWeekEnd &#8230; <a href="https://blog.developpez.com/philben/p12178/vba-access/vba-sagit-il-dun-jour-de-week-end">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Une petite fonction qui permet de savoir si la date passée en argument correspond à une jour de week-end ou non.<br />
<span id="more-637"></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;"><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> EstWeekEnd(<span style="color: #151B8D; font-weight: bold;">ByVal</span> dDate <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> <span style="color: #F660AB; font-weight: bold;">Boolean</span><br />
&nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> Weekday(dDate, vbMonday) &gt; 5 <span style="color: #8D38C9; font-weight: bold;">Then</span> EstWeekEnd = <span style="color: #00C2FF; font-weight: bold;">True</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 />
Elle retourne <strong>Vrai</strong> si la date est un samedi ou un dimanche sinon elle renvoie <strong>Faux</strong> par défaut.</p>
<p><strong>Principe</strong><br />
On impose à &lsquo;Weekday()&rsquo; que le 1er jour de semaine est le lundi (constante &lsquo;vbMonday&rsquo;) et non le dimanche par défaut. Ainsi, le 6ème et le 7ème jour sont le samedi et le dimanche.</p>
<p><strong>Exemples</strong><br />
Dans la fenêtre Exécution 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">?EstWeekEnd(#2013/8/16#) <span style="color: #008000;">'Vendredi 16 août 2013 : Faux<br />
</span>?EstWeekEnd(#2013/8/17#) <span style="color: #008000;">'Samedi 17 août 2013 : Vrai<br />
</span>?EstWeekEnd(#2013/8/18#) <span style="color: #008000;">'Dimanche 18 août 2013 : Vrai<br />
</span>?EstWeekEnd(#2013/8/19#) 'Lundi 19 août 2013 : Faux</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 : Récapitulatif des fonctions &#8216;jour de semaine&#8217;</title>
		<link>https://blog.developpez.com/philben/p12177/vba-access/vba-recapitulatif-des-fonctions-jour-de-semaine</link>
		<comments>https://blog.developpez.com/philben/p12177/vba-access/vba-recapitulatif-des-fonctions-jour-de-semaine#comments</comments>
		<pubDate>Thu, 15 Aug 2013 11:15:36 +0000</pubDate>
		<dc:creator><![CDATA[philben]]></dc:creator>
				<category><![CDATA[VBA - Ms Access]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[Jour de semaine]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/philben/?p=623</guid>
		<description><![CDATA[Dans ces exemples, on recherche la date d&#8217;un mercredi. Le mercredi avec une croix rouge indique le mercredi retourné selon la fonction utilisée et le jour de la date de référence. DateJourSemaineEnCours() renvoie toujours la date du mercredi de la &#8230; <a href="https://blog.developpez.com/philben/p12177/vba-access/vba-recapitulatif-des-fonctions-jour-de-semaine">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><img src="http://philben.developpez.com/Weekday.png" alt="Recapitulatif weekday" /><br />
<span id="more-623"></span><br />
Dans ces exemples, on recherche la date d&rsquo;un mercredi. Le mercredi avec une croix rouge indique le mercredi retourné selon la fonction utilisée et le jour de la date de référence.</p>
<ul>
<li><a href="http://blog.developpez.com/philben/p11481/vba-access/date-dun-jour-de-la-semaine-courante" title="Date d’un jour de la semaine courante" target="_blank">DateJourSemaineEnCours()</a> renvoie toujours la date du mercredi de la semaine quelque soit le jour de la date de référence</li>
<li><a href="http://blog.developpez.com/philben/p11470/vba-access/date-du-jour-meme-ou-precedent" title="Date du jour-même ou précédent" target="_blank">DateJourMemeOuPrecedent()</a> renvoie la date du mercredi qui précède la date de référence ou la date de référence elle-même s&rsquo;il s&rsquo;agit d&rsquo;un mercredi</li>
<li><a href="http://blog.developpez.com/philben/p11454/vba-access/date-du-jour-de-semaine-precedent" title="Date du jour de semaine précédent" target="_blank">DateJourPrecedent()</a> renvoie la date du mercredi qui précède la date de référence</li>
<li><a href="http://blog.developpez.com/philben/p11438/vba-access/date-du-jour-meme-ou-suivant" title="Date du jour-même ou suivant" target="_blank">DateJourMemeOuSuivant()</a> renvoie la date du mercredi qui suit la date de référence ou la date de référence elle-même s&rsquo;il s&rsquo;agit d&rsquo;un mercredi</li>
<li><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">DateJourSuivant()</a> renvoie la date du mercredi qui suit la date de référence</li>
</ul>
<p><strong>Remarque</strong><br />
Les fonctions décrites peuvent provoquer une erreur (dépassement de capacité &#8211; Erreur n°6) :</p>
<ul>
<li>si on passe une date proche du 1er janvier de l&rsquo;an 100 aux fonctions ayant un balayage arrière</li>
<li>si on passe une date proche du 31 décembre 9999 aux fonctions ayant un balayage avant</li>
</ul>
<p>En effet, on peut dépasser dans ces cas, la plage des dates autorisées par Access. Pour ne pas lever cette erreur, vous pouvez introduire un gestionnaire d&rsquo;erreur dans les fonctions.</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>Date du jour de semaine suivant</title>
		<link>https://blog.developpez.com/philben/p11417/vba-access/date-du-jour-de-semaine-suivant</link>
		<comments>https://blog.developpez.com/philben/p11417/vba-access/date-du-jour-de-semaine-suivant#comments</comments>
		<pubDate>Sun, 07 Oct 2012 21:22:47 +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=416</guid>
		<description><![CDATA[Premier billet de la saga &#8216;Weekday&#8217; qui répond ici aux questions suivantes : &#171;&#160;Quelle est la date du prochain lundi ? du prochain jeudi ? &#8230;&#160;&#187;. Ce billet présente aussi la fonction VBA &#8216;WeekDay()&#8217; et sa matrice des résultats qui &#8230; <a href="https://blog.developpez.com/philben/p11417/vba-access/date-du-jour-de-semaine-suivant">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Premier billet de la saga &lsquo;Weekday&rsquo; qui répond ici aux questions suivantes : <strong>&laquo;&nbsp;Quelle est la date du prochain lundi ? du prochain jeudi ? &#8230;&nbsp;&raquo;</strong>.<br />
Ce billet présente aussi la fonction VBA &lsquo;WeekDay()&rsquo; et sa matrice des résultats qui sert de base à la compréhension des algorithmes basés sur les jours de semaine.<br />
<span id="more-416"></span><br />
<strong>La fonction VBA Weekday()</strong><br />
Cette fonction retourne le n° du jour de semaine de la date passée en argument.<br />
Elle comprend deux arguments dont le deuxième est facultatif :</p>
<ul>
<li>Une expression qui représente une date. Idéalement, passer une variable de type Date.</li>
<li>Une constante numérique qui représente le <strong>premier jour de la semaine</strong></li>
</ul>
<p>Si un NULL est passé comme date, Weekday renvoie NULL.</p>
<p>Le deuxième argument accepte les constantes nommées suivantes de l&rsquo;énumération <strong>VbDayOfWeek </strong>:</p>
<ul>
<li>vbUseSystem (0)</li>
<li>vbSunday    (1) &#8211; Dimanche</li>
<li>vbMonday    (2) &#8211; Lundi</li>
<li>vbTuesday   (3) &#8211; Mardi</li>
<li>vbWednesday (4) &#8211; Mercredi</li>
<li>vbThursday  (5) &#8211; Jeudi</li>
<li>vbFriday    (6) &#8211; Vendredi</li>
<li>vbSaturday  (7) &#8211; Samedi</li>
</ul>
<p>Par défaut, le premier jour de la semaine est <strong>dimanche </strong>pour correspondre au calendrier U.S (non ISO)&#8230;<br />
La solution &lsquo;vbUseSystem&rsquo; semble intéressante sur le papier mais, en pratique, le paramétrage de Windows n&rsquo;est pas sous notre contrôle et s&rsquo;appuyer dessus est parfois risqué. Si je n&rsquo;avais qu&rsquo;une devise avec MS Access, elle serait : &laquo;&nbsp;Moins j&rsquo;ai de dépendances externes (références, paramètres windows,&#8230;), mieux je me porte !&nbsp;&raquo;</p>
<p>La fonction retourne une valeur numérique entre 1 et 7 qui indique le <strong>n° du jour par rapport au premier jour de la semaine</strong>. L&rsquo;aide d&rsquo;Access est relativement ambiguë à ce niveau car elle laisse croire que, quelque soit la valeur du deuxième argument si le jour de la date est dimanche par exemple la fonction renvoie systématiquement la constante vbSunday soit la valeur 1, ce qui n&rsquo;est pas le cas.<br />
En effet, la fonction retourne <strong>la position du jour </strong>par rapport au premier jour de la semaine.<br />
Par exemple,  <code class="codecolorer vb blackboard"><span class="vb">?Weekday(#2012/10/7#)</span></code> retourne la valeur 1 qui correspond bien à vbSunday et <code class="codecolorer vb blackboard"><span class="vb">?Weekday(#2012/10/7#, vbMonday)</span></code> retourne 7 car dimanche est le 7ème jour de la semaine lorsqu&rsquo;elle commence par lundi. </p>
<p><strong>A savoir si vous travaillez avec des millisecondes&#8230;</strong><br />
Les fonctions VBA de date dont Weekday() considèrent qu&rsquo;une date avec une heure supérieure à 23h59mn59s et 499ms a basculé sur le jour suivant.<br />
Par exemple, <code class="codecolorer vb blackboard"><span class="vb">?weekday(86399.499/86400)</span></code> retourne 7 soit samedi 30/12/1899 et <code class="codecolorer vb blackboard"><span class="vb">?weekday(86399.500/86400)</span></code> retourne 1 soit dimanche 31/12/1899 (86400 = nombre de secondes en 24h).<br />
Par commodité, on utilise souvent Int() pour extraire la date d&rsquo;une variable date/heure. Si des millisecondes sont présentes, elle peut rendre également un résultat différent :</p>
<ul>
<li>?#2012/10/7#+86399.499/86400 &lsquo;-&gt; 07/10/2012 23:59:59</li>
<li>?#2012/10/7#+86399.500/86400 &lsquo;-&gt; 08/10/2012</li>
<li>?<strong>Int</strong>(#2012/10/7#+86399.500/86400) &lsquo;-&gt; 07/10/2012</li>
<li>?#2012/10/7#+86400/86400 &lsquo;-&gt; 08/10/2012</li>
</ul>
<p><strong>La matrice des valeurs renvoyées</strong><br />
A ce niveau, il est intéressant d&rsquo;avoir sous la main la matrice 7&#215;7 des valeurs renvoyées par Weekday() en fonction du jour de la date transmise et du premier jour de la semaine (deuxième argument de la fonction) :</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)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VbDayOfWeek<br />
Jour Date &nbsp; &nbsp; &nbsp; L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D<br />
Lundi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2<br />
Mardi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3<br />
Mercredi &nbsp; &nbsp; &nbsp; &nbsp;3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5 &nbsp; 4<br />
Jeudi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6 &nbsp; 5<br />
Vendredi &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7 &nbsp; 6<br />
Samedi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1 &nbsp; 7<br />
Dimanche &nbsp; &nbsp; &nbsp; &nbsp;7 &nbsp; 6 &nbsp; 5 &nbsp; 4 &nbsp; 3 &nbsp; 2 &nbsp; 1</div></div>
<p>Par exemple, si le jour de la date en argument est lundi et le 1er jour de semaine est mercredi (vbWednesday), la fonction retourne <strong>6</strong>.</p>
<p><strong>Notre question du jour sous forme de matrice</strong><br />
On souhaite connaître la date du jour de semaine défini <strong>suivant</strong> la date passée en argument. Par exemple, le dimanche suivant le dimanche 7 octobre 2012 a pour date le 14 octobre et le lundi suivant est le 8 octobre.</p>
<p>Dasn ce cas, la matrice des valeurs attendues 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">Jour suivant<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VbDayOfWeek<br />
Jour date &nbsp; &nbsp; &nbsp; L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D<br />
Lundi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 &nbsp; 5 &nbsp; 6<br />
Mardi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 &nbsp; 7 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4 &nbsp; 5<br />
Mercredi &nbsp; &nbsp; &nbsp; &nbsp;5 &nbsp; 6 &nbsp; 7 &nbsp; 1 &nbsp; 2 &nbsp; 3 &nbsp; 4<br />
Jeudi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 &nbsp; 5 &nbsp; 6 &nbsp; 7 &nbsp; 1 &nbsp; 2 &nbsp; 3<br />
Vendredi &nbsp; &nbsp; &nbsp; &nbsp;3 &nbsp; 4 &nbsp; 5 &nbsp; 6 &nbsp; 7 &nbsp; 1 &nbsp; 2<br />
Samedi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 &nbsp; 3 &nbsp; 4 &nbsp; 5 &nbsp; 6 &nbsp; 7 &nbsp; 1<br />
Dimanche &nbsp; &nbsp; &nbsp; &nbsp;1 &nbsp; 2 &nbsp; 3 &nbsp; 4 &nbsp; 5 &nbsp; 6 &nbsp; 7</div></div>
<p>&nbsp;<br />
<strong>Comment passer de la matrice initiale à celle-ci ?</strong><br />
On remarque que la somme des deux matrices pour chaque case donne 8 :</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">Somme des matrices<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;VbDayOfWeek<br />
Jour date &nbsp; &nbsp; &nbsp; L &nbsp; M &nbsp; M &nbsp; J &nbsp; V &nbsp; S &nbsp; D<br />
Lundi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8<br />
Mardi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8<br />
Mercredi &nbsp; &nbsp; &nbsp; &nbsp;8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8<br />
Jeudi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8<br />
Vendredi &nbsp; &nbsp; &nbsp; &nbsp;8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8<br />
Samedi &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8<br />
Dimanche &nbsp; &nbsp; &nbsp; &nbsp;8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8 &nbsp; 8</div></div>
<p>Ces nouvelle matrice servira de <strong>pivot</strong> pour passer de la matrice &lsquo;Weekday&rsquo; à la matrice &lsquo;Jour suivant&rsquo;. En effet, il suffit de retrancher à 8 la valeur de &lsquo;Weekday&rsquo; pour obtenir la valeur &lsquo;Jour suivant&rsquo; correspondante.<br />
L&rsquo;algorithme est <code class="codecolorer vb blackboard"><span class="vb">DateJourSuivant = DateRef + 8 - Weekday(DateRef, Jour)</span></code></p>
<p><strong>Code source de la fonction &lsquo;DateJourSuivant&rsquo;</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> DateJourSuivant(<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;DateJourSuivant = DateRef - CInt(Weekday(DateRef, Jour)) + 8<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>La conversion explicite par CInt() du Variant retourné par Weekday() autorise un gain sensible de rapidité.<br />
L&rsquo;argument &lsquo;Jour&rsquo; accepte aussi bien une constante nommée de VbDayOfWeek (SAUF vbUseSystem !) qu&rsquo;un entier compris entre 1 et 7. Ne pas oublier que la valeur 1 correspond au dimanche (vbSunday) !</p>
<p><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 &lsquo;Exécution&rsquo; de l&rsquo;é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> TestDateJourSuivant()<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 DateJourSuivant()&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 = DateJourSuivant(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 />
Voir 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>@+ pour la suite des épisodes &lsquo;Weekday&rsquo;.</p>
<p>Philippe</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
