在Windows 10 Excel 2013上删除命名范围时出错 - 旧版本工作正常

时间:2017-09-20 07:50:02

标签: excel-vba windows-10 named-ranges vba excel

我遇到了一个我真不知道如何修复的问题。我有一个代码可以在Windows 7 Excel 2010中完美运行并始终具有。在过去的几周里,它只是在Windows 10 Excel 2013中运行时出现了错误。

基本上,下面的代码会将一些数据表复制到新工作簿中,以便剥离VBA(因为这被公司过滤器阻止)。然后循环遍历所有对象和名称,并在通过Outlook

发送之前删除它们
Private Sub btnSend_Click()
'---------------------Create the email spreadsheet---------------------------------
Dim FName           As String
Dim FPath           As String
Dim NewBook         As Workbook
Dim EEName As String
Dim nm As Name

EEName = Range("ForeNameCell").Value & " " & Range("SurnameCell").Value
FPath = VBA.Environ("temp") & "\" 'file path to store the copy of the sheet
FName = "NS Form - " & EEName & " " & VBA.Format(VBA.Now(), "mm_dd_yyyy hh mm AMPM")

Set NewBook = Workbooks.Add 'create a new workbook
ThisWorkbook.Sheets("contract Print Out").Copy Before:=NewBook.Sheets(1) 'copy over the printoutallfields sheet
ThisWorkbook.Sheets("Oracle Data").Copy Before:=NewBook.Sheets(1) 'copy over the printoutallfields sheet
'copy and paste special data in new workbook to remove formula
NewBook.Sheets("contract Print Out").Cells.Copy
NewBook.Sheets("contract Print Out").Cells.PasteSpecial xlPasteValues
NewBook.Sheets("Oracle Data").Cells.Copy
NewBook.Sheets("Oracle Data").Cells.PasteSpecial xlPasteValues
'remove back buttons
Dim myshape As Shape
For Each myshape In NewBook.Sheets("contract Print Out").Shapes
    myshape.Delete
Next myshape
For Each myshape In NewBook.Sheets("Oracle Data").Shapes
    myshape.Delete
Next myshape
'remove links and named ranges
Application.Calculation = xlManual
On Error Resume Next
For Each nm In NewBook.Names
    nm.Delete ' This line now errors in W10 E2013
Next
On Error GoTo 0
Application.Calculation = xlCalculationAutomatic

'NewBook.Sheets("PrintOutallFields").Name = "User Data" 'rename it in the destination workbook
Application.DisplayAlerts = False 'turn off overwrite prompt so that it will just overwrite
NewBook.SaveAs Filename:=FPath & FName 'save the file
DoEvents
Application.DisplayAlerts = True 'turn overwrite prompt back on
NewBook.Close

出现的错误在下方,当我查询名称时,它试图删除它会出现#NAME?。每个名称似乎都以这种方式出现在代码中,因为当我向前跳过并尝试转到下一个nm时,同样的事情发生了。

enter image description here

enter image description here

enter image description here

我忘了提到我们从工作簿中删除名称的原因是因为他们总是引用原始文件。当我们收到问题时,这会导致问题,原因当然是在客户PC上。

我的猜测是他们永恒智慧的MS,推动了更新,打破版本之间的VBA ......再次!我已经不得不解决其中的一些问题,但我对此感到难过。

1 个答案:

答案 0 :(得分:0)

似乎我的Windows个人资料在某个地方出现了腐败现象。该文件还适用于办公室周围的其他Windows 10安装。重新安装Excel没有修复它,但重建我的Windows配置文件。这对我来说是第一次,我以为我已经看过一切了。

我要感谢所有关注此事的人。我确定这是否是Excel的正常问题,枚举建议可能会被发现。