从单元格中获取Objectmember

时间:2016-12-20 11:23:40

标签: vba excel-vba excel

我想动态创建图表并遇到问题。

通常(静态)我使用

  

Selection.Position = xlLabelPositionBelow

一切正常。现在我想从像

这样的单元格中获取成员
  

Selection.Position = Range(" H18")。Value2

我刚刚将xlLabelPostionBelow写入H18。

但现在我得到RuntimeError: 438: Object does not support the Method

我只是不知道该怎么做。我的猜测是我不能使用" xlLabelPostionBelow"作为一个字符串,但我不知道它是否是另一种类型。

1 个答案:

答案 0 :(得分:0)

我不认为我们在评论中回答了您的问题。您希望在工作表上放置一些值以影响您的代码。

好的,可以使用Names作为常量和Ranges,所以我们可以定义一个常量并稍后引用它

Option Explicit

Sub DefineMySymbol_RunOnce()
    Sheet1.Names.Add "xlLabelPositionBelow", 1
    Sheet1.Range("H18").Value2 = "xlLabelPositionBelow"  '* this is just a string


End Sub

Sub UsingValueFromSheet()

    '* here we will pull out the constant (1) and cast to a variable
    Dim eDataLabelPosition As Excel.XlDataLabelPosition
    eDataLabelPosition = CLng(Split(Sheet1.Names.Item(Sheet1.Range("H18").Value2).RefersTo, "=")(1))

    '* eDataLabelPosition should now appear in Locals Window as xlLabelPositionBelow
    Stop

    '* and now you can set you Position
    'Selection.Position = eDataLabelPosition

End Sub