简体   繁体   English

在打印按钮后运行宏

[英]run macro after print button

We are looking for a macro that after a user hits the print button will delete an entire row if Column I has value of zero. 我们正在寻找一个宏,如果第I列的值为零,则在用户单击打印按钮后将删除整行。 Its deleting everything. 其删除所有内容。 http://i.imgur.com/1SVDVLi.jpg http://i.imgur.com/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

This code worked for me. 这段代码对我有用。

  • Get rid of "Sub DeleteRows()" - Not sure why that was in your function 摆脱“ Sub DeleteRows()”-不确定为什么在您的函数中
  • Set SrchRng so that it only searches column I 设置SrchRng,使其仅搜索第I列
  • Remove the quotes from SrchRng.Find("0") 从SrchRng.Find(“ 0”)删除引号

Here it is with the changes: 这就是更改:

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

Also, this should do the trick if you just want to use a For Loop 另外,如果您只想使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM