问题
I am currently making an excel template for other people in company to use. I need to delete rows with a single button. I believe I've done everything correctly, but I am keep getting an error.
Right below you can see the codes which gives error;
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
I am keep getting an error on the "s.topleftcell" part. It says that "application-defined or object defined error".
On this code; "Baslangic" and "Bitis" are predefined with a form.
I can use any possible advise here..
回答1:
A data validation (DV) dropdown is a shape, but DV dropdowns don't have a TopLeftCell
property. What you can do is loop through the DrawingObjects
instead:
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
回答2:
Actually, try something like this:
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
This is a good way to use the topLeftCell. You can even compare the rows explicitly and make your code without the Intersect()
.
It would be something like if myShape.TopLeftCell.row >Baslangic
or similar.
Your code would probably work, if you change it to:
If Not Intersect(s.TopLeftCell, Worksheets("Storyboard").Range("L" & Baslangic & ":" & "L" & Bitis)) Is Nothing Then
来源:https://stackoverflow.com/questions/43631452/delete-row-with-shapes-topleftcell-error