简体   繁体   English

Excel 2007 VBA Autofiltr:选择Case IsEmpty与复制

[英]Excel 2007 VBA Autofiltr: Select Case IsEmpty vs Copying

I am struggling with my macro. 我正在努力与我的宏。

GOAL & MAIN IDEA: It opens different xls files, based on Autofilter it checks 10th column to choose the rows with four "last-friday-days" (so if we have 18/02 it returns 13/02, 06/02, 30/01, 23/01.) 目标和主要想法:它打开不同的xls文件,基于Autofilter它检查第10列选择具有四个“最后一个星期五 - 天”的行(所以如果我们有18/02它返回13 / 02,06 / 02,30 / 01,23 / 01。)

PROBLEM: Problem is only when in my file there are no dates like aboves.Even though the file should be closed automatically it try to copying empty range!! 问题:问题是只有在我的文件中没有像上面那样的日期。即使文件应该自动关闭它尝试复制空范围! I use Select Case (code below) 我使用Select Case(下面的代码)

空选择但仍在复制

CODE: 码:

 Rows("4:4").Select '*Autofilter - this part is ok*
 Selection.AutoFilter *' four last-Fridays from the Column - ok*
 ActiveSheet.Range("J5", Range("J5").End(xlDown)).AutoFilter Field:=10, Criteria1:=Array( _
    LastFridayDate, Friday2weeksagoDate, Friday3weeksagoDate, Friday4weeksagoDate), Operator:= _
       xlFilterValues

Range("A4").Offset(1, 0).Activate
  Select Case IsEmpty(ActiveCell) ' *MISTAKE!!* 
    Case True MISTAKE!! *no data in file, file should be closed!*
        Workbooks("prima UKACZEE.xls").Close SaveChanges:=False
    Case Else ' *this part always works*
        Range("A5", Range("A5").End(xlDown).End(xlToRight)).Select
        Selection.Copy
        Workbooks("Makro XXX Claim Hours v.01.xls").Sheets("TRN Claim Report - Labour").Activate
        Range("A5").End(xlDown).Offset(1, 0).Activate
        ActiveSheet.Paste

    Application.DisplayAlerts = False *' closing file - ok*
    Workbooks("prima UKACZEE.xls").Close SaveChanges:=False
    Application.DisplayAlerts = True
End Select

If you could help. 如果你能提供帮助。 Appreciate 欣赏

The issue here is that AutoFilter doesn't make rows non-existent, it simply hides them. 这里的问题是AutoFilter不会使行不存在,它只是隐藏它们。 Regardless of whether a filter has been applied Range("A4").Offset(1,0) will always refer to cell A5 so if there's a value in A5 then IsEmpty(ActiveCell) will always return false. 无论是否已应用过滤器Range("A4").Offset(1,0)将始终引用单元格A5,因此如果A5中有值,则IsEmpty(ActiveCell)将始终返回false。

To solve this problem you're going to have to use the SpecialCells method of the Range object. 要解决此问题,您将不得不使用Range对象的SpecialCells方法。 The below code will activate the 1st visible cell in column A that's below cell A4, in your case the first row of results after filtering. 下面的代码将激活A列中位于单元格A4下方的第一个可见单元格,在您的情况下是过滤后的第一行结果。

Range("A5:A" & ActiveSheet.Rows.Count).SpecialCells(xlCellTypeVisible).Cells(1, 1).Activate

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

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