VBA代码在宏中工作,但不在按钮上工作

时间:2017-06-03 09:48:34

标签: excel vba excel-vba

当我在Macro中逐行(F8)时,以下代码运行良好。但是,当我将它复制到工作表" Cover"中的按钮时,进入"时会出错。工作表(" FES LIST")。范​​围(单元格(2,2),单元格(LastRow2,4))。选择"。然后我在工作表" FES LIST"中创建了另一个按钮,这是我执行的工作表'选择',代码重新开始工作。

有没有办法让我可以将按钮留在表格中?#34; cover"但仍然选择表格中的范围" FES LIST"?非常感谢。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            let alert = UIAlertController(title: "Delete Task", message: "Do you want to delete this task?", preferredStyle: .alert)
            let confirmAction = UIAlertAction(title:"OK", style: .default) { action in
                // Show activity indicator
                ViewControllerUtils().showActivityIndicator(uiView: self.view)

                // Retrieve Firebase reference to delete
                let repairItem = self.repairItems[indexPath.row]
                let storage = Storage.storage()
                let storageRef = storage.reference()
                let imageRef = storageRef.child(repairItem.imageRef)

                // Delete the item from Firebase
                repairItem.ref?.removeValue(completionBlock: {(error, ref) in
                    if let error = error {
                        print("Problem deleting item")
                    } else {
                        print("Item deleted successfully")
                    }
                })

                // Delete the photo
                imageRef.delete { error in
                    if let error = error {
                        print("Problem deleting photo")
                    } else {
                        print("Photo deleted successfully")
                    }
                }

                print("Stopping activity indicator")
                // Stop the activity indicator
                ViewControllerUtils().hideActivityIndicator(uiView: self.view)

                print("Reloading table view")
                // Reloading table view
                self.tableView.reloadData()
            }

            let cancelAction = UIAlertAction(title: "Cancel", style: .default)

            alert.addAction(confirmAction)
            alert.addAction(cancelAction)

            present(alert, animated: true, completion: nil)
        }
    }

1 个答案:

答案 0 :(得分:3)

更改

Sheets("FES LIST").Activate

Worksheets("FES LIST").Range(Cells(2, 2), Cells(LastRow2, 4)).Select
Worksheets("FES LIST").Range(Cells(2, 2), Cells(LastRow2, 4)).Name = "NameRange0"

With Worksheets("FES LIST")
    .Range(.Cells(2, 2), .Cells(LastRow2, 4)).Name = "NameRange0"
End With

哪个不使用.Activate/.Select并且还完全符合Cells对象的资格。

尝试和测试

Private Sub CommandButton1_Click()
    Dim nm As Name
    Dim LastRow2 As Long

    On Error Resume Next
    For Each nm In ActiveWorkbook.Names
        nm.Delete
    Next
    On Error GoTo 0

    With Worksheets("FES LIST")
        LastRow2 = .Cells(.Rows.Count, "B").End(xlUp).Row

        .Range(.Cells(2, 2), .Cells(LastRow2, 4)).Name = "NameRange0"
    End With
End Sub