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

后端 未结 4 583
情歌与酒
情歌与酒 2020-12-19 03:36

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 f

相关标签:
4条回答
  • 2020-12-19 04:10

    The line Application.PrintCommunication = False (which is added by the macro recorder) before doing PageSetup screws up the formating via VBA.

    If your code has got this line in it, try removing it. That solved my problem with setting the header and footer via VBA.

    0 讨论(0)
  • 2020-12-19 04:17

    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!! Also, you need to remember, I am a CPA not a programmer ;-)

    I am reading some values from the ActiveSheet to populate the header. 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.

    0 讨论(0)
  • 2020-12-19 04:28

    I split the sheet print setup into 2 loops. First loop with Application.PrintCommunication = False I run the non-header/footer setup. I then set Application.PrintCommunication = True and run the header/footer setup in a second loop. Appears to run faster than in XL2003, and applies the header/footer correctly. Until MS fixes this bug, that works fine for me.

    0 讨论(0)
  • 2020-12-19 04:33

    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.

    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. This led me to believe that the formatting codes were very important.

    0 讨论(0)
提交回复
热议问题