SIBA INU のメモ帳

(祝)東京オリンピック!

(祝)北京オリンピック!

エクセルのセルから呼び出すシェイプカレンダー

全コード



   

COPY

  1. Option Explicit
  2. '---------------------------------------
  3. Dim shpHeight As Long
  4. Dim shpWidth As Long
  5. Dim TBT As Long
  6. Dim YBT As Long
  7. Dim DBT(1 To 6) As Long
  8. Dim BL(1 To 7) As Long
  9.  
  10. Private Const ColorSunday = vbRed
  11. Private Const ColorSaturday = vbBlue
  12. Private Const ColorWeekday = vbBlack
  13. Private Const ColorPreHome = 5288016
  14.  
  15. Private Enum IncFlg
  16. IncForward = 0
  17. IncBackward = -1
  18. End Enum
  19.  
  20. Private Const FirstYobi = vbSunday
  21. Private Const DayStr As String = "d"
  22. Private Const MonthStr As String = "m"
  23. Private Const YearStr As String = "yyyy"
  24. Private Const WeekStr As String = "ww"
  25.  
  26. Private StartPosi As Integer
  27. Private MonthLen As Variant
  28. Private Yobi(1 To 7) As Variant
  29.  
  30. Private DefYear As Integer
  31. Private DefMonth As Integer
  32. Private DefDay As Integer
  33.  
  34. Private CurDate As Date
  35. Private CurYear As Integer
  36. Private CurMonth As Integer
  37. Private CurDay As Integer
  38.  
  39. Private DefFirstYobi As Integer
  40. Private SelectShape As String
  41.  
  42. '---------------------------------------
  43. Public Sub OpenCalendar()
  44. Dim I As Integer
  45. Dim J As Long
  46. Dim BUF As String
  47. Call DeleteCal
  48. '--------月の日数の配列 0はダミー値
  49. MonthLen = Array(0, 31, 28, 31, 30, 31, 30, _
  50. 31, 31, 30, 31, 30, 31)
  51. '--------最初に来る曜日を日曜日に指定
  52. DefFirstYobi = FirstYobi
  53. For J = 1 To 7
  54. Yobi(J) = Left$(WeekdayName(J, FirstDayOfWeek:=DefFirstYobi), 1)
  55. Next J
  56.  
  57. shpHeight = 15
  58. shpWidth = 35
  59. TBT = ActiveCell.OffSet(0, 1).Top
  60. BL(1) = ActiveCell.OffSet(0, 1).Left
  61.  
  62. YBT = TBT + shpHeight
  63. DBT(1) = YBT + shpHeight
  64. For I = 2 To 6
  65. DBT(I) = DBT(I - 1) + shpHeight
  66. Next I
  67. For J = 2 To 7
  68. BL(J) = BL(J - 1) + shpWidth
  69. Next J
  70.  
  71. Application.ScreenUpdating = False
  72.  
  73. BUF = Replace(StrConv(ActiveCell.Value, vbNarrow), ".", "/")
  74. If IsDate(BUF) Then
  75. BUF = CDate(BUF)
  76. Else
  77. BUF = Date
  78. End If
  79. '--------年月日の要素を取り出す
  80. DefDay = DatePart(DayStr, BUF)
  81. DefMonth = DatePart(MonthStr, BUF)
  82. DefYear = DatePart(YearStr, BUF)
  83.  
  84. Call UpShapes
  85. Call StartValues(BUF)
  86. Call CalendarDisp
  87.  
  88. Application.ScreenUpdating = True
  89.  
  90. End Sub
  91.  
  92. '---------------------------------------
  93. Private Sub UpShapes()
  94. Dim SHP As Shape
  95. Dim I As Integer
  96. Dim J As Integer
  97. Dim intLogicalDay As Integer
  98.  
  99. '--------
  100. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(1), _
  101. TBT, shpWidth, shpHeight)
  102. With SHP
  103. .Name = "SHP_PreviousYear"
  104. .OnAction = "ShapeCalendar.PreviousYearClick"
  105. .TextFrame.Characters.Text = "▼"
  106. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 255)
  107. .Fill.ForeColor.RGB = RGB(252, 213, 181)
  108. End With
  109. Call ShapeHyozi(SHP)
  110. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(2), _
  111. TBT, shpWidth * 2, shpHeight)
  112. With SHP
  113. .Name = "SHP_TextYaer"
  114. .OnAction = "ShapeCalendar.Dummy"
  115. .TextFrame.Characters.Text = ""
  116. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 255)
  117. .Fill.ForeColor.RGB = RGB(221, 221, 221)
  118. End With
  119. Call ShapeHyozi(SHP)
  120. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(4), _
  121. TBT, shpWidth, shpHeight)
  122. With SHP
  123. .Name = "SHP_NextYear"
  124. .OnAction = "ShapeCalendar.NextYearClick"
  125. .TextFrame.Characters.Text = "▲"
  126. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 255)
  127. .Fill.ForeColor.RGB = RGB(146, 246, 166)
  128. End With
  129. Call ShapeHyozi(SHP)
  130. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(5), _
  131. TBT, shpWidth, shpHeight)
  132. With SHP
  133. .Name = "SHP_PreviousMonth"
  134. .OnAction = "ShapeCalendar.PreviousMonthClick"
  135. .TextFrame.Characters.Text = "▼"
  136. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 255)
  137. .Fill.ForeColor.RGB = RGB(252, 213, 181)
  138. End With
  139. Call ShapeHyozi(SHP)
  140. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(6), TBT, shpWidth, shpHeight)
  141. With SHP
  142. .Name = "SHP_TextMonth"
  143. .OnAction = "ShapeCalendar.Dummy"
  144. .TextFrame.Characters.Text = ""
  145. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 255)
  146. .Fill.ForeColor.RGB = RGB(221, 221, 221)
  147. End With
  148. Call ShapeHyozi(SHP)
  149. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(7), _
  150. TBT, shpWidth, shpHeight)
  151. With SHP
  152. .Name = "SHP_NextMonth"
  153. .OnAction = "ShapeCalendar.NextMonthClick"
  154. .TextFrame.Characters.Text = "▲"
  155. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 255)
  156. .Fill.ForeColor.RGB = RGB(146, 246, 166)
  157. End With
  158. Call ShapeHyozi(SHP)
  159. '--------
  160. For J = 1 To 7
  161. intLogicalDay = (((J - 1) + (DefFirstYobi - 1)) Mod 7) + 1
  162. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(J), _
  163. YBT, shpWidth, shpHeight)
  164. With SHP
  165. .Name = "SHPY" & Format(J, "00")
  166. .OnAction = "ShapeCalendar.Dummy"
  167. .Fill.ForeColor.RGB = RGB(221, 221, 221)
  168. If ((intLogicalDay - 1) Mod 7) = 0 Then
  169. .TextFrame.Characters.Text = Yobi(intLogicalDay)
  170. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(255, 0, 0)
  171. ElseIf ((intLogicalDay - 1) Mod 7) = 6 Then
  172. .TextFrame.Characters.Text = Yobi(intLogicalDay)
  173. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 255)
  174. Else
  175. .TextFrame.Characters.Text = Yobi(intLogicalDay)
  176. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = ColorWeekday
  177. End If
  178. End With
  179. Call ShapeHyozi(SHP)
  180. Next J
  181.  
  182. '--------
  183. For I = 1 To 6
  184. For J = 1 To 7
  185. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(J), _
  186. DBT(I), shpWidth, shpHeight)
  187. With SHP
  188. .OnAction = "ShapeCalendar.DateClick"
  189. .Name = "SHPD" & I & J
  190. .TextFrame.Characters.Text = CStr((I - 1) * 7 + J)
  191. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = ColorWeekday
  192. .Fill.ForeColor.RGB = RGB(221, 221, 221)
  193. End With
  194. Call ShapeHyozi(SHP)
  195. Next J
  196. Next I
  197.  
  198. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(1), _
  199. DBT(6) + shpHeight, shpWidth * 2, shpHeight)
  200. With SHP
  201. .Name = "SHP_HOME"
  202. .OnAction = "ShapeCalendar.GoHome"
  203. .Fill.ForeColor.RGB = RGB(221, 221, 221)
  204. .TextFrame.Characters.Text = "HOME"
  205. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = ColorSaturday
  206. End With
  207. Call ShapeHyozi(SHP)
  208. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(3), _
  209. DBT(6) + shpHeight, shpWidth * 2, shpHeight)
  210. With SHP
  211. .Name = "SHP_PREHOME"
  212. .OnAction = "ShapeCalendar.GoPreHome"
  213. .Fill.ForeColor.RGB = RGB(221, 221, 221)
  214. .TextFrame.Characters.Text = "preHOME"
  215. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = ColorPreHome
  216. End With
  217. Call ShapeHyozi(SHP)
  218.  
  219. Set SHP = ActiveSheet.Shapes.AddShape(1, BL(5), _
  220. DBT(6) + shpHeight, shpWidth * 3, shpHeight)
  221. With SHP
  222. .Name = "SHP_CANCEL"
  223. .OnAction = "ShapeCalendar.DeleteCal"
  224. .Fill.ForeColor.RGB = RGB(221, 221, 221)
  225. .TextFrame.Characters.Text = "CANCEL"
  226. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = ColorSunday
  227. End With
  228. Call ShapeHyozi(SHP)
  229.  
  230. End Sub
  231.  
  232. '---------------------------------------
  233. Private Sub ShapeHyozi(ByRef myShape As Shape)
  234.  
  235. With myShape
  236. .Line.Visible = msoTrue
  237. .Line.ForeColor.RGB = RGB(166, 166, 166)
  238. .Line.Weight = 0.5
  239. .Fill.Visible = msoTrue
  240. .Fill.Solid
  241. .TextFrame2.TextRange.ParagraphFormat.Alignment = msoAlignCenter
  242. .TextFrame2.TextRange.Font.Size = 10
  243. .Placement = xlFreeFloating
  244. .Locked = msoTrue
  245. End With
  246. End Sub
  247.  
  248. '---------------------------------------
  249. Private Sub StartValues(ByVal OpenDate As Date)
  250.  
  251. CurDate = OpenDate
  252.  
  253. CurYear = DatePart(YearStr, CurDate)
  254. CurMonth = DatePart(MonthStr, CurDate)
  255. CurDay = DatePart(DayStr, CurDate)
  256. Call DispYearMonth
  257. End Sub
  258.  
  259. '---------------------------------------
  260. Private Sub DispYearMonth()
  261.  
  262. With ActiveSheet.Shapes("SHP_TextYaer").DrawingObject
  263. .Caption = Format(DateSerial(CurYear, CurMonth, 1), "ggge年")
  264. End With
  265. With ActiveSheet.Shapes("SHP_TextMonth").DrawingObject
  266. .Caption = Format(DateSerial(CurYear, CurMonth, 1), "m月")
  267. End With
  268. End Sub
  269.  
  270. '---------------------------------------
  271. Private Sub DateClick()
  272. Dim strN As String
  273. Dim FLG As Boolean
  274. '-----マクロを呼び出したオブジェクトの名前
  275. strN = Application.Caller
  276. '-----シェイプにテキストデータがない
  277. If Len(ActiveSheet.Shapes(strN).TextFrame.Characters.Text) = 0 Then
  278. Exit Sub
  279. End If
  280. '-----2回連続でクリックされた場合、Tureで新たに押された場合Falseになる
  281. FLG = (SelectShape = strN)
  282. Call ClickShape(strN)
  283. '-----2回連続でクリックされた場合
  284. If FLG Then
  285. ActiveCell.Value = DateSerial(CurYear, CurMonth, CurDay)
  286. Call DeleteCal
  287. End If
  288.  
  289. End Sub
  290.  
  291. '---------------------------------------
  292. Public Sub DeleteCal()
  293. Dim SHP() As String
  294. Dim Sp As Shape
  295. Dim objRange As Object
  296. Dim iCount As Long
  297. Dim I As Integer
  298. '-----シェイプがなければ終了
  299. If ActiveSheet.Shapes.Count = 0 Then
  300. Exit Sub
  301. End If
  302. '-----名称がSHPから始まるシェイプの拾い上げ
  303. ReDim SHP(1 To ActiveSheet.Shapes.Count)
  304. iCount = 0
  305. For Each Sp In ActiveSheet.Shapes
  306. If InStr(1, Sp.Name, "SHP") > 0 Then
  307. iCount = iCount + 1
  308. SHP(iCount) = Sp.Name
  309. End If
  310. Next Sp
  311. '-----なければ終わり
  312. If iCount = 0 Then
  313. Exit Sub
  314. End If
  315. '-----シェイプの一括削除のため一括選択
  316. ReDim Preserve SHP(1 To iCount)
  317. Set objRange = ActiveSheet.Shapes.Range(SHP)
  318. objRange.Select
  319. '-----削除のためシート保護の解除
  320. ActiveSheet.Unprotect
  321. '----一括削除の実行
  322. objRange.Delete
  323.  
  324. End Sub
  325.  
  326. '---------------------------------------
  327. Private Sub GoHome()
  328. Call MoveToToday(UseCurYear:=True)
  329. End Sub
  330.  
  331. '---------------------------------------
  332. Private Sub GoPreHome()
  333. Call MoveToToday(UseCurYear:=False)
  334. End Sub
  335.  
  336. '---------------------------------------
  337. Private Sub Dummy()
  338.  
  339. End Sub
  340.  
  341. '---------------------------------------DateCalendarの日付の描写
  342. Private Sub CalendarDisp()
  343. Dim newSelected As String
  344. Dim TsuBan As Integer
  345. '--------1日の週日
  346. StartPosi = DatePart("w", _
  347. DateSerial(CurYear, CurMonth, 1), DefFirstYobi)
  348. '--------DateCalendarの日付の描写
  349. Call DaysInMonth(StartPosi)
  350. '--------選択された
  351. newSelected = "SHPD" & Grid(CurDay, StartPosi)
  352. Call ClickShape(newSelected)
  353.  
  354. End Sub
  355.  
  356. '---------------------------------------
  357. Private Function Grid(intDay As Integer, _
  358. intStart As Integer) As String
  359. Dim TsuBan As Integer
  360. Dim Res As String
  361.  
  362. TsuBan = intDay + intStart - 1
  363.  
  364. Res = CStr(((TsuBan - 1) \ 7) + 1) & CStr((TsuBan - 1) Mod 7 + 1)
  365. Grid = Res
  366.  
  367. End Function
  368.  
  369. '---------------------------------------
  370. Private Sub DaysInMonth(intStartDay As Integer)
  371. Dim intRow As Integer
  372. Dim intCol As Integer
  373. Dim intDays As Integer
  374. Dim intCount As Integer
  375. Dim strTemp As String
  376. Dim lngForeColor As Long
  377.  
  378. If CurMonth <> 2 Then
  379. '--------2月以外の場合
  380. intDays = MonthLen(CurMonth)
  381. Else
  382. '-------2月の場合(31日の1日前)
  383. intDays = DatePart(DayStr, DateSerial(CurYear, 3, 1) - 1)
  384. End If
  385. '--------月、年を更新した場合、1/312/28等の処理
  386. If CurDay > intDays Then
  387. CurDay = intDays
  388. End If
  389. '--------DateCalendarの日付の描写
  390. intCount = 0
  391. For intRow = 1 To 6
  392. For intCol = 1 To 7
  393. If (intRow = 1) And (intCol < intStartDay) Then
  394. ActiveSheet.Shapes("SHPD1" & intCol).TextFrame.Characters.Text = ""
  395. Else
  396. intCount = intCount + 1
  397. strTemp = "SHPD" & intRow & intCol
  398. With ActiveSheet.Shapes(strTemp)
  399. If intCount <= intDays Then
  400. .TextFrame.Characters.Text = intCount
  401. '追加--------休日の赤色表示処理
  402. Select Case Kyujitu(DateSerial(CurYear, CurMonth, intCount))
  403. Case 1
  404. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = ColorSunday
  405. Case 7
  406. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = ColorSaturday
  407. Case Else
  408. .TextFrame2.TextRange.Font.Fill.ForeColor.RGB = ColorWeekday
  409. End Select
  410. Else
  411. .TextFrame.Characters.Text = ""
  412. End If
  413. End With
  414. End If
  415. Next intCol
  416. Next intRow
  417.  
  418. End Sub
  419.  
  420. '---------------------------------------
  421. Private Function HandleSelected(strName As String)
  422. Call ClickShape(strName)
  423. End Function
  424.  
  425. '---------------------------------------
  426. Private Sub ClickShape(NewSelect As String)
  427.  
  428. If Len(SelectShape) > 0 Then
  429. If SelectShape <> NewSelect Then
  430. With ActiveSheet.Shapes(SelectShape)
  431. .Fill.ForeColor.RGB = RGB(221, 221, 221)
  432. End With
  433. SelectShape = NewSelect
  434. End If
  435. Else
  436. SelectShape = NewSelect
  437. End If
  438.  
  439. With ActiveSheet.Shapes(SelectShape)
  440. .Fill.ForeColor.RGB = RGB(255, 255, 0)
  441. CurDay = .TextFrame.Characters.Text
  442. End With
  443.  
  444. End Sub
  445.  
  446. '---------------------------------------
  447. Private Sub NextYearClick()
  448. Call NextYear
  449. End Sub
  450.  
  451. Private Sub PreviousYearClick()
  452. Call PreviousYear
  453. End Sub
  454.  
  455. Private Sub NextMonthClick()
  456. Call NextMonth
  457. End Sub
  458.  
  459. Private Sub PreviousMonthClick()
  460. Call PreviousMonth
  461. End Sub
  462.  
  463. '---------------------------------------
  464. Public Sub Today()
  465. Call MoveToToday(UseCurYear:=True)
  466. End Sub
  467.  
  468. Public Sub NextMonth()
  469. Call ChageCalendar(MonthStr, IncForward)
  470. End Sub
  471.  
  472. Public Sub NextYear()
  473. Call ChageCalendar(YearStr, IncForward)
  474. End Sub
  475.  
  476. Public Sub PreviousMonth()
  477. Call ChageCalendar(MonthStr, IncBackward)
  478. End Sub
  479.  
  480. Public Sub PreviousYear()
  481. Call ChageCalendar(YearStr, IncBackward)
  482. End Sub
  483. '---------------------------------------
  484. Private Sub ChageCalendar(strMoveUnit As String, dt As IncFlg)
  485. Dim iMonth As Integer
  486. Dim iYear As Integer
  487. Dim iDay As Integer
  488. Dim INCDate As Date
  489. Dim OldDate As Date
  490. Dim iInc As Integer
  491.  
  492. iYear = CurYear
  493. iMonth = CurMonth
  494. iDay = CurDay
  495.  
  496. If dt = IncForward Then
  497. iInc = 1
  498. Else
  499. iInc = -1
  500. End If
  501. OldDate = DateSerial(iYear, iMonth, iDay)
  502. INCDate = DateAdd(strMoveUnit, iInc, OldDate)
  503.  
  504. iMonth = DatePart(MonthStr, INCDate)
  505. iYear = DatePart(YearStr, INCDate)
  506. iDay = DatePart(DayStr, INCDate)
  507.  
  508. If CurMonth = iMonth And CurYear = iYear Then
  509. Call ClickShape("SHPD" & Grid(iDay, StartPosi))
  510. Else
  511. CurDay = iDay
  512. CurMonth = iMonth
  513. CurYear = iYear
  514. Call DispYearMonth
  515. Call CalendarDisp
  516. End If
  517.  
  518. End Sub
  519.  
  520. '---------------------------------------
  521. Private Sub MoveToToday(UseCurYear As Boolean)
  522.  
  523. CurMonth = DefMonth
  524. If UseCurYear Then
  525. CurYear = DefYear
  526. End If
  527. CurDay = DefDay
  528. Call DispYearMonth
  529. Call CalendarDisp
  530. End Sub

