繁体   English   中英

如果某些列在值之间,则将整个行复制到 Excel 中的新工作表

[英]Copy enitire row to new sheet in excel if certain column is between values

我有一个包含数据的巨大 excel 文件,我想创建一个新工作表,其中仅包含其中一列(在我的示例中为“日期”列)在值 5 到 10 之间的行。

例如,

我有这样的数据:

Date  m  X   Y

4    30  5   7

7    31  5   7

7    32  5   7

9    33  5   7

10   34  5   7

2    35  5   7

我想制作这样的新表:

Date  m  X   Y

7    31  5   7

7    32  5   7

9    33  5   7

任何线索?

怎么样是这样的:

Sub test()
Dim lastRow As Integer, newLastRow As Integer, lastCol As Integer
Dim ws As Worksheet, newWS As Worksheet

Set ws = ActiveSheet
Set newWS = Sheets.Add
newWS.Name = "Parsed Info"

Dim cel As Range, rng As Range

newLastRow = 1

With ws
    lastRow = .Cells(1, 1).End(xlDown).Row
    For Each cel In .Range(.Cells(1, 1), .Cells(lastRow, 1))
        If 10 >= cel.Value And cel.Value >= 5 Then
            lastCol = .Cells(cel.Row, 1).End(xlToRight).Column
            Set rng = .Range(.Cells(cel.Row, 1), .Cells(cel.Row, lastCol))
            rng.Copy
            With newWS
                .Range(.Cells(newLastRow, 1), .Cells(newLastRow, lastCol)).PasteSpecial
            End With
            newLastRow = newLastRow + 1
        End If
    Next cel
End With

Application.CutCopyMode = False
End Sub

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM