1

Good afternoon everyone,

I am new here and only have reasonable knowledge about VBA, so please excuse if my terminology may not be right.

I have an excel file with several macros I have written. The main macro applies an autofilter to all my data based on a criteria I type in a cell on a separate sheet within the same workbook. The filter is working fine and if the criteria specified matches a record in my data then that data is copied and pasted into a separate sheet. Unfortunately, however, if I enter a criteria that is not present in the data, excel/VBA copies all of my data (each of the 7000 records) and pastes the entire data into the separate sheet although no result was actually found. I was hoping you would be able to give me advice on how to overcoem this and what code to use to make the filter work properly.

Basically what I want is that if the filter cannot match any data, then no data is copied/pasted. My macro checks several criteria (total 8) and then applies the autofilter. I don't know if I would need a code and add that to each of my code lines for each criteria, or only one at the end.

Thank you for your help!

4

1 回答 1

0

我将通过将表中的行数与过滤表中的行数进行比较来检查过滤器是否产生任何结果/行。

'assuming the table starts from A1,
MsgBox Range("A1").CurrentRegion.Rows.Count
'confirms the number of rows in the table.
'After trying AutoFilter,
MsgBox ActiveSheet.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count
'confirms the number of filtered rows.

将这两个值存储在变量中并进行比较,或者只检查第二个值是否为 1(标题行)。如果是一个,请不要尝试复制。

于 2013-07-12T18:06:58.843 回答