特定标准“大于50000”或“小于-50000”

时间:2018-11-28 18:33:37

标签: excel vba copy row

这是我到目前为止所拥有的。我想作一些修改,我不完全知道该怎么做;

  1. 在第3行上,我希望我的“复制条件”为“大于50000”或“小于50000”。

  2. 如何在Sheet2上指定将第一项复制到的单元格?例如,Sheet2! B10?

  3. 然后我如何将从工作表1中符合我的条件的行复制的列限制为(例如)工作表1中的A,B,E,F,H,I,O和AG列? / p>

    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(x1Up).Row

    For i = 2 To a

        If Worksheets("Sheet1").Cells(i, 3).Value = **>50000 OR <50000** Then

            Worksheets("Sheet1").Rows(i).Copy
            Worksheets("Sheet2").Activate
            b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(x1Up).Row
            Worksheets("Sheet2").Cells(b + 1, 1).Select
            ActivateSheet.Paste
            Worksheets("Sheet1").Activate

        End if

    Next

    Application.CutCopyMode = False
    ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select

End Sub

2 个答案:

答案 0 :(得分:2)

您可以使用Abs()函数并仅进行一次检查:

并使用Range对象的Worksheet属性通过Intersect()方法在给定行中选择所需列:

Option Explicit

Sub main()
    Dim a As Long, i As Long
    Dim sht2 As Worksheet

    Set sht2 = Worksheets("Sheet2") ' set a worksheet object for destination sheet

    With Worksheets("Sheet1") ' reference Sheet1
        a = .Cells(.Rows.Count, 1).End(xlUp).Row ' get referenced sheet column A row index of last not empty cell
        For i = 2 To a
            If Abs(.Cells(i, 3).Value) > 50000 Then ' if cell value in current row index and column 3 is greater than 50000 or less then -500000
                Intersect(.Rows(i), .Range("A:B , E:F, H:I, O:O, AG:AG")).Copy
                sht2.Cells(sht2.Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial
                Application.CutCopyMode = False
            End If
        Next
    End With
End Sub

答案 1 :(得分:0)

您使用的是x1Up而不是xlUp

Application.ScreenUpdating = False
Dim cell As Range
With Worksheets("Sheet1")
    For Each cell In .Range("A2", .Cells(Rows.Count, 1).End(xlUp)).Offset(0, 2)
        If cell.Value > -50000 Or cell.Value < 50000 Then
            With Worksheets("Sheet2")
                cell.EntireRow.Range("A1:B1,E1:F1,H1,I1,O1,AG1").Copy Destination:=.Cells(Rows.Count, 1).End(xlUp).Offset(1)
            End With
        End If
    Next
End With