Excel 如果为零则不打印

Excel do not print if zero

我的实际代码是:

Option Explicit

Sub SaveMailActiveSheetAsPDFIn2016()
    'Ron de Bruin : 1-May-2016
    'Test macro to save/mail the Activesheet as pdf with ExportAsFixedFormat with Mail
    Dim FileName As String
    Dim FolderName As String
    Dim Folderstring As String
    Dim FilePathName As String
    Dim strbody As String

    'Check for AppleScriptTask script file that we must use to create the mail
    If CheckAppleScriptTaskExcelScriptFile(ScriptFileName:="RDBMacMail.scpt") = False Then
        MsgBox "Sorry the RDBMacMail.scpt is not in the correct location"
        Exit Sub
    End If

    'My example sheet is landscape, I must attach this line
    'for making the PDF also landscape, seems to default to
    'xlPortait the first time you run the code
    ActiveSheet.PageSetup.Orientation = xlLandscape

    'Name of the folder in the Office folder
    FolderName = "TempPDFFolder"
    'Name of the pdf file
    FileName = "Order " & [C1] & " " & Format(Date, "dd-mm-yyyy") & ".pdf"

    Folderstring = CreateFolderinMacOffice2016(NameFolder:=FolderName)
    FilePathName = Folderstring & Application.PathSeparator & FileName

    'Create the body text in the strbody string
    strbody = "Hi " & [C2] & "," & vbNewLine & vbNewLine & _
        "Please find attached our new order" & vbNewLine & _
vbNewLine & _
        "Thanks"

    'expression A variable that represents a Workbook, Sheet, Chart, or Range object.
    'Not working if you change activeworkbook, always save the activesheet
    'Also the parameters are not working like in Windows
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    FilePathName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False

    'Call the MacExcel2016WithMacMailPDF function to save the new pdf and create the mail
    'When you use more mail addresses separate them with a ,
    'Look in Mail>Preferences for the name of the mail account or signature
    'Account name looks like this : "Your Name <your@mailaddress.com>"
    MacExcel2016WithMacMailPDF subject:=[C6] & Format(Date, "dd/mm/yy"), _
    mailbody:=strbody, _
    toaddress:=[C3], _
    ccaddress:=[C4], _
    bccaddress:=[C5], _
    attachment:=FilePathName, _
    displaymail:=True, _
    thesignature:="", _
    thesender:=""
End Sub 

我希望不显示打印区域 =0 中 E 列的所有单元格,并且 sheet 会自行收缩(就像删除 =0 行一样),这是在创建 .pdf 之前文档和打开邮箱。

我不知道我是否足够清楚抱歉

谢谢你的帮助

我假设你想在 E 列中的所有值都为零时隐藏它?

将值求和到另一个单元格(在我的示例中为 X99),然后使用以下代码:

With ActiveSheet
  If .Range("X99").Value = 0 Then
      .Range("e:e").EntireColumn.Hidden = True
  Else
      .Range("e:e").EntireColumn.Hidden = False
  End If
End With

编辑:

如果你有负值,你可以使用 Abs(Min(E:E))>0 而不是 Sum


出于某种原因,我无法添加另一个答案,所以这里进行另一个编辑。

要隐藏 e 列中有零的

Dim i As Integer
Dim pa As Range

Dim ecolnumber As Integer
ecolnumber = 5

Set pa = Range(ActiveSheet.PageSetup.PrintArea)

For i = 0 To pa.Rows.Count

  Dim ecell As Range
  Set ecell = pa(i, ecolnumber)

  ecell.EntireRow.Hidden = ecell.Value = 0   

Next

注意 ecolnumber,您可能需要更改它以引用正确的列。

完成所有操作后,您可以使用以下命令取消隐藏行:

For i = 0 To pa.Rows.Count

  Set ecell = pa(i, ecolnumber)

  ecell.EntireRow.Hidden = False

Next

假设 Sheet1 的 E 列是您要隐藏的填充零的列:

Sub hideZeroFilledColumn()

    Dim rng As Range
    Set rng = ThisWorkbook.Worksheets("Sheet1").Range("E:E")

    rng.EntireColumn.Hidden = (Excel.WorksheetFunction.Count(rng) = _
                                Excel.WorksheetFunction.CountIf(rng, "0"))

End Sub

或者,如果您只想在列 E:E 中的单元格值为 0 时隐藏行:

Sub hideLineWithZero()

    Dim WS As Worksheet
    Set WS = ThisWorkbook.Worksheets("Sheet1")

    Dim strColumn As String
    strColumn = "E" 'If the column you want to check is "E:E"

    'Getting first row of printarea and setting "rngPrintStart" to that row in column strColumn
    Dim rngPrintStart As Range
    'The split is used to separate the start and end of the PrintArea address
    'Here we take component "0" of the split, so the start part of the PrintArea
    Set rngPrintStart = Range(Split(WS.PageSetup.PrintArea, ":")(0))
    Set rngPrintStart = WS.Range(strColumn & rngPrintStart.Row)

    'Getting last row of printarea and setting "rngPrintEnd" to that row in column strColumn
    Dim rngPrintEnd As Range
    'The split is used to seperate the start and end of the PrintArea address
    'Here we take component "1" of the split, so the end part of the PrintArea
    Set rngPrintEnd = Range(Split(WS.PageSetup.PrintArea, ":")(1))
    Set rngPrintEnd = WS.Range(strColumn & rngPrintEnd.Row)

    'Merging rngPrintStart and rngPrintEnd ranges from printarea in column strColumn
    Dim rngPrintColumnE As Range
    Set rngPrintColumnE = WS.Range(rngPrintStart, rngPrintEnd)

    Dim rng As Range
    Dim rngToHide As Range
    'looking in all cells from rngPrintColumnE
    For Each rng In rngPrintColumnE
        'checking if cell value is equal to 0 and is not empty
        If (rng.Value2 = 0) And (rng.Value2 <> "") Then
            'Building the range to be hidden
            If rngToHide Is Nothing Then 'For the first time when "rngToHide" is not yet set
                Set rngToHide = rng
            Else
                Set rngToHide = Union(rngToHide, rng)
            End If
        End If
    Next rng

    'to hide the rows from the previously built range
    rngToHide.Rows.EntireRow.Hidden = True

End Sub