动态定义选择数据的标准

时间:2016-04-11 23:38:10

标签: excel vba excel-vba

我需要一些帮助;我正在尝试从一张纸上选择数据并转移到另一张纸上。我用来选择此数据的标准是“发货”,因为它在行.Range("A1:H" & lRow).AutoFilter Field:=2, Criteria1:="Ship"中说明但我想通过使用Criteria1:= Range(“A2”)之类的东西来动态选择标准,以便我可以在Excel工作表上定义条件,而不必进入VBA代码。我该怎么做呢? 谢谢, 基什

Sub ImportShipper()    
    Dim wsEff As Worksheet
    Dim wsShip As Worksheet
    Dim wsFirst As Worksheet
    Set wsEff = Worksheets("Efficiency")
    Set wsFirst = Worksheets("1")    
    Set wsShip = ActiveSheet
    wsShip.Name = wsFirst.Range("B34").Value     
    With wsEff
        Dim lRow As Long
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("A1:H" & lRow).AutoFilter Field:=2, Criteria1:="Ship"
        Dim rngCopy As Range
        'All Columns A:H
        Set rngCopy = .Columns("A:H")
        'filtered rows, not including header row - assumes row 1 is headers
        Set rngCopy = Intersect(rngCopy, .Range("A1:H" & lRow), .Range("A1:H" & lRow).Offset(1)).SpecialCells(xlCellTypeVisible)
        rngCopy.Copy
        End With
    wsShip.Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Worksheets("Efficiency").ShowAllData
    End Sub

1 个答案:

答案 0 :(得分:1)

替换:

   Criteria1:="Ship"

使用:

   Criteria1:=wsWhatever.Range("A2").Value

您可以在适当的 A2 单元格中使用您喜欢的任何工作表。