简体   繁体   English

根据多个单元格中的条件将一行中的单元格(不是整行)复制到另一个工作表

[英]Copy cells in a row (not entire row) based on criteria in multiple cells to another sheet

I have a spreadsheet with data for 1 year that I'm needing to separate onto quarterly sheets.我有一个包含 1 年数据的电子表格,我需要将其分成季度工作表。

The data I need copied is in columns A:B & L:N, but only if there is a "Y" in columns L:N.我需要复制的数据位于 A:B 和 L:N 列中,但前提是 L:N 列中有“Y”。

The main data is on sheet "Client List", & the destination sheet is "Wool 1st Qtr".主要数据在“客户列表”表上,目标表是“Wool 1st Qtr”。 I have 2 header rows, making the data starting on row 3.我有 2 个 header 行,使数据从第 3 行开始。

I've been looking at both Formulas & VBAs but I'm struggling to find a similar answer on Google that I can modify, & after looking at what feels like 100 different questions they're all starting to look the same!我一直在查看公式和 VBA,但我很难在 Google 上找到一个可以修改的类似答案,并且在查看了 100 个不同的问题之后,它们都开始看起来一样了!

I've tried this code using a command button, but it is copying the entire row.我已经使用命令按钮尝试了此代码,但它正在复制整行。 It is also only taking the "Y" from one column.它也只从一列中取“Y”。

Private Sub CommandButton1_Click()  
a = Worksheets("Client List").Cells(Rows.Count, 1).End(xlUp).Row

For i = 3 To a

    If Worksheets("Client List").Cells(i, 12).Value = "Y" Then
    
        Worksheets("Client List").Rows(i).Copy
        Worksheets("Wool 1st Qtr").Activate
        b = Worksheets("Wool 1st Qtr").Cells(Rows.Count, 1).End(xlUp).Row
        Worksheets("Wool 1st Qtr").Cells(b + 1, 1).Select
        ActiveSheet.Paste
        Worksheets("Client List").Activate
        
    End If
Next

Application.CutCopyMode = False

ThisWorkbook.Worksheets("Client List").Cells(3, 1).Select

End Sub

Export to Another Worksheet导出到另一个工作表

Option Explicit

Private Sub CommandButton1_Click()
    ExportQuarter
End Sub

Sub ExportQuarter()
    
    Const sName As String = "Client List"
    Const sCols1 As String = "A:B"
    Const sCols2 As String = "L:N"
    Const slrCol As String = "A"
    Const sfRow As Long = 3
    Const sCriteriaString As String = "Y"
    
    Const dName As String = "Wool 1st Qtr"
    Const dCol As String = "A"
    
    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim sws As Worksheet: Set sws = wb.Worksheets(sName)
    Dim slRow As Long: slRow = sws.Cells(sws.Rows.Count, slrCol).End(xlUp).Row
    If slRow < sfRow Then Exit Sub ' no data
    Dim srCount As Long: srCount = slRow - sfRow + 1
    Dim srg As Range: Set srg = sws.Rows(sfRow).Columns(sCols2).Resize(srCount)
    Dim scCount As Long: scCount = srg.Columns.Count
    
    Dim ddrg As Range
    Dim srrg As Range
    For Each srrg In srg.Rows
        If Application.CountIf(srrg, sCriteriaString) = scCount Then
            If ddrg Is Nothing Then
                Set ddrg = srrg.Cells(1)
            Else
                Set ddrg = Union(ddrg, srrg.Cells(1))
            End If
        End If
    Next srrg
    
    If ddrg Is Nothing Then Exit Sub ' no match
    
    Dim dws As Worksheet: Set dws = wb.Worksheets(dName)
    Dim dfCell As Range
    Set dfCell = dws.Cells(dws.Rows.Count, dCol).End(xlUp).Offset(1)
    
    Intersect(ddrg.EntireRow, Union(srg, sws.Columns(sCols1))).Copy dfCell
    
    MsgBox "Quarter exported.", vbInformation, "ExportQuarter"
    
End Sub

暂无
暂无

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

相关问题 VBA-根据多个条件从另一张纸复制单元格 - VBA - copy cells from another sheet based on multiple criteria 通过根据条件覆盖另一个工作表中的单元格来复制单元格和粘贴 - Copy cells and paste by overwriting cells in another sheet based on a criteria Excel-宏以比较/匹配两张纸之间一行中的多个单元格,并将整行复制到第二张纸上 - Excel - Macro to compare /match multiple cells in a row between two sheets, and copy the entire row to the second sheet 将整行(首先复制 A,然后复制 B,然后复制 C)复制到其特定单元格中的另一张纸上 - Copy an entire row (first copy A then B and then C) to another sheet in their specific cells 根据另一个单元格中的条件将单元格从一个工作表复制到另一个工作表 - Copy cells from one sheet to another based on criteria in another cell 根据另一列中的条件将一列中的单元格复制到另一张工作表 - Copy Cells in One Column, Based on Criteria in Another Column, to Another Sheet 查找符合条件的列,并将该行中的其他单元格复制到新工作表中 - Find column that meet a criteria and copy other cells in that row to a new sheet 遍历列中的单元格(如果包含条件),将行复制到新表 - Iterate through cells in a column, if contains criteria, copy row to new sheet VBA附加-根据条件将单元格从一张纸复制到另一张纸 - VBA Append - copy cells froma sheet to another based to a criteria 将数据从工作表中的单元格复制到另一个工作表中的下一个可用行 - Copy data from cells in sheet to next available row in another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM