2017-04-26 13 views
1

我目前正在爲公司中的其他人使用excel模板。 我需要用一個按鈕刪除行。 我相信我所做的一切都正確,但我一直在收到錯誤。刪除具有形狀的行.. TopLeftCell錯誤

正下方可以看到出錯的代碼;

Worksheets("Storyboard").Activate 
Worksheets("Storyboard").Unprotect Password:="**$#B'A1313XQ.;**" 

satirlar = Baslangic & ":" & Bitis 

For i = Baslangic To Bitis 
Dim s As Shape 
For Each s In Worksheets("Storyboard").Shapes 
    If Not Intersect(s.TopLeftCell, Range("L" & Baslangic & ":" & "L" & Bitis)) Is Nothing Then 
     s.Delete 
    End If 
Next s 

Next i 

Rows(satirlar).Delete Shift:=xlUp 

我一直在「s.ftopleftcell」部分發生錯誤。它說「應用程序定義或對象定義的錯誤」。

關於此代碼; 「Baslangic」和「Bitis」是用表單預定義的。

我可以在這裏使用任何可能的提醒..

+0

你有你的工作表中的任何數據驗證的下拉列表中? – Rory

+0

其實是的,我有另一個模塊自動插入驗證到同一張表的範圍。但是在這個單元格範圍內,沒有形狀。 – Bildircin13

+0

請參閱下面我的建議答案。 – Rory

回答

2

的數據驗證(DV)下拉列表是一個形狀,但DV的下拉列表中沒有TopLeftCell財產。你可以做的就是通過DrawingObjects代替循環:

For i = Baslangic To Bitis 
Dim s As Object 
For Each s In Worksheets("Storyboard").DrawingObjects 
    If Not Intersect(s.TopLeftCell, Range("L" & Baslangic & ":" & "L" & Bitis)) Is Nothing Then 
     s.Delete 
    End If 
Next s 

Next i 
+0

哇,我欠你一個大其中一個真的救了我,非常感謝。 – Bildircin13

0

其實,嘗試這樣的事情:

Sub TestMe() 

    Dim myShape As Shape 

    For Each myShape In ActiveSheet.Shapes 
     Debug.Print myShape.TopLeftCell.Address(0, 0) 
     Debug.Print myShape.TopLeftCell.row 
    Next myShape 

End Sub 

這是使用topLeftCell的好辦法。你甚至可以明確地比較這些行,並讓你的代碼沒有Intersect()

它會像if myShape.TopLeftCell.row >Baslangic或類似的東西。

您的代碼將可能工作,如果你把它改爲:

If Not Intersect(s.TopLeftCell, Worksheets("Storyboard").Range("L" & Baslangic & ":" & "L" & Bitis)) Is Nothing Then 
+0

非常感謝您的快速回復。不幸的是,這次我在「Debug.Pring myShape.TopLeftCell.Address(i,」L「)行中不斷收到錯誤信息:(它說」應用程序定義的錯誤或對象定義的錯誤「myShape.TopLeftCell.Row」 – Bildircin13

+0

在地址中留下2個零點 – Vityata

+0

對於2個零點,我仍然得到相同的錯誤,不幸的是,這給了這部分錯誤; {Debug.Print s.TopLeftCell.Address(0,0)} – Bildircin13