ワークシートの仕込み

セルの選択した時アイコンを表示します。



   

COPY

  1. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
  2. Dim weekz As Variant
  3. Dim wz As Variant
  4. Call DeleteCal
  5.  
  6. weekz = Array("yyyy/mm/dd", "yy/m/d", "yy/mm/dd", "d""日""", "dd""日""", "d/m/yyyy", "dd/mm/yyyy", "ggge", "ge")
  7. For Each wz In weekz
  8. If InStr(Cells(Target.Row, Target.Column).NumberFormatLocal, wz) > 0 Then
  9. Call DispCalendarIcon
  10. Exit For
  11. End If
  12. Next
  13. End Sub
  14.  
  15. Private Sub DispCalendarIcon()
  16. Dim SHP As Object
  17.  
  18. Set SHP = ActiveSheet.Pictures. _
  19. Insert(Application.Path & "\FORMS\1041\APPTS.ICO")
  20. With SHP
  21. .Left = ActiveCell.OffSet(0, 1).Left + 5
  22. .Top = ActiveCell.OffSet(0, 1).Top + 2
  23. .Name = "SHPIcon"
  24. .OnAction = "ShapeCalendar.OpenCalendar"
  25. .PrintObject = msoFalse
  26. .Placement = xlMove
  27. .Locked = msoTrue
  28. End With
  29.  
  30. End Sub

