次のデータを折れ線グラフにしたいのですが、その際、
.Axes(xlcategory).MinimumScale = 8
.Axes(xlcategory).MaximumScale = 70
.Axes(xlcategory).MajorUnit = 5
で作成したグラフの横軸の最小値、最大値、目盛り幅を変更しようとすると、
「minimumscale'メソッドは失敗しました 'axis'オブジェクト」
とエラーが出ます。
この3行分をなくすときちんとグラフは作成されました(画像はその時のものです)。
マクロのコードとシートの画像は以下の通りです。
最近マクロを使い始めたのですが、新しい取り組みでわからないことが多いのでどなたかご教授いただけますと幸いです。よろしくお願いいたします。
Sub pressure() Dim last As Long Dim first_sw_s As Single Dim first_sw_l As Single Dim sec_sw_l As Single Dim sw_max_1 As Single Dim sw_max_2 As Single Dim Calstart As Single Dim Callast As Single Dim Graph As Variant start = 1 last = Cells(Rows.Count, "C").End(xlUp).Row '衝撃波地点探査 first_sw_s = Cells(10, "B").Value '第一衝撃波の開始点 first_sw_l = Cells(12, "B").Value '第一衝撃波の終点 sec_sw_l = Cells(14, "B").Value '第二衝撃波の終点 For i = 2 To last If Cells(i, "C").Value * 1000000 >= first_sw_s Then s = i Exit For End If Next For i = s To last If Cells(i, "C").Value * 1000000 >= first_sw_l Then t = i Exit For End If Next For i = t To last If Cells(i, "C").Value * 1000000 >= sec_sw_l Then u = i Exit For End If Next sw_max_1 = WorksheetFunction.Max(Range(Cells(s, "D"), Cells(t, "D"))) sw_max_2 = WorksheetFunction.Max(Range(Cells(t, "D"), Cells(u, "D"))) sw_min = WorksheetFunction.Min(Range(Cells(t, "D"), Cells(u, "D"))) Cells(23, "A").Value = sw_max_1 Cells(24, "A").Value = sw_max_2 Cells(25, "A").Value = sw_min For i = 2 To last If Cells(i, "D").Value = sw_min Then Cells(25, "B").Value = Cells(i, "C") End If Next For i = 2 To last If Cells(i, "D").Value = sw_max_1 Then Cells(23, "B").Value = Cells(i, "C") End If Next '------------------------------井上が追加------------------------------------------------------------------------ Calstart = Cells(6, "B").Value Callast = Cells(8, "B").Value For i = 2 To last If Cells(i, "C").Value * 1000000 >= Calstart Then Calstart = i Exit For End If Next For k = Calstart To last If Cells(k, "C").Value * 1000000 >= Callast Then Callast = k Exit For End If Next n = Callast - Calstart j = 0 ReDim Graph(n, 1) For h = Calstart To Callast Graph(j, 0) = Cells(h, "C").Value * 1000000 Graph(j, 1) = Cells(h, "D").Value j = j + 1 Next Range("K1").Resize(n + 1, 2) = Graph ' グラフの位置 gpos_x = 300 gpos_y = 50 ' グラフのサイズ g_width = 800 g_height = 300 graph_name = "my_graph" y_offset = 1 x_offset = 12 series_num = 1 series_names = Array("圧力") chart_title = "Pressure" x_title = "time[μs]" y_title = "Pressure[MPa]" ' アクティブシート上に既存のグラフがあれば削除 If ActiveSheet.ChartObjects.Count > 0 Then For i = 1 To ActiveSheet.ChartObjects.Count ' グラフ名が一致するか If ActiveSheet.ChartObjects(i).Name = graph_name Then ActiveSheet.ChartObjects(i).Delete Exit For End If Next i End If ' データ範囲を決定 (何行目まであるのか) y_temp = y_offset continue_flag = True Do While continue_flag = True ' この行に内容があれば次の行へ進む If Len(Cells(y_temp, x_offset).Value) > 0 Then y_temp = y_temp + 1 Else continue_flag = False End If Loop y_end = y_temp x_end = x_offset + series_num - 1 ' グラフを描画 ' http://brain.cc.kogakuin.ac.jp/~kanamaru/lecture/vba2003/13-applications02.html ' http://www.officepro.jp/excelvba/chart_edit/index2.html Dim chartObj As ChartObject Set chartObj = ActiveSheet.ChartObjects.Add( _ gpos_x, gpos_y, g_width, g_height _ ) chartObj.Name = graph_name With chartObj.Chart ' データ範囲をセット(左上と右下) .SetSourceData ActiveSheet.Range( _ Cells(y_offset, x_offset), _ Cells(y_end, x_end) _ ), xlColumns ' x軸の項目軸範囲をセット .SeriesCollection(1).XValues = Range( _ Cells(y_offset, x_offset - 1), _ Cells(y_end, x_offset - 1) _ ) ' オプションをセット .ChartType = xlLine ' 折れ線 .HasTitle = True .ChartTitle.Characters.Text = chart_title .Axes(xlcategory, xlPrimary).HasTitle = True .Axes(xlcategory, xlPrimary).AxisTitle.Characters.Text = x_title .Axes(xlValue, xlPrimary).HasTitle = True .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = y_title .Axes(xlcategory).MinimumScale = 8 .Axes(xlcategory).MaximumScale = 70 .Axes(xlcategory).MajorUnit = 5 ' 系列名をセット For i = 1 To series_num .SeriesCollection(i).Name = series_names(i - 1) Next i End With End Sub
回答1件
あなたの回答
tips
プレビュー