休日の関数を動かしたい
- 休日の関数を使いたいのですが、どんな関数を追加すればいいのでしょうか?
- シートには、特定のセルが変更された場合に、指定した条件に応じて他のセルの値を変更するコードが入っています。
- アドインには分析ツールと分析ツールVBAの両方がチェックされています。
- ベストアンサー
休日の関数を動かしたい
こちらで 質問して 平日の関数 を追加したおかげで、とても助かっています。 そこで、休日の関数を使いたいのですが、そんな関数はあるのでしょうか N7に1が入ると A7が 8/12になる N8に1が入ると A8が 8/12になる にしたいのですが、 どんな関数を下記にに追加すればいいのでしょうか? ちなみに アドインには 分析ツールと分析ツール VBA 両方共チェックがあります。 シートには、下記のコードが入っています。 ご存知のかた教えていただければ助かります。 Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim h As Range On Error Resume Next For Each h In Application.Intersect(Target, Range("N:N")) If h > 0 Then Select Case Cells(h.Row, "B") Case "毎日" Cells(h.Row, "A") = Cells(h.Row, "A") + 1 Case "平日" Cells(h.Row, "A") = Application.Run("ATPVBAEN.XLA!WorkDay", Cells(h.Row, "A"), 1) Case "毎週" Cells(h.Row, "A") = Cells(h.Row, "A") + 7 Case "隔週" Cells(h.Row, "A") = Cells(h.Row, "A") + 14 Case "毎月" Cells(h.Row, "A") = DateAdd("M", 1, Cells(h.Row, "A")) Case "隔月" Cells(h.Row, "A") = DateAdd("M", 2, Cells(h.Row, "A")) End Select End If Next For Each h In Application.Intersect(Target, Range("B:B")) If StrConv(h, vbNarrow) = "-" Then Cells(h.Row, "A").ClearContents End If Next End Sub
- ryujixryuj
- お礼率23% (109/460)
- オフィス系ソフト
- 回答数2
- ありがとう数1
- みんなの回答 (2)
- 専門家の回答
質問者が選んだベストアンサー
休日の関数が特に用意されているわけではありません。 次のようにすることで平日の場合でも日曜日や土曜日さらには別に指定した祝日などを避けた日付が表示されるように、また、休日の場合では日曜日や銅曜日の日付が表示されるようにするためには次のようなマクロにすることが必要でしょう。 Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim h As Range On Error Resume Next i = 0 For Each h In Application.Intersect(Target, Range("N:N")) If h > 0 Then Select Case Cells(h.Row, "B") Case "毎日" Cells(h.Row, "A") = Cells(h.Row, "A") + 1 Case "平日" myDate1 = Cells(h.Row, "A") Do i = i + 1 myDate = myDate1 + i myWeekday = Weekday(Date:=myDate) myHolyday = WorksheetFunction.CountIf(Worksheets("Sheet2").Range("A:A"), myDate) Loop Until myWeekday <> 1 And myWeekday <> 7 And myHolyday = 0 '日曜日と土曜日以外さらにシート2のA列での休日を含まない日を選んでいます。 Cells(h.Row, "A") = myDate Case "休日" myDate1 = Cells(h.Row, "A") Do i = i + 1 myDate = myDate1 + i myWeekday = Weekday(Date:=myDate) Loop Until myWeekday = 1 Or myWeekday = 7 '日曜日又はの場合の日付を選んでいます。 Cells(h.Row, "A") = myDate Case "毎週" Cells(h.Row, "A") = Cells(h.Row, "A") + 7 Case "隔週" Cells(h.Row, "A") = Cells(h.Row, "A") + 14 Case "毎月" Cells(h.Row, "A") = DateAdd("M", 1, Cells(h.Row, "A")) Case "隔月" Cells(h.Row, "A") = DateAdd("M", 2, Cells(h.Row, "A")) End Select End If Next For Each h In Application.Intersect(Target, Range("B:B")) If StrConv(h, vbNarrow) = "-" Then Cells(h.Row, "A").ClearContents End If Next End Sub
その他の回答 (1)
- bin-chan
- ベストアンサー率33% (1403/4213)
> N7に1が入ると A7が 8/12になる > N8に1が入ると A8が 8/12になる 8/12、今年は日曜日ですよね。 「どういう理由で8/12なのか」をもっと詳しく教えてください。
補足
説明が不足していました N列に0以上の数字が入力されたらA列の日付を変えたい B列の 毎日は +1日後 B列の 毎週は +7日後 B列の 休日は 月~金を飛ばして 次の土日 つまり N7に1が入力されると A7が 8/12になるという意味です。
関連するQ&A
- パソコンを変えたら ワークデイ関数が無効になった
http://okwave.jp/qa/q7456687.html 前回上記の質問をして、アドバイスどおりやることで問題なく使っていました。 先日パソコンをウィンドウズ7にして 同じエクセルファイルを使ったらワークデイ関数が無効になってしまいました。エクセルは 2003 のままです。 ちなみに アドインには 分析ツールと分析ツール VBA 両方共チェックがあります。 なぜ 環境が変わると、ワークデイ関数が無効になるのでしょうか? ちなみに シートには、下記のコードが入っています。 ご存知のかた教えていただければ助かります。 Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim h As Range On Error Resume Next For Each h In Application.Intersect(Target, Range("N:N")) If h > 0 Then Select Case Cells(h.Row, "B") Case "毎日" Cells(h.Row, "A") = Cells(h.Row, "A") + 1 Case "平日" Cells(h.Row, "A") = Application.Run("ATPVBAEN.XLA!WorkDay", Cells(h.Row, "A"), 1) Case "毎週" Cells(h.Row, "A") = Cells(h.Row, "A") + 7 Case "隔週" Cells(h.Row, "A") = Cells(h.Row, "A") + 14 Case "毎月" Cells(h.Row, "A") = DateAdd("M", 1, Cells(h.Row, "A")) Case "隔月" Cells(h.Row, "A") = DateAdd("M", 2, Cells(h.Row, "A")) End Select End If Next For Each h In Application.Intersect(Target, Range("B:B")) If StrConv(h, vbNarrow) = "-" Then Cells(h.Row, "A").ClearContents End If Next End Sub
- ベストアンサー
- オフィス系ソフト
- セルを空欄にできますか?
M列に0以上の数字が入力されたらA列の日付を変えるシートです B列の 毎日は +1日後 B列の 毎週は +7日後 下記がその計算式です。 アドインで「分析ツール」「分析ツール-VBA」のチェックを入れてWORKDAY関数 private sub Worksheet_Change(byval Target as excel.range) dim h as range on error resume next for each h in application.intersect(target, range("M:M")) if h > 0 then select case cells(h.row, "B") case "毎日" cells(h.row, "A") = cells(h.row, "A") + 1 case "平日" cells(h.row, "A") = application.run("ATPVBAEN.XLA!WorkDay", cells(h.row, "A"), 1) case "毎週" cells(h.row, "A") = cells(h.row, "A") + 7 case "隔週" cells(h.row, "A") = cells(h.row, "A") + 14 case "毎月" cells(h.row, "A") = dateadd("M", 1, cells(h.row, "A")) case "隔月" cells(h.row, "A") = dateadd("M", 2, cells(h.row, "A")) end select end if next end sub ------------------ そこで 質問ですが B5 の 「-」が入ると A5が空欄になるなんて 可能ですか? 上記の式になんと追加すればいいのでしょうか?
- ベストアンサー
- その他MS Office製品
- EXCEL VBA 指定した数字ごとに表示
・1から3までの数字をいれた場合に、9:00から11:00と表示する場合として以下のソースを書きます。(以前にこちらで教えていただきました) ・a = array()の部分について、直接書くのではなく、セルを参照することはできますでしょうか? a = array("cell(1,1)", "cell(1,2)", "cell(1,3)")みたいなイメージです。 よろしくお願い致します。 option base 1 private sub Worksheet_Change(byval Target as excel.range) dim h as range dim a as variant a = array("9:0", "10:0", "11:0") ’1から3 on error resume next for each h in application.intersect(target, range("D:D")) if cells(h.row, "F") <> "○" then if 1=< h.value and h.value <= 3 then ’1から3 if time >= timevalue(a(h.value)) then cells(h.row, "F") = a(h.value) end if end if end if next end sub
- ベストアンサー
- Excel(エクセル)
- VBA MsgBOXでの処理分岐
B列に抹消と入力したら別の列に抹消と記載する以下のマクロがありますが、 B列に抹消と入力したらMsgBoxで抹消しますか?と表示させたいです。 MsgBox関数を特定場所に挿入したのですが、うまくいかず、どこに挿入したらよろしいでしょうか。(オブジェクトが必要です。のエラーが出てしまう状況です。) Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim h As Range On Error Resume Next For Each h In Application.Intersect(Target, Range("B:B")) If h = "抹消" Then Cells(h.Row, "E").Resize(1, 7).SpecialCells(xlCellTypeConstants) = "抹消" End If Next End Sub
- ベストアンサー
- Excel(エクセル)
- VBA For~Next
こんにちは 上手く 説明できるか心配なのですが 下記のテスト1ですと 36行毎に Targetが A4,A40,A76,A112だと A1,A437,A73,A109.Value = "" Then ComboBox3.DropDownさせてから ComboBox4.DropDownさせてますが A1,A437,A73,A109にValue が入っていると A4,A40,A76,A112のCellをActiveしても ComboBox4.DropDown しません。 そこで、テスト2のように For~Nextを二つに分けました。 テスト2の方法しか無いのでしょうか? 宜しくお願いします。 Dim Row As Long 'テスト1 For Row = 4 To 112 Step 36 If Not Intersect(Target, Cells(Row, "A")) Is Nothing Then '今日の日付DropDown If Cells(Row - 3, "A").Value = "" Then form.ComboBox3.DropDown ElseIf Not Intersect(Target, Range(Cells(Row, "A"), Cells(Row + 13, "A"))) Is Nothing Then 'A列日付 form.ComboBox4.DropDown End If Next Dim Row As Long, iRow As Long 'テスト2 For Row = 4 To 112 Step 36 If Not Intersect(Target, Cells(Row, "A")) Is Nothing Then '今日の日付DropDown If Cells(Row - 3, "A").Value = "" Then form.ComboBox3.DropDown End If Next For iRow = 4 To 112 Step 36 If Not Intersect(Target, Range(Cells(iRow, "A"), Cells(iRow + 13, "A"))) Is Nothing Then 'A列日付 form.ComboBox4.DropDown End If Next
- ベストアンサー
- オフィス系ソフト
- 2つのVBAを組み合わせる方法
お世話になります、2つのVBAを組み合わせる方法で迷っています。 1つ目が Private Sub Worksheet_Change(ByVal Target As Range) Dim i As Long, k As Long, myNum As Long If Intersect(Target, Range("C1,B9:B39")) Is Nothing Or Target.Count > 1 Then Exit Sub Application.EnableEvents = False With Target If .Column = 3 Then myNum = WorksheetFunction.Max(Range("B9:B39")) If IsDate(.Value) Then For i = 9 To 39 If Cells(i, "A").Value = "" Then Cells(i, "B").Value = "" Else Cells(i, "B") = myNum + i - 8 End If Next i End If Else i = .Row If .Value = "" Then Range(Cells(i + 1, "B"), Cells(39, "B")).ClearContents Else For k = i + 1 To 39 If Cells(k, "A").Value = "" Then Cells(k, "B").Value = "" Else Cells(k, "B") = Cells(k - 1, "B") + 1 End If Next k End If End If End With Application.EnableEvents = True End Sub です。 2つめが Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = True If Intersect(Target, Range("R8:R38")) Is Nothing Then Exit Sub Application.EnableEvents = False Range(Cells(Target.Row, 18), Cells(39, 18)).Value = Target.Value Application.EnableEvents = True End Sub です。2つのPrivate Sub Worksheet_Change(ByVal Target As Range)イベントのVBAですが、どのようにして組み合わせれば良いのでしょうか?
- ベストアンサー
- Excel(エクセル)
- 毎月とか隔週に連動して日付を変えたい
OKWAVEで質問してある程度出来が上がってきましたが、まだ解決できないところがあり、 ご指導下さい、 M列に0以上の数字が入力されたらA列の日付を変えたい B列の 毎日は +1日後 B列の 毎週は +7日後 つまり M1に0以上なら 5/5が表示できるようにしたい(+1日後) M6に0以上なら 5/13が表示できるようにしたい(+7日後) それが下記の計算式です。 ------------------------------------- エクセルのシート名タブを右クリックしてコードの表示-現れたシートに下記をコピー貼り付けています Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim h As Range Dim c As Long On Error Resume Next For Each h In Application.Intersect(Target, Range("M:M")) If h > 0 Then If Cells(h.Row, "B") = "毎日" Then c = 1 ElseIf Cells(h.Row, "B") = "毎週" Then c = 7 Else c = 0 End If Cells(h.Row, "A") = Cells(h.Row, "A") + c End If Next End Sub ---------------- <質問1> 毎月、隔月、隔週 は下記であってますか?どこに入れたら動作するのでしょうか? ElseIf Cells(h.Row, "B") = "毎月" Then c = 30 Else c = 0 End If ElseIf Cells(h.Row, "B") = "隔月" Then c = 60 Else c = 0 End If ElseIf Cells(h.Row, "B") = "隔週" Then c = 14 Else c = 0 End If 投稿日時 - 2012-05-04 10:29:04
- ベストアンサー
- その他MS Office製品
- このマクロを訂正できますでしょうか?
private sub worksheet_change(byval Target as excel.range) dim h as range on error resume next for each h in application.intersect(target, range("C:C")) if h <> "" then cells(h.row, "A").formular1c1 = "=COUNTA(R1C[2]:RC[2])" cells(h.row, "B") = date else h.offset(0,-2).resize(1, 2).clearcontents end if next end sub このマクロはC列に入力すると、A列に番号、B列に入力した日付が入力されるマクロです。 現状の問題として、 (1)A列に計算式が入ってしまうこと (2)C3セルに品名という項目が入っているために、C4から品目を入力していく上で、最初の割り振られるNOが2番からになってしまう の2つの問題が生じています。これを解決するにはどのように訂正すればよいかご教授いただければ助かります。
- ベストアンサー
- オフィス系ソフト
- 平日の場合も 動作したい
OKWAVEで質問してある程度出来が上がってきましたが、まだ解決できないところがあり、 ご指導下さい、 M列に0以上の数字が入力されたらA列の日付を変えたい B列の 毎日は +1日後 B列の 毎週は +7日後 つまり M1に0以上なら 5/5が表示できるようにしたい(+1日後) M6に0以上なら 5/13が表示できるようにしたい(+7日後) それが下記の計算式です。 ------------------------------------- エクセルのシート名タブを右クリックしてコードの表示-現れたシートに下記をコピー貼り付けています Private Sub Worksheet_Change(ByVal Target As Excel.Range) Dim h As Range Dim c As Long On Error Resume Next For Each h In Application.Intersect(Target, Range("M:M")) If h > 0 Then If Cells(h.Row, "B") = "毎日" Then c = 1 ElseIf Cells(h.Row, "B") = "毎週" Then c = 7 Else c = 0 End If Cells(h.Row, "A") = Cells(h.Row, "A") + c End If Next End Sub ------------------------------- <質問1> C列を使う事で A7の平日 は可能でしょうか? 平日とは 月~金までをいい、M7に1が入ったら A7は 5/7 と表示したいです
- ベストアンサー
- オフィス系ソフト
- どこを修正すれば 日付が変わるのか?
先ほど、 http://okwave.jp/qa/q7454676.html の質問して サンプルにはきちんと作動することを確認しました。 そこで、サンプル版ではないものに手を加えても動作がうまくいきません private sub Worksheet_Change(byval Target as excel.range) dim h as range on error resume next for each h in application.intersect(target, range("C:C")) if h = "完了" then if h.offset(0, -1) = "毎日" then h.offset(0, -2) = h.offset(0, -2) + 1 elseif h.offset(0, -1) = "毎週" then h.offset(0, -2) = h.offset(0, -2) + 7 end if end if next end sub ------------- for each h in application.intersect(target, range("N:N")) と変更したのですが、動作しません。 どこを修正すれば、動作するでしょうか?
- ベストアンサー
- Visual Basic
お礼
回答ありがとうございました 動作確認取れました 助かりました 完璧です