gpt4 book ai didi

vba - Excel VBA 自动筛选添加空行

转载 作者:行者123 更新时间:2023-12-02 19:46:18 24 4
gpt4 key购买 nike

我修改了 Excel 宏,之前它是逐行进行的,现在它会批量过滤结果和副本。效率更高。

我现在遇到的问题是,自动筛选器向工作表中添加了数百万个空行,但我无法确定为什么会这样做。

CountryCodes 是一个字典,其中包含过滤器的值。Criteria 正在查找包含字典中的条目的行。

这是代码:

    For Each vall In CountryCodes
thisWB.Activate
thisWB.Sheets("Overall Numbers").Activate

lookfor = CountryCodes.Item(vall)
rep = Replace(thisWBName, "EMEA", lookfor)

Set rng = ActiveSheet.Range("A1:Z1")

FilterField = WorksheetFunction.Match("Host", rng.Rows(1), 0)

If ActiveSheet.AutoFilterMode = False Then rng.AutoFilter

rng.AutoFilter Field:=FilterField, Criteria1:="=*" & lookfor & "*", Operator:=xlFilterValues

Set rng2 = ThisWorkbook.Worksheets("Overall Numbers").Cells.SpecialCells(xlCellTypeVisible)

rng2.Copy Workbooks(rep).Worksheets("Overall Numbers").Range("A1")

Workbooks(rep).Save

thisWB.Activate
thisWB.Sheets("Overall Numbers").Activate

Cells.AutoFilter
Next

最佳答案

测试:

Dim ur As Range
Set ur = ThisWorkbook.Sheets("Overall Numbers").UsedRange

Application.ScreenUpdating = False
filterField = Application.Match("Host", ur.Rows(1), 0)
If Not IsError(filterField) Then

For Each vall In countryCodes
rep = Replace(thisWBName, "EMEA", vall)

ur.AutoFilter Field:=filterField, Criteria1:="=*" & vall & "*"

'copy visible rows with data only
ur.SpecialCells(xlCellTypeVisible).Copy

'paste visible rows with data only
Workbooks(rep).Worksheets("Overall Numbers").Range("A1").PasteSpecial xlPasteAll
Workbooks(rep).Save

ur.AutoFilter
Next
End If
Application.ScreenUpdating = True

关于vba - Excel VBA 自动筛选添加空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32374003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com