【问题标题】:VBA Filter and copy data on ConditionVBA根据条件过滤和复制数据
【发布时间】:2020-07-02 15:42:10
【问题描述】:

我有一张需要根据某些条件进行过滤的工作表,然后将第一列值/和列 AT 复制到另一张工作表。 第一张表(Sheet1)包含多行(但我们只需要使用 A 和 AT 列) 因此,如果 AT 列包含“N/A”或空白值..那么我们需要将 A 列和 AT 值复制到 Sheet2。 我正在编写如下的 VBA 代码并停留在 YDest 表中的过滤中,我需要在其中过滤数据并放入 Ydest 上的另一个表“缺少信息”

Private Sub Grab_Click()
    Dim xSource As Workbook
    Dim yDest As Workbook
    '## Open both workbooks first:

    Set xSource = Workbooks.Open("Vendor Dispatch new.xlsx")
    Set yDest = Workbooks.Open("Vendor DisPatch Standard.xlsm")

    With xSource.Sheets("Vendor Dispatch new").UsedRange
        'Now, paste to y worksheet:
        yDest.Sheets("Vendor Dispatch new").Range("A2").Resize( _
            .Rows.Count, .Columns.Count) = .Value
        yDest.Sheets("Vendor Dispatch new").Range("A2").WrapText = True
    End With
    yDest.Sheets("Vendor Dispatch new").Rows("2:4").Delete
    'y.Sheets("Vendor Dispatch new").Range("1:1").EntireRow.Interior.Color = 1280
    'Filter Data with copy into MissingInfoSheet
    xSource.Close
    yDest.Save
    yDest.Close
End Sub

【问题讨论】:

  • 你的问题是?..你能举个例子吗?

标签: excel vba macos


【解决方案1】:

试试这个。这是一种使用变体数组的方法。

Sub test()
    Dim Ws As Worksheet, toWs As Worksheet
    Dim vDB, vR()
    Dim xSource As Workbook
    Dim yDest As Workbook
    Dim i As Long, n As Long, c As Integer, j As Integer
    '## Open both workbooks first:

    Set xSource = Workbooks.Open("Vendor Dispatch new.xlsx")
    Set yDest = Workbooks.Open("Vendor DisPatch Standard.xlsm")

    Set Ws = xSource.Sheets("Vendor Dispatch new")
    Set toWs = yDest.Sheets("Vendor Dispatch new")

    With Ws
        r = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        c = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        vDB = .Range("a1", .Cells(r, c))
        For i = 1 To r
            If IsError(vDB(i, 46)) Then
                n = n + 1
                ReDim Preserve vR(1 To c, 1 To n)
                For j = 1 To c
                    vR(j, n) = vDB(i, j)
                Next j
            Else
                If vDB(i, 46) = "" Then
                    n = n + 1
                    ReDim Preserve vR(1 To c, 1 To n)
                    For j = 1 To c
                        vR(j, n) = vDB(i, j)
                    Next j
                End If
            End If
        Next i
    End With
    With toWs
        .Cells.Clear
        .Range("a2").Resize(n, c) = WorksheetFunction.Transpose(vR)
    End With
    xSource.Close
    yDest.Save
    yDest.Close
End Sub

【讨论】:

    猜你喜欢
    • 2018-10-08
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多