###前提・実現したいこと
VBAでタスクをガントチャートに反映するシステムを作っています。
コードの中でlと言う変数があるのですが、図のように2つ目(2行目)のタスクを60分に設定して登録ボタンを押すとlの値が9になり、一番目のタスク(着色されているタスク)の右下から始めることができません。
本来であれば、この時lの値は8となり、一番目のタスクの右下から2番目のタスクが着色されるようにしたいです。
Private Sub ComboBox1_Change() End Sub Private Sub CommandButton1_Click() Dim i For i = 10 To 60 Step 10 ComboBox1.AddItem i & "分" Next End Sub Private Sub CommandButton2_Click() Cells.Select Selection.ColumnWidth = 2 If Cells(1, 1) = "" Then Worksheets("Task").Cells(1, 1).Value = TextBox1.Value Columns("A:E").AutoFit If ComboBox1.Text = "10分" Then Cells(1, 2).Interior.ColorIndex = 3 Cells(1, 2).Value = "□" ElseIf ComboBox1.Text = "20分" Then Range(Cells(1, 2), Cells(1, 3)).Interior.ColorIndex = 3 Range(Cells(1, 2), Cells(1, 3)).Value = "□" ElseIf ComboBox1.Text = "30分" Then Range(Cells(1, 2), Cells(1, 4)).Interior.ColorIndex = 3 Range(Cells(1, 2), Cells(1, 4)).Value = "□" ElseIf ComboBox1.Text = "40分" Then Range(Cells(1, 2), Cells(1, 5)).Interior.ColorIndex = 3 Range(Cells(1, 2), Cells(1, 5)).Value = "□" ElseIf ComboBox1.Text = "50分" Then Range(Cells(1, 2), Cells(1, 6)).Interior.ColorIndex = 3 Range(Cells(1, 2), Cells(1, 6)).Value = "□" ElseIf ComboBox1.Text = "60分" Then Range(Cells(1, 2), Cells(1, 7)).Interior.ColorIndex = 3 Range(Cells(1, 2), Cells(1, 7)).Value = "□" End If Else 'MsgBox Maxcol n = Cells(Rows.Count, 1).End(xlUp).Row + 1 MsgBox n With Range("A" & n - 1).SpecialCells(xlLastCell) MaxRow = .Row l = .Column + 1 End With MsgBox l '最終行を求めてしまっている↓ ''m = Range("A" & n - 1).End(xlToRight).Column + 1 'm = Range("A" & Columns.Count).End(xlToLeft).Column + 1 'm = Cells.SpecialCells(xlCellTypeLastCell).Offset(1, 1) ''MsgBox m Worksheets("Task").Cells(n, 1).Value = TextBox1.Value Columns("A:E").AutoFit If ComboBox1.Text = "10分" Then Cells(n, l).Interior.ColorIndex = 3 'Range(n, m).Value = "□" Cells(n, l).Value = "□" ElseIf ComboBox1.Text = "20分" Then Range(Cells(n, l), Cells(n, l + 1)).Interior.ColorIndex = 3 Range(Cells(n, l), Cells(n, l + 1)).Value = "□" ElseIf ComboBox1.Text = "30分" Then Range(Cells(n, l), Cells(n, l + 2)).Interior.ColorIndex = 3 Range(Cells(n, l), Cells(n, l + 2)).Value = "□" ElseIf ComboBox1.Text = "40分" Then Range(Cells(n, l), Cells(n, l + 3)).Interior.ColorIndex = 3 Range(Cells(n, l), Cells(n, l + 3)).Value = "□" ElseIf ComboBox1.Text = "50分" Then Range(Cells(n, l), Cells(n, l + 4)).Interior.ColorIndex = 3 Range(Cells(n, l), Cells(n, l + 4)).Value = "□" ElseIf ComboBox1.Text = "60分" Then Range(Cells(n, l), Cells(n, l + 5)).Interior.ColorIndex = 3 Range(Cells(n, l), Cells(n, l + 5)).Value = "□" End If End If End Sub Private Sub Label2_Click() End Sub Private Sub TextBox1_Change() End Sub Private Sub UserForm_Click() End Sub Private Sub UserForm_Initialize() UserForm1.Show End Sub
###試したこと
msgbox関数でlの値を確認したところ、2回目にタスクを登録しようとすると"9"が格納されており、その原因と理由がわからない状態です。
分かる方、ご教示お願いいたします。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。