2017-08-11 72 views
0

我實際的代碼是:Excel中不打印,如果零

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 <[email protected]>" 
    MacExcel2016WithMacMailPDF subject:=[C6] & Format(Date, "dd/mm/yy"), _ 
    mailbody:=strbody, _ 
    toaddress:=[C3], _ 
    ccaddress:=[C4], _ 
    bccaddress:=[C5], _ 
    attachment:=FilePathName, _ 
    displaymail:=True, _ 
    thesignature:="", _ 
    thesender:="" 
End Sub 

我想這給E列的打印區域的所有小區= 0不顯示和紙張收縮本身(如刪除行是= 0),這之前創建.pdf文件和打開郵箱。

我不知道如果我不夠清晰對不起

謝謝您的幫助雖然

回答

0

假設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 
+0

謝謝,我想隱藏與列中零值對應的行(隱藏所有行,結果爲零E) –

+1

它完美的工作,非常感謝你的幫助傢伙! –

0

我假設你要隱藏的列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(敏(E:E)) > 0,而不是總和,如果您有負值


出於某種原因,我不能添加另一種答案所以在這裏與其他編輯去。

要隱藏,在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 
+0

如果有可能存在ne積極和積極的價值觀,這不會是充分的證據。 –

+0

@simpLE MAn這將是_fool proof_然後,我們都不是完美的:-) –

+0

我會糾正我的意見,但它已經太遲了;) –