試編寫程式碼示例如下:
Private Sub CommandButton1_Click() Dim sText, sPrintArea "1只要B40:D40單元格有文字就列印A35:Q62區域 sText = Trim(Range("B40").Value & Range("C40").Value & Range("D40").Value) If sText "" Then sPrintArea = "$A$35:$Q$62" End If "只要B75:D75單元格有文字就列印A70:Q97區域 sText = Trim(Range("B75").Value & Range("D75").Value & Range("D75").Value) If sText "" Then If sPrintArea = "" Then sPrintArea = "$A$35:$Q$62" Else sPrintArea = "$A$35:$Q$62, $A$70:$Q$97" End If End If "只要B105:D105單元格有文字就列印A100:Q127區域 sText = Trim(Range("B105").Value & Range("C105").Value & Range("D105").Value) If sText "" Then If sPrintArea = "" Then sPrintArea = "$A$100:$Q$127" Else sPrintArea = sPrintArea & "," & "$A$100:$Q$127" End If End If If sPrintArea "" Then ActiveSheet.PageSetup.PrintArea = sPrintArea ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False End If End Sub
試編寫程式碼示例如下: Private Sub CommandButton1_Click() Dim sText, sPrintArea "1只要B40:D40單元格有文字就列印A35:Q62區域 sText = Trim(Range("B40").Value & Range("C40").Value & Range("D40").Value) If sText "" Then sPrintArea = "$A$35:$Q$62" End If "只要B75:D75單元格有文字就列印A70:Q97區域 sText = Trim(Range("B75").Value & Range("D75").Value & Range("D75").Value) If sText "" Then If sPrintArea = "" Then sPrintArea = "$A$35:$Q$62" Else sPrintArea = "$A$35:$Q$62, $A$70:$Q$97" End If End If "只要B105:D105單元格有文字就列印A100:Q127區域 sText = Trim(Range("B105").Value & Range("C105").Value & Range("D105").Value) If sText "" Then If sPrintArea = "" Then sPrintArea = "$A$100:$Q$127" Else sPrintArea = sPrintArea & "," & "$A$100:$Q$127" End If End If If sPrintArea "" Then ActiveSheet.PageSetup.PrintArea = sPrintArea ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False End If End Sub