如何使用变量在VBA(Excel)中设置属性

时间:2011-04-18 17:49:02

标签: excel vba excel-vba eval

拿这段代码:

With ActiveSheet.Shapes.AddShape(msoShapeRectangle, x, y, w, h).TextFrame
  .Parent.Line.Visible = False
  .Parent.Fill.ForeColor.RGB = RGB(r, g, b)
End With

是否有任何VBA方式可以“执行”或“评估”,就像可以在perl / python / ...中完成一样,这样文本.Parent.Line.Visible可以从变量(或单元格值)中提取,而不是比硬编码?

ParentLine = ".Parent.Line.Visible"
ParentLineValue = "False"

With ActiveSheet.Shapes.AddShape(msoShapeRectangle, x, y, w, h).TextFrame
  **eval**(ParentLine & "=" & ParentLineValue)
  .Parent.Fill.ForeColor.RGB = RGB(r, g, b)
End With

编辑:我发现MSDN information for Access提到了Eval,但是当我执行我的代码时,它会显示“Undefined Sub or Function”,指向Eval(Excel似乎不知道这个函数)。

编辑2:找到definitive (negative) answer on SO

编辑3:似乎总有一个答案,因为我不是在任意代码执行的一般解决方案之后。感谢GSerg帮助使用CallByName。

3 个答案:

答案 0 :(得分:10)

解决方案1。

使用CallByName

Option Explicit

Private Type Callable
  o As Object
  p As String
End Type

Public Sub SetProperty(ByVal path As String, ByVal Value As Variant, Optional ByVal RootObject As Object = Nothing)
  With GetObjectFromPath(RootObject, path)
    If IsObject(Value) Then
      CallByName .o, .p, VbSet, Value
    Else
      CallByName .o, .p, VbLet, Value
    End If
  End With
End Sub

Public Function GetProperty(ByVal path As String, Optional ByVal RootObject As Object = Nothing) As Variant
  With GetObjectFromPath(RootObject, path)
    GetProperty = CallByName(.o, .p, VbGet)
  End With
End Function

Public Function GetPropertyAsObject(ByVal path As String, Optional ByVal RootObject As Object = Nothing) As Object
  With GetObjectFromPath(RootObject, path)
    Set GetPropertyAsObject = CallByName(.o, .p, VbGet)
  End With
End Function


Private Function GetObjectFromPath(ByVal RootObject As Object, ByVal path As String) As Callable
  'Returns the object that the last .property belongs to
  Dim s() As String
  Dim i As Long

  If RootObject Is Nothing Then Set RootObject = Application

  Set GetObjectFromPath.o = RootObject

  s = Split(path, ".")

  For i = LBound(s) To UBound(s) - 1
    If Len(s(i)) > 0 Then
      Set GetObjectFromPath.o = CallByName(GetObjectFromPath.o, s(i), VbGet)
    End If
  Next

  GetObjectFromPath.p = s(UBound(s))
End Function

用法:

? getproperty("activecell.interior.color")
16777215 

SetProperty "activecell.interior.color", vbYellow
'Sets yellow background

? getproperty("names.count", application.ActiveWorkbook)
0 

? getproperty("names.count", GetPropertyAsObject("application.activeworkbook"))
0

解决方案2.

动态添加代码 不要这样做。这是错误的,需要设置"Allow access to VB project"刻度。

添加对Microsoft Visual Basic for Applications Extensibility X.X的引用。

创建一个名为ModuleForCrap的模块。

添加动态构建的子/函数:

ThisWorkbook.VBProject.VBComponents("ModuleForCrap").CodeModule.AddFromString _
"function foobar() as long" & vbNewLine & _
"foobar = 42" & vbNewLine & _
"end function"`

称之为:

msgbox application.run("ModuleForCrap.foobar")

删除它:

With ThisWorkbook.VBProject.VBComponents("ModuleForCrap").CodeModule
  .DeleteLines .ProcStartLine("foobar", vbext_pk_Proc), .ProcCountLines("foobar", vbext_pk_Proc)
End With

答案 1 :(得分:1)

您可以尝试查看CallByName,但我认为它不会做您想要的(至少,如果您想要评估多点对象/属性引用,则不容易) 。

答案 2 :(得分:0)

False评估为零。你可以构造一个整数变量来等于零,并使其变为与False相同的方式。