セルをダブルクリックした時

カレンダーを表示



   

COPY

  1. Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
  2. Call ShapeCalendar.OpenCalendar
  3. End Sub

祝日休日を判定する関数



   

COPY

  1. Private Function Kyujitu(lDate As Long) As Integer
  2. Dim CurYear As Integer
  3. Dim ResWeekNum As Integer
  4. Dim FLG As Boolean
  5. Dim I As Integer
  6. Dim J As Integer
  7. Dim k As Integer
  8. Dim iCount As Integer
  9. Dim lDay As Long
  10. Dim DateBUF As Long
  11. Dim lKyujitu() As Long
  12. Dim lKokumin() As Long
  13. Dim lKanrei() As Long
  14. Dim lHurikae() As Long
  15.  
  16. CurYear = Year(lDate)
  17. ResWeekNum = Weekday(lDate)
  18. ReDim lKyujitu(0) '----------値の初期化
  19. lKyujitu(0) = 0
  20. ReDim lHurikae(0)
  21. lHurikae(0) = 0
  22. ReDim lKokumin(0)
  23. lKokumin(0) = 0
  24. ReDim lKanrei(0)
  25. lKanrei(0) = 0
  26. If ResWeekNum <> 1 And lDate > DateSerial(1948, 7, 19) Then '----------祝祭日のセット
  27. '①元日
  28. iCount = 1
  29. ReDim Preserve lKyujitu(iCount)
  30. lKyujitu(iCount) = DateSerial(CurYear, 1, 1)
  31. '②成人の日 1月15日 → 1月の第2月曜
  32. If CurYear > 1949 And CurYear < 2000 Then
  33. iCount = iCount + 1
  34. ReDim Preserve lKyujitu(iCount)
  35. lKyujitu(iCount) = DateSerial(CurYear, 1, 15)
  36. ElseIf CurYear > 1999 Then
  37. iCount = iCount + 1
  38. ReDim Preserve lKyujitu(iCount)
  39. lKyujitu(iCount) = DateSerial(CurYear, 1, 8) + ((9 - Weekday(DateSerial(CurYear, 1, 8))) Mod 7)
  40. End If
  41. '③建国記念の日
  42. iCount = iCount + 1
  43. ReDim Preserve lKyujitu(iCount)
  44. lKyujitu(iCount) = DateSerial(CurYear, 2, 11)
  45.  
  46. '④春分の日
  47. iCount = iCount + 1
  48. ReDim Preserve lKyujitu(iCount)
  49. 'int(19.8277+0.242194*(年-1980)-int((年-1983)/4)) ----------1851-1899年通用
  50. 'int(20.8357+0.242194*(年-1980)-int((年-1983)/4)) ----------1900-1979年通用
  51. 'int(20.8431+0.242194*(年-1980)-int((年-1980)/4)) ----------1980-2099年通用
  52. 'int(21.8510+0.242194*(年-1980)-int((年-1980)/4)) ----------2100-2150年通用
  53.  
  54. Select Case CurYear
  55. Case Is < 2100
  56. DateBUF = Int(20.8431 + 0.242194 * (CurYear - 1980) - Int((CurYear - 1980) / 4))
  57. Case Is >= 2100
  58. DateBUF = Int(20.851 + 0.242194 * (CurYear - 1980) - Int((CurYear - 1980) / 4))
  59. End Select
  60. lKyujitu(iCount) = DateSerial(CurYear, 3, DateBUF)
  61. '⑤天皇誕生日→みどりの日→昭和の日
  62. iCount = iCount + 1
  63. ReDim Preserve lKyujitu(iCount)
  64. lKyujitu(iCount) = DateSerial(CurYear, 4, 29)
  65.  
  66. '⑥憲法記念日
  67. iCount = iCount + 1
  68. ReDim Preserve lKyujitu(iCount)
  69. lKyujitu(iCount) = DateSerial(CurYear, 5, 3)
  70.  
  71. '⑦みどりの日
  72. If CurYear > 2006 Then
  73. iCount = iCount + 1
  74. ReDim Preserve lKyujitu(iCount)
  75. lKyujitu(iCount) = DateSerial(CurYear, 5, 4)
  76. End If
  77.  
  78. '⑧こどもの日
  79. iCount = iCount + 1
  80. ReDim Preserve lKyujitu(iCount)
  81. lKyujitu(iCount) = DateSerial(CurYear, 5, 5)
  82.  
  83. '⑨海の日 7月20日 → 7月の第3月曜日
  84. If CurYear > 1995 And CurYear < 2003 Then
  85. iCount = iCount + 1
  86. ReDim Preserve lKyujitu(iCount)
  87. lKyujitu(iCount) = DateSerial(CurYear, 7, 20)
  88. ElseIf CurYear > 2002 Then
  89. iCount = iCount + 1
  90. ReDim Preserve lKyujitu(iCount)
  91. lKyujitu(iCount) = DateSerial(CurYear, 7, 15) + ((9 - Weekday(DateSerial(CurYear, 7, 15))) Mod 7)
  92. End If
  93.  
  94. '⑪山の日
  95. If CurYear > 2015 Then
  96. iCount = iCount + 1
  97. ReDim Preserve lKyujitu(iCount)
  98. lKyujitu(iCount) = DateSerial(CurYear, 8, 11)
  99. End If
  100.  
  101. '⑫敬老の日 9月15日 → 9月の第3月曜日
  102. If CurYear > 1965 And CurYear < 2003 Then
  103. iCount = iCount + 1
  104. ReDim Preserve lKyujitu(iCount)
  105. lKyujitu(iCount) = DateSerial(CurYear, 9, 15)
  106. ElseIf CurYear > 2002 Then
  107. iCount = iCount + 1
  108. ReDim Preserve lKyujitu(iCount)
  109. lKyujitu(iCount) = DateSerial(CurYear, 9, 15) + ((9 - Weekday(DateSerial(CurYear, 9, 15))) Mod 7)
  110. End If
  111.  
  112. '⑬秋分の日
  113. iCount = iCount + 1
  114. ReDim Preserve lKyujitu(iCount)
  115. 'int(22.2588+0.242194*(年-1980)-int((年-1983)/4)) ----------1851-1899年通用
  116. 'int(23.2588+0.242194*(年-1980)-int((年-1983)/4)) ----------1900-1979年通用
  117. 'int(23.2488+0.242194*(年-1980)-int((年-1980)/4)) ----------1980-2099年通用
  118. 'int(24.2488+0.242194*(年-1980)-int((年-1980)/4)) ----------2100-2150年通用
  119. Select Case CurYear
  120. Case Is < 2100
  121. DateBUF = Int(23.2488 + 0.242194 * (CurYear - 1980) - Int((CurYear - 1980) / 4))
  122. Case Is >= 2100
  123. DateBUF = Int(24.2488 + 0.242194 * (CurYear - 1980) - Int((CurYear - 1980) / 4))
  124. End Select
  125. lKyujitu(iCount) = DateSerial(CurYear, 9, DateBUF)
  126. If CurYear > 1965 And CurYear < 2000 Then '----------体育の日 10月10日 → 10月の第二月曜日
  127. iCount = iCount + 1
  128. ReDim Preserve lKyujitu(iCount)
  129. lKyujitu(iCount) = DateSerial(CurYear, 10, 10)
  130. ElseIf CurYear > 1999 Then
  131. iCount = iCount + 1
  132. ReDim Preserve lKyujitu(iCount)
  133. lKyujitu(iCount) = DateSerial(CurYear, 10, 8) + ((9 - Weekday(DateSerial(CurYear, 10, 8))) Mod 7)
  134. End If
  135.  
  136. '⑭文化の日
  137. iCount = iCount + 1
  138. ReDim Preserve lKyujitu(iCount)
  139. lKyujitu(iCount) = DateSerial(CurYear, 11, 3)
  140.  
  141. '⑮勤労感謝の日
  142. iCount = iCount + 1
  143. ReDim Preserve lKyujitu(iCount)
  144. lKyujitu(iCount) = DateSerial(CurYear, 11, 23)
  145.  
  146. '⑯天皇誕生日
  147. If CurYear > 1988 Then
  148. iCount = iCount + 1
  149. ReDim Preserve lKyujitu(iCount)
  150. lKyujitu(iCount) = DateSerial(CurYear, 12, 23)
  151. End If
  152. '----------祝祭休日の判定
  153. For I = 1 To UBound(lKyujitu())
  154. If lKyujitu(I) = lDate Then
  155. ResWeekNum = 1
  156. End If
  157. Next I
  158. End If
  159. '----------振り替え休日の判定
  160. If ResWeekNum <> 1 And CurYear > 1972 Then
  161. '----------値の初期化
  162. iCount = 0
  163. FLG = False
  164. DateBUF = 0
  165. For lDay = DateSerial(CurYear, 1, 1) To DateSerial(CurYear, 12, 31)
  166.  
  167. If Weekday(lDay) = vbSunday Then
  168. For I = 1 To UBound(lKyujitu())
  169. If lKyujitu(I) = lDay Then '----------日曜日で祝日であること
  170. FLG = True
  171. DateBUF = lDay
  172. End If
  173. Next I
  174. End If
  175. If FLG = True And lDay = DateBUF + 1 Then '----------翌日の判定
  176. FLG = False
  177. DateBUF = 0
  178. For I = 1 To UBound(lKyujitu()) '----------祝日のチェック
  179. If lKyujitu(I) = lDay Then
  180. FLG = True '----------祝休日該当
  181. DateBUF = lDay
  182. End If
  183. Next I
  184. If FLG = False Then '----------祝日に該当しない場合、振替日にする
  185. iCount = iCount + 1
  186. ReDim Preserve lHurikae(iCount)
  187. lHurikae(iCount) = lDay
  188. FLG = False
  189. DateBUF = 0
  190. End If
  191. End If
  192. Next lDay
  193.  
  194. If UBound(lHurikae()) > 0 Then '----------振り替え休日の判定
  195. For I = 1 To UBound(lHurikae())
  196. If lHurikae(I) = lDate Then
  197. ResWeekNum = 1
  198. End If
  199. Next I
  200. End If
  201. End If
  202. '----------国民の休日の判定
  203. If ResWeekNum <> 1 And CurYear > 1987 Then
  204. iCount = 0 '----------値の初期化
  205. For I = 1 To UBound(lKyujitu()) - 1
  206. For J = I + 1 To UBound(lKyujitu())
  207. If Abs(lKyujitu(J) - lKyujitu(I)) = 2 Then '----------挟まれた日が休日かどうかチェックします
  208. FLG = False
  209. For k = 1 To iCount
  210. If lKyujitu(k) = (lKyujitu(I) + lKyujitu(J)) / 2 Then
  211. FLG = True
  212. End If
  213. Next k
  214. If FLG = False Then '----------挟まれた日が休日でない場合その日を追加登録します
  215. iCount = iCount + 1
  216. ReDim Preserve lKokumin(iCount)
  217. lKokumin(iCount) = (lKyujitu(I) + lKyujitu(J)) / 2
  218. End If
  219. End If
  220. Next J
  221. Next I
  222. If UBound(lKokumin()) > 0 Then '----------国民の休日の判定
  223. For I = 1 To UBound(lKokumin())
  224. If lKokumin(I) = lDate Then
  225. ResWeekNum = 1
  226. End If
  227. Next I
  228. End If
  229. End If
  230. '----------慣例になっている休日の判定
  231. If ResWeekNum <> 1 Then
  232. ReDim Preserve lKanrei(3)
  233. lKanrei(1) = DateSerial(CurYear, 1, 2)
  234. lKanrei(2) = DateSerial(CurYear, 1, 3)
  235. lKanrei(3) = DateSerial(CurYear, 12, 31)
  236.  
  237. For I = 1 To UBound(lKanrei())
  238. If lKanrei(I) = lDate Then
  239. ResWeekNum = 1
  240. End If
  241. Next I
  242. End If
  243.  
  244. Erase lHurikae()
  245. Erase lKyujitu()
  246. Erase lKokumin()
  247. Erase lKanrei()
  248. Kyujitu = ResWeekNum
  249.  
  250. End Function

