[英]Code errors if there are no cells to copy and paste into a new sheet using paste special
Code errors if there are no cells to copy and paste into a new sheet, when I would like it to display nothing, or even a message like "no results" when the filter comes up with nothing to copy.如果没有单元格可复制并粘贴到新工作表中,则代码错误,当我希望它不显示任何内容时,或者当过滤器没有任何内容可复制时甚至出现“无结果”之类的消息。 Is this possible?
这可能吗?
Sub DepartmentSearch()
' Assembly Engineer
Sheets.Add.Name = ("Assembly Engineer")
Sheets("Assembly Engineer").Activate
TeamRole = "Assembly Engineer"
MaxDate = Date
Set rng = Worksheets("Overview").Range("Table1") ' source table
rng.AutoFilter Field:=13, Criteria1:=TeamRole ' filter Department
Worksheets("Overview").Range("Table1").AutoFilter Field:=8, Criteria1:="<" & MaxDate ' filter date
rng.Copy ' copy filtered rows
Range("A2").Select ' source table
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False ' paste values to target table
rng.AutoFilter ' turn off filter on source table
' Cost Accounting
Sheets.Add.Name = ("Cost Accounting")
Sheets("Cost Accounting").Activate
TeamRole = "Cost Accounting"
MaxDate = Date
Set rng = Worksheets("Overview").Range("Table1") ' source table
rng.AutoFilter Field:=13, Criteria1:=TeamRole ' filter Department
Worksheets("Overview").Range("Table1").AutoFilter Field:=8, Criteria1:="<" & MaxDate ' filter date
rng.Copy ' copy filtered rows
Range("A2").Select ' source table
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False ' paste values to target table
rng.AutoFilter ' turn off filter on source table
Add in a line at the start of your code like在代码开头添加一行,例如
On Error Goto ErrHandler
' then at the end add ' 然后在最后添加
exit sub
ErrHandler:
'code to do what you want if there's an error
msgbox "No Result"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.