Sub ExeCreateCalendar18()
Dim dtTp As Date
Dim c As Long
dtTp = dtFm
c = 0
Dim shSummary As Worksheet
Set shSummary = Worksheets("Summary")
Dim lnMx As Long
lnMx = shSummary.Range("A65536").End(xlUp).Row + 1
Dim rgSummary As Range
Set rgSummary = shSummary.Range("A" & lnMx)
Dim cYoko As Long
Dim rgCtrl As Range
With Range("A2") 'with?X?e?[?g?????g?@
Do While Month(dtTp) = Month(dtFm)
.Offset(c).Value = dtTp
.Offset(c, 1).Value = WeekdayName(Weekday(dtTp))
.Offset(c, 2).Value = #9:00:00 AM#
.Offset(c, 3).Value = #5:00:00 PM#
.Offset(c, 4).Formula = "=" & .Offset(c, 3).Address & "-" & .Offset(c, 2).Address
For cYoko = 0 To 4
rgSummary.Offset(c, cYoko).Formula = "='" & .Worksheet.Name & "'!" & .Offset(c, cYoko).Address
Next
With Worksheets("Control").Range("A1").Offset(Weekday(dtTp)) 'with?\????????????????????????
With Range(.Offset(c), .Offset(c, 4)) 'with?X?e?[?g?????g?A
.Interior.ColorIndex = .Interior.ColorIndex
.Font.ColorIndex = .Font.ColorIndex
End With
With Range(rgSummary.Offset(c, 0), rgSummary.Offset(c, 4))
.Interior.ColorIndex = .Interior.ColorIndex
.Font.ColorIndex = .Font.ColorIndex
End With
End With
dtTp = DateAdd("d", 1, dtTp)
c = c + 1
Loop
End With
End Sub
2020/05/31 21:36
受講生さんのコメント
(コメントID: 6628)
申し訳ございません、先ほどの投稿内に一部文字化けしていたので再送いたします。
Sub ExeCreateCalendar18()
Dim dtTp As Date
Dim c As Long
dtTp = dtFm
c = 0
Dim shSummary As Worksheet
Set shSummary = Worksheets("Summary")
Dim lnMx As Long
lnMx = shSummary.Range("A65536").End(xlUp).Row + 1
Dim rgSummary As Range
Set rgSummary = shSummary.Range("A" & lnMx)
Dim cYoko As Long
Dim rgCtrl As Range
With Range("A2") 'with構文①
Do While Month(dtTp) = Month(dtFm)
.Offset(c).Value = dtTp
.Offset(c, 1).Value = WeekdayName(Weekday(dtTp))
.Offset(c, 2).Value = #9:00:00 AM#
.Offset(c, 3).Value = #5:00:00 PM#
.Offset(c, 4).Formula = "=" & .Offset(c, 3).Address & "-" & .Offset(c, 2).Address
For cYoko = 0 To 4
rgSummary.Offset(c, cYoko).Formula = "='" & .Worksheet.Name & "'!" & .Offset(c, cYoko).Address
Next
With Worksheets("Control").Range("A1").Offset(Weekday(dtTp)) 'with構文でまとめてみるも正しく挙動せず
With Range(.Offset(c), .Offset(c, 4)) 'with構文②
.Interior.ColorIndex = .Interior.ColorIndex
.Font.ColorIndex = .Font.ColorIndex
End With
With Range(rgSummary.Offset(c, 0), rgSummary.Offset(c, 4))
.Interior.ColorIndex = .Interior.ColorIndex
.Font.ColorIndex = .Font.ColorIndex
End With
End With
dtTp = DateAdd("d", 1, dtTp)
c = c + 1
Loop
End With
End Sub
2020/05/31 21:40
小川慶一さんのコメント
(コメントID: 6630)
ドット「 . 」ではじまる言葉は、「 . 」より手前に来るべき言葉が省略されています。
では、何が省略されているのか?というと。 その言葉が出てくる【直前】で出てきた With 句の With 以降で示されたオブジェクトの指定でした。
ということで。 以下、説明に必要な箇所だけ抜き出し、さらにそのマクロを改変してから、解説します。
With Range("A2")
Do ...
With Worksheets("Control").Range("A1").Offset(Weekday(dtTp))
With Range(.Offset(c), .Offset(c, 4))
.Interior.ColorIndex = .Interior.ColorIndex
.Font.ColorIndex = .Font.ColorIndex
End With
End With
Loop
End With
Do ... Loop は今回の話に無関係なのではずします。 ↓
With Range("A2")
dtTp = #2020/05/31#
With Worksheets("Control").Range("A1").Offset(Weekday(dtTp))
With Range(.Offset(c), .Offset(c, 4))
.Interior.ColorIndex = .Interior.ColorIndex
.Font.ColorIndex = .Font.ColorIndex
End With
End With
End With
Dim rg2 As Range
With Range("A2")
dtTp = #2020/05/31#
Set rg2 = Worksheets("Control").Range("A1").Offset(Weekday(dtTp))
With rg2
With Range(.Offset(c), .Offset(c, 4))
.Interior.ColorIndex = .Interior.ColorIndex
.Font.ColorIndex = .Font.ColorIndex
End With
End With
End With
話を簡単にするため、3つ目のWithの中も書き換えておきましょう。 ↓
Dim rg2 As Range
Dim rg3 As Range
With Range("A2")
dtTp = #2020/05/31#
Set rg2 = Worksheets("Control").Range("A1").Offset(Weekday(dtTp))
With rg2
Set rg3 = Range(.Offset(c), .Offset(c, 4))
With rg3
.Interior.ColorIndex = .Interior.ColorIndex '[1-1]
.Font.ColorIndex = .Font.ColorIndex '[1-2]
End With
End With
End With
With Range("A2")
> Do ...
> With Worksheets("Control").Range("A1").Offset(Weekday(dtTp))
> With Range(.Offset(c), .Offset(c, 4))
> .Interior.ColorIndex = .Interior.ColorIndex
> .Font.ColorIndex = .Font.ColorIndex
> End With
> End With
> Loop
> End With
> > Do ... Loop は今回の話に無関係なのではずします。 > ↓ > >
With Range("A2")
> dtTp = #2020/05/31#
> With Worksheets("Control").Range("A1").Offset(Weekday(dtTp))
> With Range(.Offset(c), .Offset(c, 4))
> .Interior.ColorIndex = .Interior.ColorIndex
> .Font.ColorIndex = .Font.ColorIndex
> End With
> End With
> End With
Dim rg2 As Range
> With Range("A2")
> dtTp = #2020/05/31#
> Set rg2 = Worksheets("Control").Range("A1").Offset(Weekday(dtTp))
> With rg2
> With Range(.Offset(c), .Offset(c, 4))
> .Interior.ColorIndex = .Interior.ColorIndex
> .Font.ColorIndex = .Font.ColorIndex
> End With
> End With
> End With
> > 話を簡単にするため、3つ目のWithの中も書き換えておきましょう。 > ↓ >
Dim rg2 As Range
> Dim rg3 As Range
> With Range("A2")
> dtTp = #2020/05/31#
> Set rg2 = Worksheets("Control").Range("A1").Offset(Weekday(dtTp))
> With rg2
> Set rg3 = Range(.Offset(c), .Offset(c, 4))
> With rg3
> .Interior.ColorIndex = .Interior.ColorIndex '[1-1]
> .Font.ColorIndex = .Font.ColorIndex '[1-2]
> End With
> End With
> End With
受講生さんの投稿
(投稿ID: 4749)
一連のカレンダー作成マクロにて少しずつ理解しながらも四苦八苦している日々です。。。
その中での質問なのですが、今回の講座で最後に説明頂いている「Worksheets("Control").Range("A1")」を書き直す部分にて、with構文を利用して後述のマクロを書いてみました。が、正しく挙動せず・・・。
恐らくwith構文内にて自身の指定したいオブジェクトが、別のwith構文によってテレコになっているからなのかと思います。
それを踏まえてなのですが、後述マクロ内with構文①とwith構文②は構文内に構文が含まれる形となっておりますが、なぜこちらは意図する通りそれぞれ正しくオブジェクトが指定されるのでしょうか。
説明下手かつ理解不足で恐縮ですがご教示頂けると幸いです。
受講生さんのコメント
(コメントID: 6628)
小川慶一さんのコメント
(コメントID: 6630)
では、何が省略されているのか?というと。
その言葉が出てくる【直前】で出てきた With 句の With 以降で示されたオブジェクトの指定でした。
ということで。
以下、説明に必要な箇所だけ抜き出し、さらにそのマクロを改変してから、解説します。
Do ... Loop は今回の話に無関係なのではずします。
↓
話を簡単にするため、2つ目のWithの中を書き換えておきます。
(一般に、長い言葉は短い言葉に置き換えたほうが構造がわかりやすい)
↓
話を簡単にするため、3つ目のWithの中も書き換えておきましょう。
↓
ということで、事前準備終わり。
以下、事例を使っての解説です。
ここで、たとえば [1-1] について、受講生さんは以下のように解釈されることを期待していたのではないかと思いますが..。
残念ながら、以下のように解釈されます。(右辺が、 rg2 ではなく、 rg3 )
理由は、冒頭に述べたとおり。 ドット「 . 」ではじまる言葉は、その言葉が出てくる【直前】で出てきた With 句で指定されたオプジェクトを指しているからです。
ということで、どうでしょうか。
たぶんこれで伝わるかな、と思うのですが...。
受講生さんのコメント
(コメントID: 6631)
返信ありがとうございます。
with構文内にて新たなwith句が出てきた場合、指定されるオブジェクトは上書きされてしまうんですね。
大変わかりやすい説明ありがとうございました。
> ドット「 . 」ではじまる言葉は、「 . 」より手前に来るべき言葉が省略されています。
>
> では、何が省略されているのか?というと。
> その言葉が出てくる【直前】で出てきた With 句の With 以降で示されたオブジェクトの指定でした。
>
> ということで。
> 以下、説明に必要な箇所だけ抜き出し、さらにそのマクロを改変してから、解説します。
>
>
>
> Do ... Loop は今回の話に無関係なのではずします。
> ↓
>
>
>
> 話を簡単にするため、2つ目のWithの中を書き換えておきます。
> (一般に、長い言葉は短い言葉に置き換えたほうが構造がわかりやすい)
> ↓
>
>
> 話を簡単にするため、3つ目のWithの中も書き換えておきましょう。
> ↓
>
>
> ということで、事前準備終わり。
> 以下、事例を使っての解説です。
>
> ここで、たとえば [1-1] について、受講生さんは以下のように解釈されることを期待していたのではないかと思いますが..。
>
>
>
> 残念ながら、以下のように解釈されます。(右辺が、 rg2 ではなく、 rg3 )
> 理由は、冒頭に述べたとおり。 ドット「 . 」ではじまる言葉は、その言葉が出てくる【直前】で出てきた With 句で指定されたオプジェクトを指しているからです。
>
>
>
> ということで、どうでしょうか。
> たぶんこれで伝わるかな、と思うのですが...。
>
小川慶一さんのコメント
(コメントID: 6632)
理解につながったようで良かったです。
> with構文内にて新たなwith句が出てきた場合、指定されるオブジェクトは上書きされてしまうんですね。
ルールはそれしかないので、シンプルですね。
ひきつづき、お楽しみください☆