有没有办法检查OLEObject是否具有LinkedCell属性?例如,标签和按钮没有链接单元,而其他单元则没有。我试图编写一段代码,通过循环遍历工作表中的所有OLEObjects来替换linkedCells。提前谢谢。
答案 0 :(得分:1)
您必须使用标准VBA技术来捕获错误以测试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工作表上使用四种不同控件进行工作的证明图片。
答案 1 :(得分:0)
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: