简体   繁体   English

除非空白,否则Excel页眉/页脚不会通过VBA更改

[英]Excel headers/footers won't change via VBA unless blank

Disclaimer: It's been a few years since I worked (a lot) with VBA, so this might be an issue caused by confusing myself with what is essentially a very different language from what I usually deal with. 免责声明:自从我(很多)使用VBA工作已经有几年了,所以这可能是一个问题,因为我将自己与我通常处理的语言完全不同。

So; 所以; I've got a workbook (Excel 2010) with multiple sheets (20+), most of whom are multi-page. 我有一张多张(20+)的工作簿(Excel 2010),其中大多数是多页的。 To make things easier when printing everything, I want to add some sheet-specific headers with amongst others the name of the sheet, number of pages and so on. 为了在打印所有内容时更容易,我想添加一些特定于工作表的标题,其中包括工作表的名称,页数等。

I've written a tiny function that should (in theory) do this for me by iterating over all the sheets setting the header. 我写了一个小函数,理论上应该通过遍历所有设置标题的工作表来为我做这个。 However, for some reason it only works if the header is empty; 但是,出于某种原因,它仅在标头为空时才有效; if it already has a value it refuses to overwrite for some unknown reason. 如果它已经有一个值,它会因某些未知原因而拒绝覆盖。

Dim sheetIndex, numsheets As Integer
sheetIndex = 1
numsheets = Sheets.Count

' Loop through each sheet, but don't set any of them to active
While sheetIndex <= numsheets
    Dim sheetname, role, labeltext As String
    sheetname = Sheets(sheetIndex).name
    role = GetRole(mode) 
    labeltext = "Some text - " & sheetname & " - " & role

    With Sheets(sheetIndex).PageSetup
        .LeftHeader = labeltext
        .CenterHeader = ""
        .RightHeader = "Page &[Page] / &[Pages]"
        .LeftFooter = "&[Date] - &[Time]"
        .CenterFooter = ""
        .RightFooter = "Page &P / &N"
    End With

    sheetIndex = sheetIndex + 1
Wend

I found a solution that seems to work for replacing text. 我找到了一个似乎可以替换文本的解决方案。 For whatever reason, in the macro, you need to include the header/footer format character codes in order for it to work properly. 无论出于何种原因,在宏中,您需要包含页眉/页脚格式字符代码才能使其正常工作。

This code worked to replace existing header text with new information: 此代码用于使用新信息替换现有标题文本:

Sub test()
    Dim sht As Worksheet
    Set sht = Worksheets(1)
    sht.PageSetup.LeftHeader = "&L left text"
    sht.PageSetup.CenterHeader = "&C center Text"
    sht.PageSetup.RightHeader = "&R right text"
End Sub

Without the &L , &C , and &R codes before the text, I could not get it to work. 在文本之前没有&L&C&R代码,我无法使其工作。

Some interesting behavior I found is that if you use the following code: 我发现一些有趣的行为是,如果您使用以下代码:

.CenterHeader = "&L some text"

it will actually put the some text in the LeftHeader position. 它实际上会将some text放在LeftHeader位置。 This led me to believe that the formatting codes were very important. 这让我相信格式化代码非常重要。

The line Application.PrintCommunication = False (which is added by the macro recorder) before doing PageSetup screws up the formating via VBA. 在执行PageSetup通过VBA搞砸了格式之前, Application.PrintCommunication = False (由宏录制器添加)。

If your code has got this line in it, try removing it. 如果您的代码中包含此行,请尝试将其删除。 That solved my problem with setting the header and footer via VBA. 这解决了我通过VBA设置页眉和页脚的问题。

I've read StackOverflow for years and this is the first time I've actually been able to post a solution ... hope it helps someone!! 我已经阅读了StackOverflow多年,这是我第一次真正能够发布解决方案...希望它可以帮助别人! Also, you need to remember, I am a CPA not a programmer ;-) 另外,你需要记住,我是注册会计师而不是程序员;-)

I am reading some values from the ActiveSheet to populate the header. 我正在从ActiveSheet中读取一些值来填充标题。 The application is a tax election that will be sent with a tax return so it must have the taxpayer's name and social security number at the top. 该申请是一个税收选举,将与纳税申报表一起发送,因此必须在纳税人的名字和社会安全号码的顶部。

Sub PrintElection()
' Print preview the MTM Election
    If Range("Tax_Year").Value = Range("First_MTM_year").Value Then
        ActiveSheet.PageSetup.LeftHeader = Format(Worksheets("Election").Range("Taxpayer_Name").Value)
        ActiveSheet.PageSetup.RightHeader = Format(Worksheets("Election").Range("Taxpayer_SSN").Value)

        ActiveWindow.SelectedSheets.PrintPreview

    Else

        MsgBox "The 'Effective For Tax Year' date must EQUAL the 'First MTM year' date", vbOKOnly, "Check Years"
        Sheets("Roadmap").Select
        Range("First_MTM_year").Select
   End If

End Sub

It checks to see if the Mark-to-Market election year is the same as the election form then formats the election page. 它检查Mark-to-Market选举年份是否与选举表格相同,然后格式化选举页面。

I split the sheet print setup into 2 loops. 我将纸张打印设置分成2个循环。 First loop with Application.PrintCommunication = False I run the non-header/footer setup. Application.PrintCommunication = False的第一个循环我运行非页眉/页脚设置。 I then set Application.PrintCommunication = True and run the header/footer setup in a second loop. 然后我设置Application.PrintCommunication = True并在第二个循环中运行页眉/页脚设置。 Appears to run faster than in XL2003, and applies the header/footer correctly. 似乎比XL2003运行得更快,并正确应用页眉/页脚。 Until MS fixes this bug, that works fine for me. 在MS修复此错误之前,这对我来说很好。

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

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