【问题标题】:Excel VBA: filter list for "empty" (failed matching) values then copy and paste to new column Code loopExcel VBA:“空”(匹配失败)值的过滤列表,然后复制并粘贴到新列代码循环
【发布时间】:2014-12-27 17:14:21
【问题描述】:

我是 excel vba 的新手,但基本上我想做的是比较两个列表的共同值。 有一些单元格显示为空但包含“”作为不匹配的结果。 我正在尝试遍历列表,并且仅将不是“”的值复制到新列。 出于某种原因,除非我停止它,否则 Excel 会一直循环,而代码永远不会完成。

Sub Order()

Dim x As Long
x = 2
ActiveSheet.Cells(x, 10).Select
While x < 19112 And ActiveCell.Value <> " "
ActiveSheet.Cells(x, 10).Select
ActiveCell.Copy
ActiveSheet.Cells(x, 11).Select
ActiveSheet.PasteSpecial
Wend
x = x + 1
End Sub

【问题讨论】:

    标签: excel list vba loops filter


    【解决方案1】:

    现在,在分析下面的代码之前,请先看看How to avoid using .Select in Excel VBA Macros

    您可能想尝试使用 AutoFilter 的更快解决方案,而不是相当慢的循环

    Sub BetterOne()
    
        Dim lastRow As Long
        lastRow = Range("J" & Rows.Count).End(xlUp).Row
    
        Dim columnJ As Range
        Set columnJ = Range("J1:J" & lastRow)
    
        columnJ.AutoFilter
        columnJ.AutoFilter Field:=1, Criteria1:="<>"
        columnJ.Copy
        Range("K1").PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        columnJ.AutoFilter
    End Sub
    

    【讨论】:

    • 好的,我想我明白了。但是,我需要的标准不是“”。我的意思是任何不是“”的东西,那么您将如何指定。
    • 我猜反过来=
    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多