2
以下代码在宏中逐行执行(F8)时效果很好。但是,当我将它复制到工作表“封面”中的按钮上时,它出现在“工作表(”FES LIST“)中时出错。范围(单元格(2,2),单元格(LastRow2,4))选择”。然后我在工作表“FES LIST”中创建了另一个按钮,这是我执行'选择'的工作表,代码再次开始工作。VBA代码在宏中工作,但不在按钮上
有没有办法让我可以离开工作表“封面”中的按钮,但仍然在工作表“FES LIST”中选择范围?非常感谢。
Private Sub CommandButton1_Click()
'This Code will remove any names defined in name manager
Dim nm As Name
On Error Resume Next
For Each nm In ActiveWorkbook.Names
nm.Delete
Next
On Error GoTo 0
'Find the last used row in a Column B and select range from B2 to D:LastRow
Dim LastRow2 As Long
With Worksheets("FES LIST")
LastRow2 = .Cells(.Rows.Count, "B").End(xlUp).Row
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"
End With
End Sub
你好谢谢你的快速回复。我试过你的代码,但错误仍然存在。然后,我累了添加“表格(”FES LIST“)。选择”强制它定位到这张表格,它终于起作用了。不过,我认为这可能不是最佳解决方案:( – AmadeusNing
在上面的文章中查看我的编辑,它对我来说很好,你可能需要刷新页面才能看到编辑 –
难道是@AmadeusNing正在重新使用已经存在的名称? –