【问题标题】:Code works in VBA for Excel 2013, but it errors in Excel 2016代码在 VBA for Excel 2013 中有效,但在 Excel 2016 中出错
【发布时间】:2018-05-24 20:45:20
【问题描述】:

以下代码在 Excel 2013 中运行良好。我们的 IT 人员为我们安装了一台带有 Excel 2016 的新计算机。现在打开文件时出现错误“运行时错误 '1004': Unable to set the CenterHeaderproperty of the PageSetup 类"

当我尝试录制宏以查看 Excel 2016 是否执行不同操作时,代码看起来与此处相同,并且此代码在 2013 年仍然运行良好。

Private Sub Workbook_Open()
Dim Obj As Object

AWb = Application.ActiveWorkbook.Name
If AWb = "UCSB Service Form.xlsm" Then
 Application.ScreenUpdating = False
 Sheets("Form").Activate
 DateFormat = Format(Date, "mm/dd/yy;@")
 Range("F2") = DateFormat
 Range("F36") = DateFormat

 Cells.Validation.Delete

'Sets record number
 Record = "Pending"
 Range("H6") = "'" & Record

 ActiveSheet.PageSetup.CenterHeader = "Record: " & Record
         '<<< Debug indicates this line
         'Run-time error '1004': Unable to set the CenterHeaderproperty of the PageSetup class

'Open blank form

 Range("F3:F7").ClearContents
 Range("E9:F10").ClearContents
 Range("E12:F18").ClearContents
 Range("E20:F23").ClearContents
 Range("E26:F29").ClearContents
 Range("K2:K5").ClearContents
 Range("H4:I6").ClearContents
 Range("G9:J25").ClearContents
 Range("I27:I31").ClearContents
 Range("K27:K31").ClearContents
 Range("K27:K31").ClearContents
 Range("G34:I39").ClearContents
 Range("I40").ClearContents
 Range("K40").ClearContents
 Range("K33:K36").ClearContents

'Clear Highlighting

 Range("F3:F7").Interior.Color = xlNone
 Range("E9:F9").Interior.Color = xlNone
 Range("E12:F13").Interior.Color = xlNone
 Range("E14:F17").Interior.Color = xlNone
 Range("E18:F18").Interior.Color = xlNone
 Range("E20:F23").Interior.Color = xlNone
 Range("H4:I4").Interior.Color = xlNone

Application.ScreenUpdating = True
 Range("A1").Select
 Range("F3").Select
Application.ScreenUpdating = False

Range("H6") = "'" & Record

Set Obj = ActiveSheet.CommandButton1
Obj.Caption = "Save"

Call M_Lists.S_Lists

End If
'Updated 4/25/18
End Sub

【问题讨论】:

  • 您的新电脑上是否设置了打印机?
  • 哦哈哈,谢谢。是的,上面设置了打印机,但我们一直遇到打印问题。
  • 这太奇怪了!我将主打印机设置为默认值,现在一切正常!谢谢!
  • 我将评论转换为答案。考虑单击它旁边的复选标记,让其他人知道它可以解决您的问题,以防他们遇到同样的问题!

标签: excel excel-2013 excel-2016 vba


【解决方案1】:

在 OP 确认它有效后从我的评论中迁移。

我怀疑这个问题与您的打印机问题有关。通常,您不能在 VBA 中执行任何类型的页面设置操作,除非打印机在安装驱动程序的情况下正常工作。看看 IT 是否可以在具有工作打印机和 Office 2013 的计算机上安装 Office 2016,并且此代码已经可以工作。或者,如果已经安装了驱动程序,请尝试将主打印机设置为默认值。

【讨论】:

    猜你喜欢
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多