页脚命令第二次无法正常工作

Footer commands don't work properly the second time

注意:我的 Excel 版本是德语,以防发生任何变化。一个问题可能是 the VBA codes for headers and footers 工作方式不同。

我的代码(第一次使用空页脚):

Sub updateFootnote_Click()
    Application.PrintCommunication = False
    Application.ScreenUpdating = False
    Table2.Activate
    With ActiveSheet.PageSetup
        .LeftFooter = "Cell Value: " & Table1.Cells(15, 4).Value
        .CenterFooter = ""
        .RightFooter = "Test" & Chr(10) & "Seite &S von &A"
    End With
    Table1.Activate
    Application.ScreenUpdating = True
    Application.PrintCommunication = True
End Sub

页脚结果 1(初始执行时页脚为空):

Cell Value: 1  |                   |  Test<\n>Seite 1 von 46
' <\n> = linebreak

页脚结果2(第二次执行的方法):

Cell Value: 1  |  von &[Register]  |  Test<\n>Seite 1 von 46
' <\n> = linebreak

页脚结果3(第三次执行的方法):

Cell Value: 1  |  ~von &[Seite]&[Pfad] von~  |  Test<\n>Seite 1 von 46
' <\n> = linebreak
' ~Test~ = *strikethrough formated text*

等等,等等。每次我再次执行该功能时,情况都会变得更糟,直到您手动删除页脚。 (使用代码将所有三个页脚部分设置为空字符串不起作用)

除此之外,代码中的想要的更改或引用的单元格值在第一次成功执行代码后不会生效。

如果有人能告诉我这里出了什么严重的错误,我将不胜感激,重复执行一些 setter 方法会导致不同的结果,并且在页脚被忽略后需要的更改会被忽略不再是空的了。

终于知道是什么原因造成的

如果 PrintCommunication 被禁用,VBA(德语 Excel 版本)中的页眉/页脚代码似乎有问题。如果是这种情况,&P 变为 &S,&N -> &A,&F -> &N 等等。此外,代码在多次执行时可能会来回转换,导致更加混乱。

简而言之 - 以下行可能会引发大量问题:

Application.PrintCommunication = False

将其排除在外即可解决问题。 然后工作代码出来:

Sub updateFootnote_Click()
    Application.ScreenUpdating = False
    Table2.Activate
    With ActiveSheet.PageSetup
        .LeftFooter = "Cell Value: " & Table1.Cells(15, 4).Value
        .CenterFooter = ""
        .RightFooter = "Test" & Chr(10) & "Seite &P von &N"
    End With
    Table1.Activate
    Application.ScreenUpdating = True
End Sub