• ベストアンサー

和暦、西暦の変換について(VBA)

こんにちは、VBAで西暦年から和暦年への変換でエラーが出て原因がよく分かりません。 下のようにして、変換してやろうとしてもyear1,month1,day1の型が違いますとかのエラーが出て、変数の型をInteger、String、 Variantでとろうとしてもエラーばかりです、宜しくお願いします。 もっと簡単に出来る方法があると思うのですが、よく分かりません。 曜日まで「平成○年○月○日(木)」というような表示形式にしたいのです。 year1 = Year(Format(Calendar1_F.Value, "yyyy/m/d/(aaaa)")) month1 = Month(Format(Calendar1_F.Value, "yyyy/m/d/(aaaa)")) day1 = Day(Format(Calendar1_F.Value, "yyyy/m/d/(aaaa)")) youbi1 = Right(Format(Calendar1_F.Value, "yyyy/m/d/(aaaa)"), 4) - Right(Format(Calendar1_F.Value, "yyyy/m/d/(aaaa)"), 3) TextBox3.Value = "平成" + (year1 - 1988) + "年" + month1 + "月" + day1 + "日" + "(" + youbi1 + ")"

質問者が選んだベストアンサー

  • ベストアンサー
  • D-Matsu
  • ベストアンサー率45% (1080/2394)
回答No.1

http://www.geocities.jp/okaway03170507/oka1303.htm まったくそのまんまの回答があります。

eiji_0035
質問者

お礼

有り難う御座いました ある日のシリアル値と別のシリアル値を引き算、足し算してこれを何日間として日に戻す関数はどのようなものでしょうか

その他の回答 (1)

  • myRange
  • ベストアンサー率71% (339/472)
回答No.2

Calendar1_Fがちゃんとした日付型になっている場合 >year1 = Year(Format(Calendar1_F.Value, "yyyy/m/d/(aaaa)")) 曜日まで変換しているのがエラーの原因ではないでしょうか。 year1 = Year(Format(Calendar1_F.Value, "yyyy/m/d")) それか、あっさりと year1 = Year(Calendar1_F.Value) また、曜日は youbi1 = Format(Calendar1_F.Value, "(aaaa)") で取得できます。 ●それから、個々の年、月、日を取得しなくていいなら次のような方法もあるかと。 TextBox3 = Format(Calendar1_F.Value, "ggge""年""m""月""d""日""(aaaa)")    

eiji_0035
質問者

お礼

有り難う御座いました

