繁体   English   中英

代码在VBA for Excel 2013中有效,但在Excel 2016中出错

[英]Code works in VBA for Excel 2013, but it errors in Excel 2016

以下代码在Excel 2013中工作正常。我们的IT人员使用Excel 2016为我们安装了新计算机。现在打开文件时,出现错误“运行时错误'1004':无法设置PageSetup类的CenterHeaderproperty”

当我尝试记录宏以查看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

在OP确认它可行后,将其从我的评论中迁移出来。

我怀疑此问题与您的打印机问题有关。 通常,除非打印机与安装的驱动程序一起正常工作,否则您无法在VBA中执行任何类型的页面设置操作。 查看IT人员是否可以将Office 2016安装在具有可正常使用此代码的打印机和Office 2013的计算机上。 或者,如果已经安装了驱动程序,请尝试将主打印机设置为默认打印机。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM