根据单元格内容将行移动到另一个工作表

问题描述 投票:-2回答:2

我是VBA的新手,如果一个单元格中有“是”,我正在尝试写一些会复制整行的内容。

源位于名为“Master List”的工作表上,目标工作表称为“CE Class”

如果列F有“是”,则从“主列表”中复制该行,并从第4行开始将其置于“CE类”。

excel vba
2个回答
0
投票

根据您提供的描述,这样的东西应该适合您。我评论了代码,试图弄清楚它在做什么以及为什么。

Sub tgr()

    'Declare variables
    Dim wb As Workbook
    Dim wsData As Worksheet
    Dim wsDest As Worksheet
    Dim rCopy As Range
    Dim FCell As Range

    'Assign object variables
    Set wb = ActiveWorkbook
    Set wsData = wb.Sheets("Master List")
    Set wsDest = wb.Sheets("CE Class")

    'Loop through each cell in column F
    For Each FCell In wsData.Range("F1", wsData.Cells(wsData.Rows.Count, "F").End(xlUp)).Cells
        'Check if the F Cell's value is "yes"
        If Trim(LCase(FCell.Value)) = "yes" Then
            'Found a "yes", add it to rCopy
            If rCopy Is Nothing Then
                Set rCopy = FCell
            Else
                Set rCopy = Union(rCopy, FCell)
            End If
        End If
    Next FCell

    'Check if there is any data to copy over
    If Not rCopy Is Nothing Then
        'There is data to copy, clear the old results
        wsDest.Range("A4", wsDest.Cells(wsDest.Rows.Count, "A")).EntireRow.Clear

        'Copy over the data
        rCopy.EntireRow.Copy wsDest.Range("A4")
    End If

End Sub

0
投票

您不必使用VBA,只需转到所需的单元格并粘贴逻辑命令。 = IF(F1 = “是”,masterlist!A1) 仅供参考:该命令也适用于vba 你也可以玩A1值,看看它是如何工作的

© www.soinside.com 2019 - 2024. All rights reserved.