top of page
検索

ものづくり:スケジュール表、第三ステップ 複数行入力できるように!

執筆者の写真: Dancing ShigekoDancing Shigeko

更新日:2024年11月11日

 こんにちは、Dancing Shigekoです!


 引き続き、スケジュール表作りに挑戦中。→前回


 自動でテキストボックスを表示させられるようになったので、今度は複数行で同じように入力できるように改造。


 早速、ChatGPTのアドバイスを受けて以下のプログラムに変更。


---

Private Sub Worksheet_Change(ByVal Target As Range)

Dim txt As String

Dim tb As Shape

Dim cell As Range

Dim tempShape As Shape

Dim tempWidth As Double

Dim tempHeight As Double

Dim padding As Double

Dim maxWidth As Double

Dim totalHeight As Double

' A1:C10の変更を監視

If Not Intersect(Target, Me.Range("A1:C10")) Is Nothing Then

' テキストボックスが存在するか確認

On Error Resume Next

Set tb = Me.Shapes("MyTextBox")

On Error GoTo 0

' テキストボックスが存在しない場合は作成

If tb Is Nothing Then

Set tb = Me.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 200, 50)

tb.Name = "MyTextBox"

End If

' A1:C10の内容を取得してテキストボックスに設定

txt = ""

For Each cell In Me.Range("A1:C10")

If cell.Value <> "" Then

txt = txt & cell.Value & vbCrLf

End If

Next cell

' 最後の改行を削除

If Len(txt) > 0 Then

txt = Left(txt, Len(txt) - 2)

End If

tb.TextFrame.Characters.Text = txt

' 一時的なテキストボックスを作成してサイズを計算

Set tempShape = Me.Shapes.AddTextbox(msoTextOrientationHorizontal, 0, 0, 0, 0)

tempShape.TextFrame.Characters.Text = txt

tempShape.TextFrame.AutoSize = True

tempWidth = tempShape.Width

tempHeight = tempShape.Height

tempShape.Delete

' パディングを追加

padding = 10

' テキストボックスのサイズを設定

tb.Width = tempWidth + padding

tb.Height = tempHeight + padding

End If

End Sub

---

 

 さて、実際に入力してみた。


 ところが・・・

 二行目のセルの内容も同一のテキストボックスの中に表示される。


 期待していた作りにならなかったので、再調整。


 このあと、だいぶ苦戦するのであった。


 挑戦はまだまだ続く。


 乞うご期待!



閲覧数:0回0件のコメント

最新記事

すべて表示

国内ドラマ『アイシー~瞬間記憶捜査・柊班~』第2話 衝撃の展開、まだ信じられない

こんにちは、Dancing Shigekoです!  どんな事件に取り組んでいくのか。  今回は国内ドラマ『アイシー~瞬間記憶捜査・柊班~』第2話を紹介します! [内容] #3 オフライン・マーダー  武田が何者かに殺される。ドライブレコーダーに映っていた男性を追う。すると武...

アニメ『メジャー 第2シーズン』第2話 吾郎、動く

こんにちは、Dancing Shigekoです!  吾郎はどうやって野球を再開するのか。  今回はアニメ『メジャー 第2シーズン』第2話を紹介します! [内容] #2 夢はひとつ!  山根たちの嫌がらせで野球部の部員は戻ってこない。それでも小森は諦めずに部員集めに励む。一年...

留言


© 2023 サイト名 Wix.comを使って作成されました
当サイトの内容、テキスト、画像等の無断転載・無断使用を固く禁じます。

 
 
bottom of page