简体   繁体   English

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

[英]Copy data from specific columns to another sheet based on a cell value

I am new to VBA and I am trying to have a macro that will copy data from specific columns, should the row meet a certain criteria.我是 VBA 的新手,我正在尝试使用一个宏来复制特定列中的数据,如果该行符合特定标准。

While the original document has more columns, the basic premise is that I have data entered into A through to F. If the value in F is Yes then I would like to copy data from column A, C & E to another sheet in A, B and C respectively.虽然原始文档有更多列,但基本前提是我已经将数据输入到 A 到 F。如果 F 中的值为 Yes,那么我想将 A 列 C 和 E 中的数据复制到 A 中的另一张表, B 和 C 分别。

I do not want the original data deleted but to stay on the page.我不想删除原始数据,而是留在页面上。

I have been using the below code which I tried to update from another code, but it was using UsedRange so I was trying to remove this and just use last row.我一直在使用下面的代码,我试图从另一个代码中更新它,但它使用的是 UsedRange,所以我试图删除它并只使用最后一行。

Sub MoveData_Confirmed()

    Dim xRg As Range
    Dim xCell As Range
    Dim i As Long
    Dim J As Long
    Dim K As Long
    i = Sheets("BP - Running Sheet").Cells(Rows.Count, "B").End(xlUp).Row
    J = Sheets("Running Sheet").Cells(Rows.Count, "B").End(xlUp).Row
    If J = 1 Then
    If Application.WorksheetFunction.CountA(Worksheets("Running Sheet").UsedRange = 0 Then J = 0
    End If
    Set xRg = Worksheets("BP - Running Sheet").Range("W2:W" & i)
    On Error Resume Next
    Application.ScreenUpdating = False
    For K = 1 To xRg.Count
        If CStr(xRg(K).Value) = "Yes" Then
            xRg(K).Range("B:D").Copy
            Worksheets("Running Sheet").Range("A" & J + 1).PasteSpecial Paste:=xlPasteValues
            xRg(K).Range("F:F").Copy
            Worksheets("Running Sheet").Range("D" & J + 1).PasteSpecial Paste:=xlPasteValues
            xRg(K).Range("I:I").Copy
            Worksheets("Running Sheet").Range("F" & J + 1).PasteSpecial Paste:=xlPasteValues
            xRg(K).Range("K:K").Copy
            Worksheets("Running Sheet").Range("G" & J + 1).PasteSpecial Paste:=xlPasteValues
            xRg(K).Range("N:N").Copy
            Worksheets("Running Sheet").Range("I" & J + 1).PasteSpecial Paste:=xlPasteValues
            xRg(K).Range("O:O").Copy
            Worksheets("Running Sheet").Range("J" & J + 1).PasteSpecial Paste:=xlPasteValues
            xRg(K).Range("P:P").Copy
            Worksheets("Running Sheet").Range("K" & J + 1).PasteSpecial Paste:=xlPasteValues
            xRg(K).Range("Q:Q").Copy
            Worksheets("Running Sheet").Range("M" & J + 1).PasteSpecial Paste:=xlPasteValues
            xRg(K).Range("V:V").Copy
            Worksheets("Running Sheet").Range("H" & J + 1).PasteSpecial Paste:=xlPasteValues
            J = J + 1
        End If
    Next
    Application.ScreenUpdating = True
End Sub

I find arrays nicer and faster to deal with:我发现 arrays 更好更快地处理:

Option Explicit
Sub MoveData()

    Dim InputArray
    Dim OutputArray
    Dim I As Integer
    Dim N As Integer
    Dim CNT As Integer
    Dim CheckRow As Integer
    Dim LastCol As Integer
    Dim RunningSheet As Worksheet
    Dim BP_RunningSheet As Worksheet
    
    Worksheets("BP - Running Sheet").Activate
    Set RunningSheet = Worksheets("Running Sheet")
    Set BP_RunningSheet = Worksheets("BP - Running Sheet")
    
    CheckRow = 11   'Column "K" = column #11, if that changes, the letter
    InputArray = BP_RunningSheet.Range("A1:Z" & Range("A" & Rows.Count).End(xlUp).Row).Value
    ReDim OutputArray(1 To UBound(InputArray, 1), 1 To UBound(InputArray, 2))
    LastCol = UBound(InputArray, 2)
    CNT = 1
    
    For I = 1 To UBound(InputArray, 1)
        'Debug.Print InputArray(I, CheckRow)
        If InputArray(I, CheckRow) = "Yes" Then
            For N = 1 To 3  'B:D to A:C
                OutputArray(CNT, N) = InputArray(I, N + 1)
            Next N
            OutputArray(CNT, 4) = InputArray(I, 6) 'F to D
            OutputArray(CNT, 6) = InputArray(I, 9) 'I to F
            OutputArray(CNT, 7) = InputArray(I, 11) 'K to G
            OutputArray(CNT, 9) = InputArray(I, 14) 'N to I
            OutputArray(CNT, 10) = InputArray(I, 15) 'O to J
            OutputArray(CNT, 11) = InputArray(I, 16) 'P to K
            OutputArray(CNT, 13) = InputArray(I, 17) 'Q to M
            OutputArray(CNT, 8) = InputArray(I, 22) 'V to H
            CNT = CNT + 1
        End If
    Next I
    
    RunningSheet.Range("A1").Resize(UBound(OutputArray, 1), UBound(OutputArray, 2)).Value = OutputArray
    Worksheets("Running Sheet").Activate
    
End Sub

Input Sheet "BP_RunningSheet":输入表“BP_RunningSheet”: BP_RunningSheet

Output Sheet "RunningSheet": Output 表“RunningSheet”: 运行表

Sheet Architecture:表架构: 表架构

PS: PS:

I would encourage you to try the "=Filter(Array, Criteria)" function for this.我鼓励您为此尝试“=Filter(Array, Criteria)”function。 It is much faster to implement than VBA.它的实现比 VBA 快得多。

暂无
暂无

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

相关问题 根据单元格值将特定数据(不是整行!)复制到另一张表 - Copy Specific data (Not the entire row!) to another sheet based on cell value 根据第一张工作表中第二个单元格的值,将数据从一个单元格复制到另一张工作表中的单元格? - Copy data from one cell to a cell in another sheet based on the value of a second cell in the first sheet? 根据单元格值将数据从一张表复制并粘贴到另一张表 - Copy and paste data from one sheet to another sheet(s) based on cell value Excel VBA 根据第二列单元格值将列从一张表复制到另一张表 - Excel VBA to Copy Column from one sheet to another based on a second columns cell value 根据条件将单元格数据从一张纸复制到另一张纸 - copy cell data from one sheet to another based on condtion 根据单元格中的值将行复制到另一张工作表 - Copy row to another sheet based on value in a cell 使用单元格值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another with the cell value 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet 根据单元格值从另一张表中提取数据 - Pull data based from another sheet based on a cell value excel根据另一个单元格值从另一个工作表中提取数据 - excel pulling data from another sheet based on another cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM