実現したいこと
ガントチャートを作成しており、カレンダーの日付に合わせたサイズのテキストボックスを複数挿入するところまではうまくいったのですが、各テキストボックスの中のテキスト(文字)を変数として設定し、それぞれに挿入することがうまくいかないのでこれを実現したいと考えています。アドバイスいただけますと幸いです。
発生している問題・分からないこと
With .TextFrame
.Characters.Text = x
のxに変数として設定し、一応認識しているようですが、挿入した複数のテキストボックスすべてに最下行の文字列が挿入されております。
該当のソースコード
Sub テキストボックス() Application.ScreenUpdating = False Application.Cursor = xlWait Application.EnableEvents = False Application.DisplayAlerts = False Application.Calculation = xlCalculationManual Dim sh1 As Worksheet Set sh1 = Worksheets("Gantt") Dim b, S, E, i b = sh1.Range("Q4") For i = 7 To sh1.Cells(Rows.Count, "M").End(xlUp).row If Cells(i, "M") <> "" And Cells(i, "N") <> "" Then Set S = sh1.Cells(i, "Q").Offset(0, DateDiff("d", b, Cells(i, "M"))) Set E = sh1.Cells(i, "Q").Offset(0, DateDiff("d", b, Cells(i, "N"))) Call 挿入(S, E) End If Next Application.StatusBar = False Application.Calculation = xlCalculationAutomatic Application.DisplayAlerts = True Application.EnableEvents = True Application.Cursor = xlDefault Application.ScreenUpdating = True End Sub Sub 挿入(S, E) Application.ScreenUpdating = False Application.Cursor = xlWait Application.EnableEvents = False Application.DisplayAlerts = False Application.Calculation = xlCalculationManual Set sh1 = Worksheets("Gantt") Dim i As Long For i = 7 To sh1.Cells(Rows.Count, "B").End(xlUp).row x = sh1.Cells(i, "B").value With sh1.Shapes.AddTextbox(msoTextOrientationHorizontal, S.Left, S.Top, (E.Left + E.Width) - S.Left, E.Height) With .TextFrame .Characters.Text = x .Characters.Font.Color = RGB(0, 0, 0) .HorizontalAlignment = xlHAlignCenter .VerticalAlignment = xlVAlignCenter End With With .Fill .ForeColor.ObjectThemeColor = msoThemeColorAccent1 .ForeColor.Brightness = 0.800000011 End With End With Next i Application.StatusBar = False Application.Calculation = xlCalculationAutomatic Application.DisplayAlerts = True Application.EnableEvents = True Application.Cursor = xlDefault Application.ScreenUpdating = True End Sub
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
x = sh1.Cells(i, "B").valueかxの書く位置が悪いのではないかといろいろ変更しましたがうまくいきません。
補足
特になし
> Set sh1 = Worksheets("Gantt")
ワークシート[Gantt]の構造、およびそれを前提とした自動処理の仕様が不明瞭です。
> b = sh1.Range("Q4")
・Q4 セルは何の値を入力するためのセルか。
> For i = 7 To sh1.Cells(Rows.Count, "M").End(xlUp).row
> Set S = sh1.Cells(i, "Q").Offset(0, DateDiff("d", b, Cells(i, "M")))
> Set E = sh1.Cells(i, "Q").Offset(0, DateDiff("d", b, Cells(i, "N")))
> For i = 7 To sh1.Cells(Rows.Count, "B").End(xlUp).row
・7行目以降の B 列、M 列、N 列および Q 列のセルのそれぞれの役割は何なのか。
> 各テキストボックスの中のテキスト(文字)を変数として設定し、それぞれに挿入する
・作成したテキストボックスに代入したいのはどんな文字列なのか。
また、何のために変数を使用するのか。
とりあえず、以上の点について具体的に明記されることをお奨めします。
言葉足らずですみません。
> Set sh1 = Worksheets("Gantt")
ワークシート[Gantt]の構造、およびそれを前提とした自動処理の仕様が不明瞭です。
> b = sh1.Range("Q4")
・Q4 セルは何の値を入力するためのセルか。
A: Q4から横軸に日別のカレンダーを配置しています。
> For i = 7 To sh1.Cells(Rows.Count, "M").End(xlUp).row
> Set S = sh1.Cells(i, "Q").Offset(0, DateDiff("d", b, Cells(i, "M")))
> Set E = sh1.Cells(i, "Q").Offset(0, DateDiff("d", b, Cells(i, "N")))
> For i = 7 To sh1.Cells(Rows.Count, "B").End(xlUp).row
・7行目以降の B 列、M 列、N 列および Q 列のセルのそれぞれの役割は何なのか。
A: B=名称 M=日付 N=日付
> 各テキストボックスの中のテキスト(文字)を変数として設定し、それぞれに挿入する
・作成したテキストボックスに代入したいのはどんな文字列なのか。
また、何のために変数を使用するのか。
A: 挿入したいのは文字列です。変数を使用したいのは挿入箇所(位置)に対応した文字列がそれぞれ異なるためです。
とりあえず、以上の点について具体的に明記されることをお奨めします。

回答2件
あなたの回答
tips
プレビュー