代码之家  ›  专栏  ›  技术社区  ›  xiaodai

在VBA中,如何检查OleObject是否具有LinkedCell属性?

vba
  •  1
  • xiaodai  · 技术社区  · 15 年前

    是否有方法检查OleObject是否具有LinkedCell属性?例如,标签和按钮没有LinkedCell,而其他的则有。我正试图编写一段代码,通过循环遍历工作表中的所有oleobjects来替换LinkedCell。事先谢谢。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Lance Roberts    15 年前

    您必须执行捕获错误的标准VBA技术,以测试LinkedCell属性。

    公共子测试()
    
    将cntl设置为对象
    出错时继续下一步
    
    对于activesheet.oleobjects中的每个cntl
    调试.打印控制名称
    
    如果ISerror(cntl.linkedcell),则
    调试打印“无链接单元格”
    否则
    调试打印“链接单元”
    结束如果
    
    下一个CNTL
    
    结束子
    < /代码> 
    
    

    以下是它在空白Excel工作表上工作的证明图片,有四个不同的控件。

    测试LinkedCell属性时出错。

    Public Sub test()
    
    Dim cntl As Object
    On Error Resume Next
    
    For Each cntl In ActiveSheet.OLEObjects
    Debug.Print cntl.Name
    
    If IsError(cntl.LinkedCell) Then
        Debug.Print "No Linked Cell"
    Else
        Debug.Print "Linked Cell"
    End If
    
    Next cntl
    
    End Sub
    

    以下是它在空白Excel工作表上工作的证明图片,有四个不同的控件。

    alt text

        2
  •  0
  •   xiaodai    15 年前
        For Each tempWk In trunkWb.Sheets
                For Each tempShape In tempWk.Shapes
    
                Set obj = tempShape.OLEFormat.Object
    
                'this bit of code can be confusing but it's necessary
                On Error GoTo LinkedCellNotValid
                With Application.WorksheetFunction
                    obj.LinkedCell = .Substitute(obj.LinkedCell, "[" & branchWb.Name & "]", "")
    
                    For j = 1 To i
                        Set oldwk = trunkWb.Worksheets(sheetNames(j - 1))
                        obj.LinkedCell = .Substitute(obj.LinkedCell, "_review_" & j, oldwk.Name)
                    Next j
                End With
                GoTo LinkedCellDone
    LinkedCellNotValid:
                'clear the error
                Err.Clear
                Resume LinkedCellDone
    
    LinkedCellDone: