将某些行复制到excel中的新工作表的公式

时间:2017-05-10 18:41:27

标签: excel excel-formula

我正在寻找一个公式,它会根据某些条件将行从一个工作表复制到另一个工作表(具体来说,是否突出显示该行)。怎么会这样做?

1 个答案:

答案 0 :(得分:0)

我最终只使用了VBA。我必须在开头使用是/否单元格来指定是否突出显示一行,以便每当有人更改值时它都会自动更新。

Sub Autosort()
Application.ScreenUpdating = False
Sheets(2).Select
Range(Cells(2, 1), Cells(Rows.Count, Columns.Count)).Clear
Sheets(3).Select
Range(Cells(2, 1), Cells(Rows.Count, Columns.Count)).Clear
Sheets(1).Select
FinalColumn = Cells(1, 1).End(xlToRight).Column
FinalRow = 1
For x = 1 To FinalColumn
    thisRow = Cells(Rows.Count, x).End(xlUp).Row
    If thisRow > FinalRow Then
        FinalRow = thisRow
    End If
Next x

For x = 2 To FinalRow
    IsCompleted = Cells(x, 1).ValueThen
    If IsCompleted = "Yes" Then
        Cells(x, 1).Resize(1, FinalColumn).Interior.ColorIndex = xlColorIndexNone
        Cells(x, 1).Resize(1, FinalColumn).Copy
        Sheets(2).Select
        NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
        Cells(NextRow, 1).Select
        ActiveSheet.Paste
        Sheets(1).Select
    ElseIf IsCompleted = "No" Then
        Cells(x, 1).Resize(1, FinalColumn).Interior.ColorIndex = 6
        Cells(x, 1).Resize(1, FinalColumn).Copy
        Sheets(3).Select
        NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
        Cells(NextRow, 1).Select
        ActiveSheet.Paste
        Sheets(1).Select
    Else
        Cells(x, 1).Value = "No"
        Cells(x, 1).Resize(1, FinalColumn).Interior.ColorIndex = 6
        Cells(x, 1).Resize(1, FinalColumn).Copy
        Sheets(3).Select
        NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
        Cells(NextRow, 1).Select
        ActiveSheet.Paste
        Sheets(1).Select
    End If
Next x
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub