|    | 
     こんにちは。かみちゃん です。 
 
>下記のようなワークシートで、(A列に自動で数値を入力したい) 
>A列は空欄です。 
>B列には8桁の日付があらかじめ入力されています。 
> 
>A列   B列 
> 61    20060202 
> 61    20050701 
> 62    20060602 
> 63    20070601 
> 
>条件は B列の数値で決まります。 
> 
>20050401 から 20060331 までなら A列に 61 
>20060401 から 20070331 までなら A列に 62 
>20070401 から 20080331 までなら A列に 63 
>20080401 から 20090331 までなら A列に 64 
 
3月31日を末日とする年度を取得し、その年度から1944を引いた値を求めるという 
規則性でよければ、以下のような感じでできます。 
 
なお、年度取得関数については、以下のURLを参考にしています。 
http://www.nurs.or.jp/~ppoy/access/access/acM059.html 
 
Sub Macro1() 
 Dim c As Range 
  
 For Each c In Range("B2:B5") 
  '3/31を末日とする年度を取得し、その年度から1944を引いた値を求める 
  If IsDate(Format(c.Value, "0000\/00\/00")) Then 
   c.Offset(, -1).Value = GetNenDo(DateValue(Format(c.Value, "0000\/00\/00")), 3, 31) - 1944 
  End If 
 Next 
End Sub 
 
'決算月日を考慮した西暦年度取得 
'GetNenDo(売上日,決算月,締め日) 
'例: GetNenDo("2006/03/14",3,31) → 2005 
Function GetNenDo(ByVal myDate, ByVal myEmonth, ByVal myEday) As Long 
 Dim tmpDate As Integer 
 Dim tmpYear As Long 
 Dim tmpMonth As Integer 
 
 GetNenDo = 0 
 If IsNull(myEmonth) Then Exit Function 
 If IsNull(myEday) Then Exit Function 
 If IsNull(myDate) Then Exit Function 
 
 '月 
 If IsDate(myDate) Then tmpMonth = Month(myDate) Else Exit Function 
 
 '年 
 If tmpMonth < myEmonth Then 
  '決算月の前 
  tmpYear = Year(myDate) - 1 
 ElseIf tmpMonth = myEmonth Then 
  '決算月 
  tmpDate = Day(myDate) 
  If tmpDate <= myEday Then 
   tmpYear = Year(myDate) - 1 
  Else 
   tmpYear = Year(myDate) 
  End If 
 Else 
  '決算月の後 
  tmpYear = Year(myDate) 
 End If 
 
 '年末決算の例外 
 If DateSerial(Year(Date), myEmonth, myEday) = _ 
  DateSerial(Year(Date), 12, 31) Then 
  tmpYear = tmpYear + 1 
 End If 
 
 GetNenDo = tmpYear 
End Function 
 
 | 
     
    
   |