1
有沒有辦法來檢查一個OLEObject是否有一個LinkedCell屬性?例如,標籤和按鈕沒有鏈接單元,而其他標籤和按鈕則是。我試圖編寫一段代碼,它可以通過循環遍歷表中的所有OLEObjects來替換linkedCells。提前致謝。在VBA中,如何檢查OLEObject是否具有LinkedCell屬性?
有沒有辦法來檢查一個OLEObject是否有一個LinkedCell屬性?例如,標籤和按鈕沒有鏈接單元,而其他標籤和按鈕則是。我試圖編寫一段代碼,它可以通過循環遍歷表中的所有OLEObjects來替換linkedCells。提前致謝。在VBA中,如何檢查OLEObject是否具有LinkedCell屬性?
您必須執行捕獲錯誤以測試LinkedCell屬性的標準VBA技術。
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工作表上使用四種不同控件的工作證明圖片。
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:
這是行不通的。 – xiaodai 2010-10-21 05:11:23
@xiaodai,實際上我測試了它,以確保它能工作,當我今天晚些時候開始工作時,我會展示證明。但是,如果你需要幫助,你真的需要付出一點,就像它不適合你一樣。 – 2010-10-21 14:46:11
@xiaodai,好的,我發佈了證明。 – 2010-10-21 18:58:54