関連するQ&A

  • 「型が一致しません」のエラーが出るのですが

    htmlファイルのformに入力されたものを、aspで処理したいのですが、 エラーが出て困っています。 html内には   <INPUT TYPE=text NAME=year VALUE="" SIZE=3>年   <INPUT TYPE=text NAME=month VALUE="" SIZE=3>月   <INPUT TYPE=text NAME=day VALUE="" SIZE=3>日 があり、 aspでは  YEAR=Request.Form("year")  MONTH=Request.Form("month")  DAY=Request.Form("day") ここで、フォームの"month"に6が入力されているときのみ、 YEARに+1925し、ほかの時には+1988したいんです。そこで Dim NEN NEN=YEAR     If month="6" Then       NEN=DateAdd("yyyy",NEN,1925)      Else     NEN=DateAdd("yyyy",NEN,1988)     End If としてみたのですが、「型が一致しません」というエラーが出てしまいます。 おそらく最初にYEARに入っている(フォームから戻ってくる)値が文字列なので、それでほかの代数?に入れても文字列として処理されてしまって、1925などの数字を足すことができないのだろうと思うんですが…それであっていますでしょうか。だとしたら、型を一致させたいんですがどうしたらいいでしょうか? ものすごく醜い感じになってしまっていると思います…すみませんが、 よろしくお願いします。

  • カレンダー作成

    C言語初心者です。 西暦と月を入力してその月のカレンダーを作成するプログラムの問題なのですが #include <stdio.h> #define MMAX 12 #define COMP (year - 1) int main(void) {  int i, j, year, month, day, youbi, ycnt, mcnt = 0;  int mday[MMAX] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  printf("西暦と月を入力して下さい-->");  scanf("%4d%2d, &year, &month");  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)   mday[1] = 29;  for (i = 0; i < [A]; i++) {   mcnt += mday[i];  }  mcnt++;  [B] = ((COMP + COMP / 4 - COMP / 100 + COMP / 400) + mcnt) % 7;  printf("\n%4d 年%2d 月\n", year, month);  printf("----------------------------\n");  printf(" 日 月 火 水 木 金 土\n");  for (j = 0, ycnt = 0; j < youbi; j++, ycnt++) {   printf(" ");  }  for (day =1; day <= mday[month - 1]; day++) {   if ([C])    printf("[%2d]", day);   else    printf(" %2d ", day);   ycnt++;   if ([D]) {    printf("\n");    ycnt = 0;   }  }  return (0); } [A]、[B]、[C]、[D]に答えを入れなきゃいけないのですが私が考えた答えだと カレンダーの表示すらされません。どこがいけないのでしょうか? #include <stdio.h> #define MMAX 12 #define COMP (year - 1) int main(void) {  int i, j, year, month, day, youbi, ycnt, mcnt = 0;  int mday[MMAX] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  printf("西暦と月を入力して下さい-->");  scanf("%4d%2d, &year, &month");  if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)   mday[1] = 29;  for (i = 0; i < month; i++) {   mcnt += mday[i];  }  mcnt++;  youbi = ((COMP + COMP / 4 - COMP / 100 + COMP / 400) + mcnt) % 7;  printf("\n%4d 年%2d 月\n", year, month);  printf("----------------------------\n");  printf(" 日 月 火 水 木 金 土\n");  for (j = 0, ycnt = 0; j < youbi; j++, ycnt++) {   printf(" ");  }  for (day =1; day <= mday[month - 1]; day++) {   if (youbi = 0)    printf("[%2d]", day);   else    printf(" %2d ", day);   ycnt++;   if (youbi > 7) {    printf("\n");    ycnt = 0;   }  }  return (0); } よろしくお願いします。

  • アクセスVBAの検索で。

    アクセスでカレンダーというかスケジュールを管理できるようなものをつくろうとしています。 非連結フォーム1に ・入力年 ・入力月 ・[1]-[45]まで(それぞれ年月が変わると日付がかわる) という感じで入っています。 T_用件というテーブルには ・年(数値型 ・月(数値型 ・日(数値型 ・用件(テキスト型) とあり、これをもとに「F_用件」フォームを作成してあります。 フォーム1のボタンの1つを例にとるとこんな感じです。 If Me![7].Value <> "" Then Year = "(T_用件.年=" & (Me!入力年.Value) & ")" Month = "(T_用件.月=" & (Me!入力月.Value) & ")" Day = "(T_用件.年=" & (Me![7].Value) & ")" WhereCond = WhereCond & " AND (" & Year & " AND " & Month & " AND " & Day & ")" End If WhereCond = Mid(WhereCond, 6) DoCmd.OpenForm "F_用件", acNormal, , WhereCond こうするとF_用件フォームは開くのですが、きちんと検索されてきません。 どこが悪いのでしょうか? よろしくお願いします。

  • Java 日付の加算がうまくいきません。。どなたか助けてください・・・

    初歩的質問で申し訳ないのですが、ご存知の方ご教授ください。日付の加算についてどうしてもわからない箇所があります。 Calendar cal= Calendar.getInstance((TimeZone.getTimeZone("GMT"))); int year = 2007; int month = 1; int day = 1; cal.set(year,month,day); cal.add(Calendar.DAY_OF_MONTH,100); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); String tempDate=sdf.format(date); tempDate=2007/05/12 私が求めている結果は tempDate=2007/4/11 なのですが、tempDate=2007/05/12になってしまいます。 お願いします。 環境:WinXp pro Eclipse 3.1 JDK 1.4.2 2007/4/11は Oracle8i の実行結果です。 select to_date('2007-01-01') +100 from dual

    • ベストアンサー
    • Java
  • エクセルで和暦のみを1つのセルに記す方法

    A1に =TODAY()をいれます。 書式の設定は・・・ee/m/d と していますので 現在15/5/6と記されています。 そして B1にA1の和暦のみ・・・ C1にA1の月のみ・・・を 入れたいのです。 現在 B1に =IF(A1="","",YEAR(A1) C1に =IF(A1="","",MONTH(A1) D1に =IF(A1="","",DAY(A1) としるしおのおのの答えが出ていますが B1にはYEARではなくて 和暦で回答がほしいのです。 つまり・・・15 と答えが記されてほしいのです。 この場合はどのように設定すればよいですか? なかなかうまくできなくって・・・・ よろしくお願いいたします!!!

  • C言語で分からないところがあるのですが……

    C言語で分からないところがあるのですが…… すみません。C言語を学習していてつまづいたので、皆さんの意見を聞きたいと思います。 現在、カレンダーを表示するプログラムをつくっています。 Yearとmonthをユーザが入力すると、その年その月のカレンダーが出るという算段です。(画像貼っておきます。ソースコードは下) これはできました。 これをいじって、Yearをユーザから受け取ると、その年の1月から12月までのカレンダーがば~っと表示されるようにしろ、と言われました。 Yearとmonthを受け取って書くやつは友人の助けを得ながらなんとかかけましたが、もう無理です。多分、for文を使うんだと思うんですが……助けてください! #include <stdio.h> int dayofweek(int year, int month); int daysinmonth(int year, int month); int daysinyear(int year); void showcal(int dow, int days); int main(void) { int year, month; int dow; int dim; printf ("Year?: "); scanf ("%d", &year); printf ("Month?: "); scanf ("%d", &month); dow = dayofweek(year, month); dim = daysinmonth(year, month); showcal(dow, dim); return 0; } void showcal (int dow, int days) { int i, j, d; printf ("Su Mo Tu We Th Fr Sa\n"); d = 1; for (i = 0; i < dow; i++) { printf (" "); } for (; i < 7; i++) { printf (" %d ", d); d++; } printf("\n"); for (j = 0; d <= days; j++) { for (i = 0; i < 7 && d <= days; i++) { if (d < 10) printf (" %d ", d); else printf ("%d ", d); d++; } printf("\n"); } } int daysinmonth( int year, int month) { int dim; dim = 31; if (month == 4 || month == 6 || month == 9 || month == 11) dim = 30; if (month == 2) { if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) dim = 29; else dim = 28; } return dim; } int daysinyear( int year) { int diy; if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) diy = 366; else diy = 365; return diy; } int dayofweek (int year, int month) { int dow; int days; int y, m; if (year >= 2000) { days = 0; for ( y = 2000; y < year; y++ ) { days = days + daysinyear(y); } for ( m = 1; m < month; m++ ) { days = days + daysinmonth(year, m); } dow = (6 + days) % 7; } else { days = 0; for ( m = month; m <= 12; m++ ) { days = days + daysinmo

  • Excel VBAで「date」関数を使いたい

    VBAを使用して年月日を入力してます。 セルに表示だけなら Year & "/" & Month… とかでもいいんですが、その日付で計算を行いたいんです。 何日足す、とか。。 それでExcelで使える Date(Year,Month,Day) を思い出したんですが、VBAではエラーになってしまいました。 Application.WorksheetFunction.Date(Year, Month, Day) や Application.Date(Year, Month, Day) もダメでした。 どのようにしたら入力した日付で計算が行えるでしょうか?

  • 月曜日から始まるカレンダ

    日曜日から始まるカレンダは作れたのですが… これを変えて例えば月曜日から始まるカレンダを作りたいと思っています。 現状は、 ##########ここから########## ----------Calendar.class.php---------- class Calendar { protected static $DTO; // constructor public function __construct() { self::$DTO = new DateTime(); } // destructor public function __destruct() { } protected static function valid( $year = null, $month = null, $day = null ) { $valid = array (); if ( false === is_numeric( $year ) ) { $year = self::$DTO->format( 'Y' ); } if ( false === is_numeric( $month ) ) { $month = self::$DTO->format( 'n' ); } if ( false === is_numeric( $day ) ) { $day = self::$DTO->format( 'j' ); } self::$DTO->setDate( $year, $month, $day ); $valid[ 'year' ] = ( int ) self::$DTO->format( 'Y' ); $valid[ 'month' ] = ( int ) self::$DTO->format( 'n' ); $valid[ 'day' ] = ( int ) self::$DTO->format( 'j' ); return ( $valid ); } } ----------CalendarMonth.class.php---------- class CalendarMonth extends Calendar { // constructor public function __construct() { parent::__construct(); } // destructor public function __destruct() { parent::__destruct(); } public function parse( $year = null, $month = null ) { foreach ( parent::valid( $year, $month ) as $key => $value ) { if ( 'day' != $key ) { $parse[ 0 ][ $key ] = $value; } else { $parse[ 0 ][ 'days' ] = ( int ) parent::$DTO->format( 't' ); } } for ( $i = 0, $weeks = 1; $i < 42; $i++ ) { parent::$DTO->setDate( $parse[ 0 ][ 'year' ], $parse[ 0 ][ 'month' ], 1 ); $first_week = ( int ) parent::$DTO->format( 'w' ); if ( $i < $first_week ) { parent::$DTO->modify( '-' . ( 86400 * ( $first_week - $i ) . ' seconds' ) ); } else { parent::$DTO->modify( '+' . ( 86400 * ( $i - $first_week ) . ' seconds' ) ); } $parse[ $weeks ][ $i ][ 'year' ] = ( int ) parent::$DTO->format( 'Y' ); $parse[ $weeks ][ $i ][ 'month' ] = ( int ) parent::$DTO->format( 'n' ); $parse[ $weeks ][ $i ][ 'day' ] = ( int ) parent::$DTO->format( 'j' ); if ( 0 == ( ( $i + 1 ) % 7 ) ) { $weeks++; } } return ( $parse ); } } ----------CalendarMonth.html---------- <table border="1"> <?php include ( 'Calendar.class.php' ); include ( 'CalendarMonth.class.php' ); $CalendarMonth = new CalendarMonth(); $calendar = $CalendarMonth->parse(); for ( $i = 0; $i < count( $calendar ); $i++ ) { if ( 0 != $i ) { ?> <tr> <?php foreach ( $calendar[ $i ] as $week ) { ?> <td><?php echo ( $week[ 'year' ] . '-' . $week[ 'month' ] . '-' . $week[ 'day' ] ); ?></td> <?php } ?> </tr> <?php } } ?> </table> ##########ここまで########## 以上です。 文字数制限のため、次へ続く。

    • 締切済み
    • PHP
  • プログラムの内容

    以下のプログラムは答えとして渡されたものなんですが、 コマンド引数を使ったプログラムの流れがよくわかりません。 簡単に解説してくれる方、いませんか? #include <stdio.h> int leapyear(int year){ if(year%400 == 0) return 1; else if(year%4 == 0 && year%100 != 0) return 1; else return 0; } void addmonth(int *day, int *month, int *year){ *day = 1; (*month)++; if(*month > 12) { *month = 1; (*year)++; } } int main(int argc, char *argv[]){ int year, month, day, a=0, max_days; int month_days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; if(argc < 2) max_days = 10000; else max_days = atoi(argv[1]); printf("今日は西暦何年 何月 何日? "); scanf("%d%d%d",&year,&month,&day); while(a<max_days) { a++; day++; if(month == 2 && leapyear(year)) { if(day > 29) addmonth(&day, &month, &year); } else { if(day > month_days[month-1]) addmonth(&day, &month, &year); } } printf("今日から%d日後は、%d年%d月%d日です。",max_days, year, month, day); }

  • カレンダコントロールについて

    vb6.0について質問です。 カレンダコントロールから日付を取得する際に、 2006年1月1日を選択した場合 monthは1、dayは1を取得してきます。 これを二桁で表示したいので(01) month = Format(Calendar1.month, "00") day = Format(Calendar1.day, "00") 上記のようにフォーマット文によって処理したのですが うまくいきません。。。 なにがいけないのでしょうか?

専門家に質問してみよう