| 
    
     |  | ▼tara さん: >>Set 項目行 = Range("A1").Resize(, 5)
 >
 >ですと、田中さんの”社会”の項目までが固定で参照されてしまいます。
 >列数も可変する場合はどのように設定すればよろしいのでしょうか?
 
 keinさんが応えてますが、一応作ってみたんで
 
 Sub Macro1()
 Dim res As Variant
 Dim res2 As Variant
 Dim 項目行 As Range
 Dim 抽出行 As Range
 Dim myRange As Range
 Dim myVal As Variant
 Dim FR As Variant
 
 
 res = Application.InputBox("誰のグラフですか", Type:=2)
 If VarType(res) = vbBoolean Then Exit Sub
 FR = Application.Match(res, Range("A:A"), 0)
 If IsError(FR) Then
 MsgBox "そんなやつおらんやろ"
 Exit Sub
 End If
 
 res2 = Application.InputBox("どの教科までですか", Type:=2)
 If VarType(res2) = vbBoolean Then Exit Sub
 myVal = Application.Match(res2, Array("国語", "理科", "算数", "社会"), 0)
 If IsError(myVal) Then
 MsgBox "そんな教科はチッチキチー"
 Exit Sub
 End If
 
 Set 項目行 = Range("A1").Resize(, myVal + 1)
 Set 抽出行 = Cells(FR, "A").Resize(, myVal + 1)
 Set myRange = Union(項目行, 抽出行)
 Charts.Add
 ActiveChart.ChartType = xlColumnClustered
 ActiveChart.SetSourceData Source:=myRange, _
 PlotBy:=xlRows
 ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
 
 End Sub
 
 |  |