Consulting

Results 1 to 7 of 7

Thread: Excel 2013>VBA>PageSetup>User Defined Formula>Interference

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location

    Excel 2013>VBA>PageSetup>User Defined Formula>Interference

    Hello,

    I have code that saves a PDF of my spreadsheet to my desktop with custom print settings. It works perfectly except when I enable the PageSetup settings. When these settings are enabled within the code, my user-defined formula generates error messages within the cell.

    What is it about the PageSetup settings that is interfering with my user-defined formula?

    Here's the PDF print code:

    Sub Button2()
    
    
    Application.ScreenUpdating = False
    
    
    Dim strFilename As String
    Dim rngRange As Range
    Dim konumber As String
    
    
    konumber = Environ$("UserName")
    Set rngRange = ThisWorkbook.Sheets("CostWorksheet").Range("D4")
    strFilename = rngRange.Value & Format(Now(), "mmddyyyy hhmm")
    
    
    ThisWorkbook.Sheets("CostWorksheet").Activate
    
    
    'With ActiveSheet.PageSetup
        '.Orientation = xlLandscape
        '.Zoom = False
        '.FitToPagesWide = 1
        '.FitToPagesTall = 1
    'End With
    
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\" & konumber & "\Desktop\" & strFilename & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    
    
    ActiveCell.Offset(1, 100).Activate
        
    Application.ScreenUpdating = True
    
    
    End Sub
    Thanks!

  2. #2
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location
    Here's the fix I found:

    Sub Button2()
    
    
    Application.ScreenUpdating = False
    
    
    Dim strFilename As String
    Dim rngRange As Range
    Dim konumber As String
    
    
    konumber = Environ$("UserName")
    Set rngRange = ThisWorkbook.Sheets("CostWorksheet").Range("D4")
    strFilename = rngRange.Value & Format(Now(), "mmddyyyy hhmm")
    
    
    ThisWorkbook.Sheets("CostWorksheet").Activate
    
    
    With ThisWorkbook.Sheets("CostWorksheet").PageSetup
        .Orientation = xlLandscape
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    
    
    ThisWorkbook.Sheets("CostWorksheet").EnableCalculation = False
    ThisWorkbook.Sheets("CostWorksheet").EnableCalculation = True
    
    
    ThisWorkbook.Sheets("CostWorksheet").Calculate
    
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\" & konumber & "\Desktop\" & strFilename & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
    
    
    ActiveCell.Offset(1000, 1000).Activate
        
    Application.ScreenUpdating = True
    
    
    End Sub

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I can't see what would cause an issue. Can you post your workbook?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location
    Here's 2 user-defined formulas that I have on 2 general Modules for summing and taking an average horizontally on visible cells:


    Function Sum_Visible_Cells(Cells_To_Sum As Object)
    Dim cell As Object
    Dim Total As Double
    
    
    Application.Volatile
    
    
    For Each cell In Cells_To_Sum
    If (cell.Rows.Hidden = False) And (cell.Columns.Hidden = False) Then
    Total = Total + cell.Value
    End If
    
    
    Next
    Sum_Visible_Cells = Total
    
    
    End Function
    Function AverageVisible(rng As Object)
        Dim rCell As Object
        Dim iCount As Integer
        Dim dTtl As Double
        
        Application.Volatile
        
        iCount = 0
        dTtl = 0
        
        For Each rCell In rng
        If (rCell.Rows.Hidden = False) And (rCell.Columns.Hidden = False) Then
        dTtl = dTtl + rCell
        iCount = iCount + 1
        End If
        
        Next
        AverageVisible = dTtl / iCount
        
    End Function
    Somehow the PageSetup settings were preventing the calculations in the sheet using those formulas from functioning.

  5. #5
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location
    But the disable and enable then calculate commands corrected the problem.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You could tidy up a bit for clarity, and I suspect omit the EnableCalculation lines.
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("CostWorksheet")
    
    
    With ws
        With .PageSetup
            .Orientation = xlLandscape
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = 1
        End With
        .EnableCalculation = False
        .EnableCalculation = True
        .Calculate
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\" & konumber & "\Desktop\" & strFilename & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
        Application.Goto .Cells(1000, 1000)
    End With
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    May 2016
    Posts
    73
    Location
    Thanks for those suggestions!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •