あなたがカスタム関数について留保を表明したことは理解していますが、正直なところ、ユーザーの介入またはコードの実行によって誤って操作/削除/その他される可能性のある非表示の列ではなく、それが私が取るべきルートです。
このサブルーチンは、Cells(2,2) から文字列値を取得し、以下の関数に渡すことで xlChartType に変換します。
Sub ChartTypes()
Dim cht As Chart
Dim ws As Worksheet
Dim stringType As String
Dim chartType As XlChartType
Set ws = Sheets(1)
stringType = CStr(Cells(2, 2).Value)
'Use the function to return the correct xlChartType in the AddChart:
Set cht = ws.Shapes.AddChart( _
GetChartTypeConstant(stringType), _
50, 50, 300, 200).Chart
End Sub
そして、これが 内で呼び出される関数ですSet cObj...
。ケース選択で関数を使用すると、「xlBarClustered」の代わりに「Horizontal Bars」などと呼ぶことができる、よりユーザーフレンドリーな名前を使用できることに注意してください。
Private Function GetChartTypeConstant(myString As String) As XlChartType
'This function returns an xlCharType constant value from a descriptive string
' which you will need to define or modify for your needs;
' using select case also means you can use more descriptive/user-friendly
' names on your worksheet.
Select Case myString
Case "xlBarClustered", "Clustered Bars", "Horizontal Bars"
GetChartTypeConstant = xlBarClustered
Case "xlColumnClustered", "Clustered Columns", "Vertical Columns"
GetChartTypeConstant = xlColumnClustered
Case "xlLine", "Lines Only"
GetChartTypeConstant = xlLine
'Additional cases can be added for
' additional chart types.
End Select
End Function
サブルーチンまたは関数のいずれにもエラー処理がないことに注意してください。