Shapeの再帰処理



   

COPY

  1. Public EndFlg As Boolean
  2.  
  3. Public Sub ShapeAct(ByVal Flg As String, ByRef Sh As Worksheet)
  4. Dim SP As Shape
  5.  
  6. EndFlg = False
  7. For Each SP In Sh.Shapes
  8. Call ShapeAct2(SP, Flg)
  9. Next SP
  10.  
  11. End Sub
  12.  
  13. Private Sub ShapeAct2(ByRef SP As Shape, ByVal Flg As String)
  14. Dim SP2 As Shape
  15.  
  16. If EndFlg Then
  17. Exit Sub
  18. End If
  19.  
  20. Select Case SP.Type
  21. Case msoGroup
  22.  
  23. For Each SP2 In SP.GroupItems
  24. Call ShapeAct2(SP2, Flg)
  25. Next SP2
  26.  
  27. Case msoOLEControlObject
  28.  
  29. With SP.OLEFormat
  30. If TypeName(.Object.Object) = "CommadButton" Then
  31. With .Object.Object
  32. If .Caption = "編集" Then
  33.  
  34. EndFlg = True
  35.  
  36. Select Case Flg
  37. Case "AA"
  38. If .ForeColor = RGB(0, 0, 255) Then
  39. .ForeColor = RGB(255, 0, 0)
  40. Call DeleteShape(SP.Parent)
  41. End If
  42. Case "BB"
  43. If .ForeColor = RGB(0, 0, 255) Then
  44. .ForeColor = RGB(255, 0, 0)
  45. Call DeleteShape(SP.Parent)
  46. Else
  47. .ForeColor = RGB(0, 0, 255)
  48. Call CellForm("ボタン")
  49. End If
  50. Case Else
  51. '何もしません
  52. End Select
  53.  
  54. Else
  55. '何もしません
  56. End If
  57. End With
  58. Else
  59. '何もしません
  60. End If
  61. End With
  62.  
  63. Case msoTextBox
  64. '何もしません
  65. Case Else
  66. '何もしません
  67. End Select
  68.  
  69. End Sub

