Excel 2016中断了之前工作的VBAmacros

我已经在Excel中开发了一个小的VBAmacros,应该在工作簿更改期间(在我的情况下,在第15行中input一个数字并按Tab键)将行15中的单元格的值添加到行6中的单元格的值。

最初,我在Excel 2013中开发和使用它,然后我切换到Mac,并在Excel 2011 for Mac 2011中使用它。现在,我已经安装了Excel的Mac 2016,突然,macros不再工作了。

这是脚本:

Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("C15:H15")) > 0 Then Call copySub End If End Sub Sub copySub() Sheets("sheet1").Protect , UserInterFaceOnly:=True For i = 3 To 8 Cells(6, i).Value = Cells(6, i).Value + Cells(15, i).Value Cells(15, i).Value = 0 Next i End Sub 

当我在Excel 2016中input一个值并按Tab键时,出现运行时错误91“对象variables或块variables未设置”。 错误似乎发生在一行中:

 Cells(6, i).Value = Cells(6, i).Value + Cells(15, i).Value 

我也尝试将其存储在一个variables,然后将其分配给Cells(6, i).Value ,但这也没有帮助。

微软是否改变了表单保护的逻辑,特别是将参数UserInterFaceOnly设置为true ? 或者这是怎么回事?

我希望你能帮助我。

谢谢,chuky

你确定你已经正确地复制了这段代码吗? 没有办法在任何版本的Excel中工作。

你的问题是这些:

  1. Intersect返回一个Range对象,所以你的代码会抛出一个91错误。
  2. 在您的线条Sheets("sheet1").Protect ...很可能存在一个错误Sheets("sheet1").Protect ...因为它可能被称为“Sheet1”。 如果是这样,这会抛出一个91错误。
  3. 如果您将工作表名称从“sheet1”更改,则会引发91错误。
  4. 为什么只在Worksheet_Change保护Worksheet_Change ? 这应该在Workbook_Open完成吗? 如果你这样做,用户如何改变单元而不使特定的单元免于保护?

目前还不清楚你指的是哪个工作表,以及copySub例程的保存位置。 我已经更新了你的代码,因为它是删除主要的错误和写入提名你的工作表的能力 – 你必须调整,如你所愿。 祝你好运。

 Private Sub Worksheet_Change(ByVal Target As Range) Dim ws As Worksheet Set ws = Target.Worksheet If Not Intersect(Target, ws.Range("C15:H15")) Is Nothing Then Call copySub(ws) End If End Sub Sub copySub(ws As Worksheet) ws.Protect , UserInterFaceOnly:=True Application.EnableEvents = False For i = 3 To 8 ws.Cells(6, i).Value = ws.Cells(6, i).Value + ws.Cells(15, i).Value ws.Cells(15, i).Value = 0 Next i Application.EnableEvents = True End Sub