按钮循环重复相同的按钮很多次,.delete失败

时间:2018-08-20 02:01:48

标签: excel vba excel-vba for-loop button

每次添加新项时,我在工作表上都有一些使用宏创建的按钮(实际上,它们是“删除此行”按钮和“创建列表”按钮)。

我使用For Each / Next循环编写了一个宏,以删除与我单击的形状范围相交的所有按钮,但此按钮不起作用。

我添加了一堆Debug.Print命令来查看正在发生的事情,我对结果感到非常困惑。

For Each循环一次又一次地敲击相同的按钮,即使该按钮被识别为与范围相交,删除功能也会失败,并且循环会再次重复该按钮。

我有2个问题:

  1. 有什么方法可以防止我的For Each循环反复重复相同的按钮吗?和
  2. 按钮未删除,我在做什么错了?

这是我的代码:

Sub DeleteBtn()
    Dim i As Integer   'variable to keep track of how many loops we've done

    'returns the number of entries on my sheet
    NumTasks = Application.WorksheetFunction.CountA(Sheets("Tasks").Range("B7:B10000"))
    'uses the clicked shape to position the macro
    Set rctngl = ActiveSheet.Shapes(Application.Caller)
    'using topleftcell, this creates an address reference for the shape that called the macro
    With rctngl.TopLeftCell
        'sets this variable to hold the characters in the address for the clicked shape
        rctnglAdd = .Address
        ''''show shape address in immediate window
        Debug.Print "Rectangle location is " & rctnglAdd
        ''''sets this variable to hold the integer value of the clicked shape row
        rctnglRow = .Row
        ''''show shape row value in immediate window
        Debug.Print "Rectangle row is " & rctnglRow
        ''''sets a range to check against for button deletion
        Set btnRng = Sheets("Tasks").Range("$M$" & rctnglRow & ":$N$" & rctnglRow)
        ''''show range to check for intersect
        Debug.Print "Intersect Range is " & btnRng.Address
    End With

    i = 1     ''''sets i to 1 for the first time through the loop
    For Each btn In Sheets("Tasks").Buttons  ''''begin loop for every button in the sheet
        On Error Resume Next   'ignore errors
        newName = btn.Name    ''''This instance name is
        newAdd = btn.TopLeftCell.Address  ''''this instance address is
        Debug.Print "Round " & i & "!"   ''''Show loop number in immediate window
        ''''Show this instance address in immediate window
        Debug.Print "This Button's address = " & newAdd
        ''''Show this instance name in immediate window,
        Debug.Print "This Button's Name = " & newName
        ''''if this instance intersects with intersect range
        If Not Intersect(Range(newAdd), btnRng) Is Nothing Then
            ''''message to say button intersects
            Debug.Print "Button address " & newAdd & " intersects with delete range " & btnRng.Address
            ''''message to say button will be deleted
            Debug.Print "Deleting Button: " & """" & newName & """"
            ActiveSheet.Buttons("""" & newName & """").Delete ''''Original code was btn.delete
        End If
        i = i + 1     ''''increase i for next loop
    Next btn          ''''begin loop for next

End Sub

这是当我使用4个按钮在工作表上运行即时窗口时显示的内容:

Rectangle location is $N$8
Rectangle row is 8
Intersect Range is $M$8:$N$8
Round 1!
This Button's address = $M$5
This Button's Name = Button 1139
Round 2!
This Button's address = $N$5
This Button's Name = Button 1144
Round 3!
This Button's address = $N$5
This Button's Name = Button 1144
Round 4!
This Button's address = $N$5
This Button's Name = Button 1144
Round 5!
This Button's address = $N$5
This Button's Name = Button 1144
Round 6!
This Button's address = $N$5
This Button's Name = Button 1144
Round 7!
This Button's address = $N$5
This Button's Name = Button 1144
Round 8!
This Button's address = $N$5
This Button's Name = Button 1144
Round 9!
This Button's address = $N$5
This Button's Name = Button 1144
Round 10!
This Button's address = $N$5
This Button's Name = Button 1144
Round 11!
This Button's address = $N$5
This Button's Name = Button 1144
Round 12!
This Button's address = $N$5
This Button's Name = Button 1144
Round 13!
This Button's address = $M$7
This Button's Name = Button 1175
Round 14!
This Button's address = $M$7
This Button's Name = Button 1175
Round 15!
This Button's address = $M$7
This Button's Name = Button 1175
Round 16!
This Button's address = $M$7
This Button's Name = Button 1175
Round 17!
This Button's address = $M$7
This Button's Name = Button 1175
Round 18!
This Button's address = $M$7
This Button's Name = Button 1175
Round 19!
This Button's address = $M$7
This Button's Name = Button 1175
Round 20!
This Button's address = $M$7
This Button's Name = Button 1175
Round 21!
This Button's address = $M$7
This Button's Name = Button 1175
Round 22!
This Button's address = $M$7
This Button's Name = Button 1175
Round 23!
This Button's address = $M$7
This Button's Name = Button 1175
Round 24!
This Button's address = $M$7
This Button's Name = Button 1175
Round 25!
This Button's address = $M$7
This Button's Name = Button 1175 
Round 26!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 27!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 28!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 29!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 30!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 31!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 32!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 33!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 34!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 35!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 36!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"
Round 37!
This Button's address = $M$5
This Button's Name = Button 1139
Round 38!
This Button's address = $N$5
This Button's Name = Button 1144
Round 39!
This Button's address = $M$7
This Button's Name = Button 1175
Round 40!
This Button's address = $M$8
This Button's Name = Button 1215
Button address $M$8 intersects with delete range $M$8:$N$8
Deleting Button: "Button 1215"

1 个答案:

答案 0 :(得分:1)

此代码对我进行了使用MS Forms Buttons 的多次测试。以下代码无法识别 ActiveX按钮

注意,尤其是我删除了System.Web。您永远无法调试有效的语句,因为它将忽略所有错误。

On Error Resume Next