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

时间:2021-08-01 22:16:28

标签: excel vba footer

注意:我的 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 方法可能会导致不同的结果,并且在页脚不再为空后,想要的更改会被忽略。

>

1 个答案:

答案 0 :(得分:0)

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

如果禁用 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