PDA

Vollständige Version anzeigen : Wie errechne ich das Alter in Jahren (Wochen)?


Stefan Kulpa
01.02.2003, 04:10
<div><link href="http://www.ms-office-forum.de/ubb/codeconv.css" rel="stylesheet"><pre><span class="TOKEN">Sub</span> Beispiel()
&nbsp;
<span class="TOKEN">Dim</span> lWeeks <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="TOKEN">Dim</span> lAge <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
&nbsp;
lAge = Age(<span class="TOKEN">CDate</span>(&quot;01.01.1970&quot;), lWeeks)
<span class="TOKEN">Debug.Print</span> &quot;Alter:&quot;; lAge; &quot;Jahre bzw.&quot;; lWeeks; &quot;Wochen&quot;
<span class="REM">'// -&gt; Alter: 33 Jahre bzw. 1726 Wochen</span>
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Sub</span>
&nbsp;
<span class="TOKEN">Function</span> Age(<span class="TOKEN">ByVal</span> dtBirthday <span class="TOKEN">As</span> Date, _
<span class="TOKEN">Optional</span> <span class="TOKEN">ByRef</span> lWeeks <span class="TOKEN">As</span> <span class="TOKEN">Long</span>) <span class="TOKEN">As</span> <span class="TOKEN">Long</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Methode: | Ermittelt das Alter in Jahren und optional in Wochen</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Parameter: | dtBirthday - g&uuml;ltiges (Geburts)Datum</span>
<span class="REM">'// | lWeeks - optionale R&uuml;ckgabevariable f&uuml;r Wochen</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// R&uuml;ckgabe: | das Alter in Jahren; optional in Wochen (lWeeks)</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="REM">'// Um zwei Werte zur&uuml;ckgeben zu k&ouml;nnen (Alter in Jahren und Monaten)</span>
<span class="REM">'// wird der Parameter lWeeks als Referenz &uuml;bergeben, damit die</span>
<span class="REM">'// aufrufende Methode diese Werte nach Durchf&uuml;hrung der Funktion</span>
<span class="REM">'// auch wieder abrufen kann.</span>
<span class="REM">'// -----------------------------------------------------------------</span>
<span class="TOKEN">On Error GoTo</span> Err_Age
<span class="TOKEN">If</span> Day(dtBirthday) &lt;= Day(Now) <span class="TOKEN">Then</span>
lWeeks = DateDiff(&quot;w&quot;, dtBirthday, Now)
<span class="TOKEN">Else</span>
lWeeks = DateDiff(&quot;w&quot;, dtBirthday, Now) - 1
<span class="TOKEN">End</span> <span class="TOKEN">If</span>
Age = Year(Now) - Year(dtBirthday) + _
(DateSerial(Year(Now), Month(dtBirthday), Day(dtBirthday)) &gt; Now)
Exit_Age:
<span class="TOKEN">Exit Function</span>
Err_Age:
Age = 0
lWeeks = 0
<span class="TOKEN">Resume</span> Exit_Age
&nbsp;
<span class="TOKEN">End</span> <span class="TOKEN">Function</span></pre></div>
Code eingefügt mit dem MOF Code Converter (http://www.ms-office-forum.net/forum/codeconverter.php)