サクッとスケジュール(略してサクスケ)のソースコードです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
Option Explicit Public Const KAISHI_CELL = "B1" 'カレンダー開始日を指定するセル Public Const OWARI_CELL = "B2" 'カレンダー終了日を指定するセル Public Const TASK_START = "B4" 'タスク記載の先頭セル Public Const CAL_START = "F2" 'カレンダー記載の先頭セル Public Const T_NAME = 2 'タスク名 Public Const B_COL = 3 'タスク開始列 Public Const E_COL = 4 'タスク終了列 Public Const COL_SIZE = 3 '列の幅 Public KAISHI_D As Date Public OWARI_D As Date Public Const CC_NAME = "CalenderControl" 'カレンダ制御(曜日、休日) 祝日は別のマクロで確認している Public ST_CC As Worksheet 'カレンダ制御するWS変数 Public Const O_HOLIDAY = "A4" 'その他休日を記入する先頭セル Public ADD_HOLIDAY() As Date 'その他休日を格納する配列 Public A_WS As Worksheet 'スケジュールを作成するWorksheet Public I_COLOR As Long 'タスクを実行するセルの背景色 Public F_COLOR As Long 'タスクを実行するセルのフォント色 Public Const HOLIDAY_COL = 38 '休日はピンク Public Yasumi As String Sub Calender_Make() Dim h_day As Variant 'その他休日指定日 Dim b_day As Date '開始日 Dim e_day As Date '終了日 Dim all_day As Long '日数 Dim all_task As Long 'タスク数 'I, J, K as Iterator Dim I As Long Dim J As Long Dim K As Long Dim hn As String 'スケジュールデータがある場合は削除 If Range(CAL_START) <> "" Then Range(CAL_START).CurrentRegion.Select Selection.EntireColumn.Delete End If 'カレンダ作成情報を取得 b_day = Range(KAISHI_CELL) '開始日の取得 e_day = Range(OWARI_CELL) '終了日の取得 all_day = DateDiff("d", b_day, e_day) '日数の取得 all_task = Range(TASK_START).Offset(60000, 0).End(xlUp).Row - Range(TASK_START).Row + 1 '行差分からタスク数の取得 '開始日を記入 With Range(CAL_START) .Value = b_day .NumberFormatLocal = "yyyy""/""m;@" .Offset(1, 0) = b_day .Offset(1, 0).NumberFormatLocal = "d;@" End With '開始日+1から終了日まで記入 For I = 1 To all_day With Range(CAL_START) '日付けを入力して、日のみ表示する .Offset(1, I) = DateAdd("d", I, b_day) .Offset(1, I).NumberFormatLocal = "d;@" '月の初日(1日)は月データも入力 If Day(.Offset(1, I)) = "1" Then '1日は年月を追加 .Offset(0, I) = .Offset(1, I) .Offset(0, I).NumberFormatLocal = "yyyy""/""m;@" End If End With Next I '幅調整 Range(CAL_START).CurrentRegion.ColumnWidth = COL_SIZE '休日チェック '設定曜日を確認 Call Yasumi_check For I = 0 To all_day With Range(CAL_START) '曜日休の確認 If InStr(Yasumi, Weekday(.Offset(1, I))) > 0 Then .Offset(1, I).Interior.ColorIndex = HOLIDAY_COL End If '祝日の確認 If Worksheets(CC_NAME).CB_Syuku.Value Then If Kyujitsu(.Offset(1, I), hn) Then .Offset(1, I).Interior.ColorIndex = HOLIDAY_COL End If End If '他休日の確認 If CHECK_A_HOLIDAY(.Offset(1, I)) Then .Offset(1, I).Interior.ColorIndex = HOLIDAY_COL End If End With Next I '全タスクに関するSchedule作成 For I = 0 To all_task - 1 '1つのタスクを作成(記入、休日) Call holiday_color_1row(Range(TASK_START).Offset(I, 0).Row) Call eday_schedule_1row(Range(TASK_START).Offset(I, 0).Row, b_day, e_day) Next I Call Draw_Keisen End Sub Sub Draw_Keisen() Dim all_task As Long Dim col_end As Long all_task = Range(TASK_START).Offset(60000, 0).End(xlUp).Row - Range(TASK_START).Row + 1 col_end = Range(CAL_START).Offset(1, 0).End(xlToRight).Column '月のマージ Dim merge_st As Long Dim merge_end As Long Dim c_row As Long Dim I As Long merge_st = Range(CAL_START).Column merge_end = merge_st c_row = Range(CAL_START).Row For I = merge_st To col_end If Cells(c_row, I + 1) <> "" Or I = col_end Then 'Merge With Range(Cells(c_row, merge_st), Cells(c_row, merge_end)) .MergeCells = True .HorizontalAlignment = xlCenter End With merge_st = merge_end + 1 End If merge_end = merge_end + 1 Next I '罫線作成 Range(CAL_START & ":" & Cells(all_task + Range(CAL_START).Row + 1, col_end).Address).Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone Selection.Borders(xlEdgeLeft).LineStyle = xlContinuous Selection.Borders(xlEdgeLeft).Weight = xlThin Selection.Borders(xlEdgeTop).LineStyle = xlContinuous Selection.Borders(xlEdgeTop).Weight = xlThin Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous Selection.Borders(xlEdgeBottom).Weight = xlThin Selection.Borders(xlEdgeRight).LineStyle = xlContinuous Selection.Borders(xlEdgeRight).Weight = xlThin Selection.Borders(xlInsideVertical).LineStyle = xlDot Selection.Borders(xlInsideVertical).Weight = xlThin Selection.Borders(xlInsideVertical).ColorIndex = 48 Selection.Borders(xlInsideHorizontal).LineStyle = xlDot Selection.Borders(xlInsideHorizontal).Weight = xlThin Selection.Borders(xlInsideHorizontal).ColorIndex = 48 Range(CAL_START & ":" & Cells(all_task + Range(CAL_START).Row + 1, col_end).Address).Rows(2).Select Selection.Borders(xlEdgeTop).LineStyle = xlContinuous Selection.Borders(xlEdgeTop).Weight = xlThin Selection.Borders(xlEdgeBottom).LineStyle = xlContinuous Selection.Borders(xlEdgeBottom).Weight = xlThin Selection.HorizontalAlignment = xlCenter '日付けのセンタリング(ついで) End Sub 'CalenderControlシートに設定されている休日の曜日を確認 Private Sub Yasumi_check() '設定曜日を確認 Yasumi = "" If Worksheets(CC_NAME).CB_mon.Value Then Yasumi = Yasumi & CStr(vbMonday) End If If Worksheets(CC_NAME).CB_tue.Value Then Yasumi = Yasumi & CStr(vbTuesday) End If If Worksheets(CC_NAME).CB_wed.Value Then Yasumi = Yasumi & CStr(vbWednesday) End If If Worksheets(CC_NAME).CB_thu.Value Then Yasumi = Yasumi & CStr(vbThursday) End If If Worksheets(CC_NAME).CB_fri.Value Then Yasumi = Yasumi & CStr(vbFriday) End If If Worksheets(CC_NAME).CB_sat.Value Then Yasumi = Yasumi & CStr(vbSaturday) End If If Worksheets(CC_NAME).CB_sun.Value Then Yasumi = Yasumi & CStr(vbSunday) End If '追加の休日を取得 Dim r_max As Long Dim I As Long Dim idx As Long r_max = Worksheets(CC_NAME).Range(O_HOLIDAY).End(xlDown).Row idx = 1 If r_max = Worksheets(CC_NAME).Range(O_HOLIDAY).Row Then ReDim ADD_HOLIDAY(1) ADD_HOLIDAY(0) = "" Else For I = Worksheets(CC_NAME).Range(O_HOLIDAY).Row To r_max ReDim Preserve ADD_HOLIDAY(idx) ADD_HOLIDAY(idx) = Worksheets(CC_NAME).Range(O_HOLIDAY).Offset(idx - 1, 0) idx = idx + 1 Next I End If End Sub Sub eday_schedule_1row(ByVal c_row As Long, ByVal b_day As Date, ByVal e_day As Date) Dim t_b As Date Dim t_e As Date Dim t_d As Long Dim t_diff As Long Dim b_offset As Long Dim J As Long t_b = Cells(c_row, B_COL) 'タスク開始日 t_e = Cells(c_row, E_COL) 'タスク終了日 t_diff = DateDiff("d", t_b, t_e) 'タスクの期間 b_offset = DateDiff("d", b_day, t_b) 'カレンダー開始日とタスク開始日の差分 'タスクの日付が、カレンダの範囲外に設定されている場合(次のタスクに移動) If DateDiff("d", e_day, t_b) > 0 Or DateDiff("d", t_e, b_day) > 0 Then Exit Sub End If 'タスクの開始日と終了日の逆転 If t_diff < 0 Then MsgBox Cells(c_row, T_NAME) & "の開始日と終了日が逆転しています" Exit Sub End If '色の塗りつぶし Range(Cells(c_row, Range(CAL_START).Column + b_offset), Cells(c_row, Range(CAL_START).Column + b_offset + t_diff)).Interior.Color = _ Cells(c_row, T_NAME).Interior.Color End Sub Sub holiday_color_1row(ByVal c_row As Long) 'HOLIDAY カラー Dim col_start As Long Dim col_end As Long Dim holiday_row As Long Dim I As Long col_start = Range(CAL_START).Column col_end = Range(CAL_START).Offset(1, 0).End(xlToRight).Column holiday_row = Range(CAL_START).Offset(1, 0).Row For I = col_start To col_end If Cells(holiday_row, I).Interior.ColorIndex = HOLIDAY_COL Then Cells(c_row, I).Interior.ColorIndex = HOLIDAY_COL End If Next I End Sub Function CHECK_A_HOLIDAY(chk_day As Date) As Boolean Dim h_day As Variant Dim tmp_flag As Boolean tmp_flag = False '他休日の確認 For Each h_day In ADD_HOLIDAY If DateDiff("d", h_day, chk_day) = 0 Then tmp_flag = True Exit For End If Next h_day CHECK_A_HOLIDAY = tmp_flag End Function |