グループ化されたシェイプの処理



   

COPY

  1. Public Sub DeleteShape(ByRef Sh As Worksheet)
  2. Dim Shp() As String
  3. Dim SP As Shape
  4. Dim objRange As Object
  5. Dim iCount As Long
  6. Dim Flg As Boolean
  7.  
  8. 'シェイプがなければ抜ける
  9. If Sh.Shapes.Count = 0 Then
  10. Exit Sub
  11. End If
  12.  
  13. 'シェイプの数に合わせて配列の添え字を定義
  14. ReDim Shp(1 To Sh.Shapes.Count)
  15.  
  16. 'ターゲットとするシェイプの名前を取得する
  17. iCount = 0
  18. For Each SP In Sh.Shapes
  19. If InStr(1, SP.Name, "SHP") > 0 Then
  20. iCount = iCount + 1
  21. Shp(iCount) = Sh.Name
  22. End If
  23. Next SP
  24.  
  25. '対象がなければ抜ける
  26. If iCount = 0 Then
  27. Exit Sub
  28. End If
  29.  
  30. '取得した名前の数に合わせて配列の添え字を修正
  31. ReDim Preserve Shp(1 To iCount)
  32.  
  33. 'シェイプの集合体を取得
  34. Set objRange = Sh.Shapes.Range(Shp)
  35.  
  36. '集合体を一括削除
  37. If Not objRange Is Nothing Then
  38. objRange.Select
  39. objRange.Delete
  40. Set objRange = Nothing
  41. End If
  42.  
  43. End Sub

アイコンのアクションの引数



   

COPY

  1. Public Sub DispIcon(ByRef Sh As Worksheet, _
  2. ByRef Target As Range)
  3. Dim Shp As Object
  4. Dim Flg As Boolean
  5.  
  6. If Target.Count <> 1 Then
  7. Exit Sub
  8. End If
  9.  
  10. If Target.Address = Sh.Cells(1, 1).Address Then
  11. Exit Sub
  12. End If
  13.  
  14. Set Shp = Sh.Shapes.AddPicture(ThisWorkbook.Path & "\INFOML.ICO", _
  15. False, _
  16. True, _
  17. Target.Offset(0, 1).Left + 5, _
  18. Target.Offset(0, 1).Top + 2, _
  19. 15, _
  20. 15)
  21.  
  22. With Shp
  23. .Name = "SHP_ICO"
  24. .OnAction = "'CellForm ""引数""'"
  25. .Placement = xlMove
  26. .Locked = msoFalse
  27. End With
  28. End Sub