简体   繁体   English

根据条件将单元格从特定列复制到另一个工作表

[英]Copy cells from a specific column to another worksheet based on criteria

I have two worksheets, "Signed" and "April". 我有两个工作表,“签名”和“ 4月”。 I want to copy Column "Y" from "Signed" based on certain criteria into column "A" of "April" starting from the next available/blank row. 我想从下一个可用/空白行开始,基于某些条件将“已签名”列中的“ Y”列复制到“四月”列“ A”中。 ( so right under the existing data). (在现有数据下也是如此)。 My criteria for column Y is that if column L = month of cell "D2" from "April" AND the year of cell "D2" from "ApriL"...( so right now D2 is 4/30/2017).. then copy that cell in the next available row of Col A of "April" and keep adding on. 我对Y列的标准是,如果L列=来自“ April”的单元格“ D2”的月份和来自“ ApriL”的单元格“ D2”的年份...(所以D2现在是4/30/2017)。然后将该单元格复制到“ April”的Col A的下一个可用行中,然后继续添加。

I've been trying several different things but just am not able to get it..any idea on how I can achieve this? 我一直在尝试几种不同的方法,但是却无法获得..关于如何实现此目标的任何想法?

My code is below: 我的代码如下:

Set sourceSht = ThisWorkbook.Worksheets("Signed")
Set myRange = sourceSht.Range("Y1", Range("Y" & Rows.Count).End(xlUp))
Set ws2 = Sheets(NewSheet)
DestRow = ws2.Cells(Rows.Count, "A").End(xlUp).Row + 1



For Each rw In myRange.Rows
If rw.Cells(12).Value = "Month(Sheets(ws2).Range("D2"))" Then
myRange.Value.Copy Destinations:=Sheets(ws2).Range("A" & DestRow)

End If

Something like this should work for you: 这样的事情应该为您工作:

Sub tgr()

    Dim wb As Workbook
    Dim wsData As Worksheet
    Dim wsDest As Worksheet
    Dim aData As Variant
    Dim aResults() As Variant
    Dim dtCheck As Date
    Dim lCount As Long
    Dim lResultIndex As Long
    Dim i As Long

    Set wb = ActiveWorkbook
    Set wsData = wb.Sheets("Signed")        'This is your source sheet
    Set wsDest = wb.Sheets("April")         'This is your destination sheet
    dtCheck = wsDest.Range("D2").Value2     'This is the date you want to compare against

    With wsData.Range("L1:Y" & wsData.Cells(wsData.Rows.Count, "L").End(xlUp).Row)
        lCount = WorksheetFunction.CountIfs(.Resize(, 1), ">=" & DateSerial(Year(dtCheck), Month(dtCheck), 1), .Resize(, 1), "<" & DateSerial(Year(dtCheck), Month(dtCheck) + 1, 1))
        If lCount = 0 Then
            MsgBox "No matches found for [" & Format(dtCheck, "mmmm yyyy") & "] in column L of " & wsData.Name & Chr(10) & "Exiting Macro"
            Exit Sub
        Else
            ReDim aResults(1 To lCount, 1 To 1)
            aData = .Value
        End If
    End With

    For i = 1 To UBound(aData, 1)
        If IsDate(aData(i, 1)) Then
            If Year(aData(i, 1)) = Year(dtCheck) And Month(aData(i, 1)) = Month(dtCheck) Then
                lResultIndex = lResultIndex + 1
                aResults(lResultIndex, 1) = aData(i, UBound(aData, 2))
            End If
        End If
    Next i

    wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Resize(lCount).Value = aResults

End Sub

Alternate method using AutoFilter instead of iterating over an array: 使用自动筛选而不是遍历数组的替代方法:

Sub tgrFilter()

    Dim wb As Workbook
    Dim wsData As Worksheet
    Dim wsDest As Worksheet
    Dim dtCheck As Date

    Set wb = ActiveWorkbook
    Set wsData = wb.Sheets("Signed")        'This is your source sheet
    Set wsDest = wb.Sheets("April")         'This is your destination sheet
    dtCheck = wsDest.Range("D2").Value2     'This is the date you want to compare against

    With wsData.Range("L1:Y" & wsData.Cells(wsData.Rows.Count, "L").End(xlUp).Row)
        .AutoFilter 1, , xlFilterValues, Array(1, Format(WorksheetFunction.EoMonth(dtCheck, 0), "m/d/yyyy"))
        Intersect(.Cells, .Parent.Columns("Y")).Offset(1).Copy wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1)
        .AutoFilter
    End With

End Sub

Here's a generic script which you can easily modify to handle almost ANY criteria, as needed. 这是一个通用脚本,您可以根据需要轻松修改该脚本以处理几乎所有条件。

Sub Copy_If_Criteria_Met()
    Dim xRg As Range
    Dim xCell As Range
    Dim I As Long
    Dim J As Long
    I = Worksheets("Sheet1").UsedRange.Rows.Count
    J = Worksheets("Sheet2").UsedRange.Rows.Count
    If J = 1 Then
       If Application.WorksheetFunction.CountA(Worksheets("Sheet2").UsedRange) = 0 Then J = 0
    End If
    Set xRg = Worksheets("Sheet1").Range("A1:A" & I)
    On Error Resume Next
    Application.ScreenUpdating = False
    For Each xCell In xRg
        If CStr(xCell.Value) = "X" Then
            xCell.EntireRow.Copy Destination:=Worksheets("Sheet2").Range("A" & J + 1)
            xCell.EntireRow.Delete
            J = J + 1
        End If
    Next
    Application.ScreenUpdating = True
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一列中的条件将一列中的单元格复制到另一张工作表 - Copy Cells in One Column, Based on Criteria in Another Column, to Another Sheet 根据特定的单词自动将单元格复制到另一个工作表 - Automatically Copy Cells to Another Worksheet Based on Specific Words 根据列中的日期将行中的特定单元格复制到月工作表 - Copy specific cells in a row to month worksheet based on date in column 根据条件将某个列中的单元格复制到下一个可用行中的另一个列中 - Copy cells from a certain column based on a criteria into another Column from the next available row 根据包含空单元格的列标题将数据从一个工作表复制到另一个工作表 - Copy data from one worksheet to another based on column heading including empty cells 将基于多个条件的行从一个工作表复制到另一个VBA - Copy Rows Based On Multiple Criteria from one Worksheet to Another VBA 将特定单元格从一个工作表复制并粘贴到另一个工作表 - Copy and Paste Specific Cells from one worksheet to another 如何将具有特定值的单元格中的数据复制到另一个工作表? - How to copy data from cells with specific value to another worksheet? 根据另一个单元格中的条件将单元格从一个工作表复制到另一个工作表 - Copy cells from one sheet to another based on criteria in another cell 如果条件如何将特定单元格从一张纸复制到另一张纸 - How to copy specific cells from one sheet to another meting if criteria
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM