0

我有一个包含数据行的 Excel 表。J 列包含商品的各种描述。我需要在该列中的所有行中搜索单词 LATEX,找到后,仅将该单词复制到同一行同一工作表上的 A 列。我试图找到一个解决方案并使用 Autofilter 想出了这个宏,但它不能正常工作。你能帮我么?

Sub FilterAndCopy()

    Dim dataWs As Worksheet
    Dim copyWs As Worksheet
    Dim totRows As Long
    Dim lastRow As Long

    Set dataWs = Worksheets("Massiv")
    Set copyWs = Worksheets("Massiv")

    With dataWs
        .AutoFilterMode = False
        With .Range("J:J")
             .AutoFilter Field:=1, Criteria1:="LATEX"
        End With
    End With

    totRows = dataWs.Range("J:J").Rows.count
    lastRow = dataWs.Range("J" & totRows).End(xlUp).Row
    dataWs.Range("J:J" & lastRow).Copy
    copyWs.Range("A6").PasteSpecial Paste:=xlPasteValues
    dataWs.AutoFilterMode = False
4

1 回答 1

0

通过以下更改,您的代码应该可以工作。我已经注意到代码中注释的变化。

With dataWs
    .AutoFilterMode = False
    With .Range("J:J")
         'Use wildcard to search for word LATEX within contents of column J cells 
         .AutoFilter Field:=1, Criteria1:="*LATEX*"
    End With
End With

totRows = dataWs.Range("J:J").Rows.Count
lastRow = dataWs.Range("J" & totRows).End(xlUp).Row
'After filtering, select the visible cells in column A...
Set rng = dataWs.Range("A2:A" & lastRow).SpecialCells(xlCellTypeVisible)
'... and set their values to "LATEX"
rng.Value = "LATEX"
dataWs.AutoFilterMode = False
于 2013-08-07T11:28:23.267 回答