简体   繁体   English

Excel Vba - 如果单元格中的值大于,则复制行

[英]Excel Vba - Copy row if value in cell is greater than

I've got this table full of data. 我有这个表充满了数据。 And column K in each row contains a number. 并且每行中的列K包含一个数字。 So basically what I'm trying to do is move that entire row, if the data in that column is greater than 9, over to sheet2. 所以基本上我要做的就是将整行(如果该列中的数据大于9)移到sheet2。

How can this be achieved? 怎么能实现这一目标? I've already created actual tables in the sheets, called Table1 and Table2. 我已经在工作表中创建了实际的表,称为Table1和Table2。

This is what I've managed to put together so far. 到目前为止,这是我设法组建的。 I've looked at autofilter, but I can't understand squat of what's happening in there. 我看过自动过滤器,但是我无法理解那里发生了什么。 So this I get! 所以我得到了!

Sub MoveData()

    Dim i As Range
    Dim num As Integer
     num = 1
    For Each i In Range("K10:K1000")
        If i.Value > 9 Then
            i.Select
            ActiveCell.Rows("1:1").EntireRow.Select
            Selection.Copy

            Sheets("Sheet2").Range("A65000").End(xlUp).Offset(num, 0).PasteSpecial
            ActiveCell.Rows.Delete
            num = num + 1

        End If
    Next i
End Sub

This kinda works so far. 到目前为止,这种方式有效。 But I can't manage to paste the row to the next blank row in sheet2. 但我无法将行粘贴到sheet2中的下一个空行。 I tried doing that num = num + 1 thing, but I guess that's way off? 我尝试做那个num = num + 1的事情,但我想这样的事情已经过去了?

Is this what you are trying? 这是你在尝试什么? ( TRIED AND TESTED ) 经过试验和测试

Option Explicit

Sub Sample()
    Dim wsI As Worksheet, wsO As Worksheet
    Dim rRange As Range

    Dim lastRowWsO As Long

    Set wsI = Sheets("sheet1")

    '~~> Assuming that the Header is in K10
    Set rRange = wsI.Range("K10:K1000")

    Set wsO = Sheets("sheet2")

    '~~> Get next empty cell in Sheet2
    lastRowWsO = wsO.Range("A" & Rows.Count).End(xlUp).Row + 1

    With wsI
        '~~> Remove Auto Filter if any
        .AutoFilterMode = False

        With rRange
            '~~> Set the Filter
            .AutoFilter Field:=1, Criteria1:=">=9"

            '~~> Temporarirly hide the unwanted rows
            wsI.Rows("1:9").EntireRow.Hidden = True
            wsI.Rows("1001:" & Rows.Count).EntireRow.Hidden = True

            '~~> Copy the Filtered rows
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Copy _
            wsO.Rows(lastRowWsO)

            '~~> Delete The filtered rows
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        '~~> Unhide the rows
        .Rows("1:9").EntireRow.Hidden = False
        .Rows("1001:" & Rows.Count).EntireRow.Hidden = False

        '~~> Remove Auto Filter
        .AutoFilterMode = False
    End With
End Sub

NOTE : I have not included any error handling. 注意 :我没有包含任何错误处理。 I would recommend you to include one in the final code 我建议你在最终代码中加入一个

FOLLOWUP 跟进

Sub Sample()
    Dim wsI As Worksheet, wsO As Worksheet
    Dim rRange As Range

    Dim lastRowWsI As Long, lastRowWsO As Long

    Set wsI = Sheets("Risikoanalyse")

    '~~> Assuming that the Header is in K10
    Set rRange = wsI.Range("K9:K1000")

    lastRowWsI = wsI.Cells.Find(What:="*", _
                After:=wsI.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Row


    Set wsO = Sheets("SJA utarbeides")

    '~~> Get next empty cell in Sheet2
    lastRowWsO = wsO.Cells.Find(What:="*", _
                After:=wsO.Range("A1"), _
                Lookat:=xlPart, _
                LookIn:=xlFormulas, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, _
                MatchCase:=False).Row + 1

    With wsI
        With .ListObjects("TableRisikoAnalyse")
            '~~> Set the Filter
            .Range.AutoFilter Field:=11, Criteria1:=">=9"

            '~~> Temporarirly hide the unwanted rows
            wsI.Rows("1:8").EntireRow.Hidden = True
            wsI.Rows(lastRowWsI & ":" & Rows.Count).EntireRow.Hidden = True

            '~~> Copy the Filtered rows
            wsI.Range(Replace(wsI.Range("K9").Offset(1, 0).SpecialCells(xlCellTypeVisible).Address, "$9:$9,", "")).EntireRow.Copy _
            wsO.Rows(lastRowWsO)

            '~~> Clear The filtered rows
            wsI.Range(Replace(wsI.Range("K9").Offset(1, 0).SpecialCells(xlCellTypeVisible).Address, "$9:$9,", "")).Clear

            .Range.AutoFilter Field:=11

            '~~> Sort the table so that blank cells are pushed down                
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=Range("TableRisikoAnalyse[[ ]]"), SortOn:=xlSortOnValues, Order _
            :=xlAscending, DataOption:=xlSortTextAsNumbers
            With .Sort
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
        End With

        '~~> Unhide the rows
        .Rows("1:8").EntireRow.Hidden = False
        .Rows(lastRowWsI & ":" & Rows.Count).EntireRow.Hidden = False

        '~~> Remove Auto Filter
        .AutoFilterMode = False
    End With
End Sub

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

相关问题 Excel VBA-将单元格值(如果为空)复制到同一行中的另一个单元格 - Excel VBA - Copy Cell Value, if empty, to another Cell in the same row Excel VBA-基于单元格值的格式(大于,小于,等于) - Excel VBA - Format based on cell value (Greater than, less than, equal to) 在Excel VBA列中搜索大于1的值 - Searching for greater than 1 value in a column Excel VBA 我希望vba在一个矩阵中找到一个大于0的值并将其复制到背景为黄色的单元格中的另一个矩阵中 - I want vba to find a value greater than 0 in one matrix and copy it into another matrix in the cell that has a yellow background 如果> 0,则Excel VBA将单元格值复制到左侧的单元格 - Excel VBA to copy cell value if > 0 to cell to the left EXCEL VBA-遍历一行中的单元格,如果不为空,则将单元格值复制到另一行,直到下一行不为空 - EXCEL VBA - Loop through cells in a row, if not empty, copy cell value into another row until next row not empty Excel VBA-如果时间大于24小时,则突出显示单元格 - Excel VBA - Highlight cell if time is greater than 24 hours ago Excel VBA:将单元格值复制到cmd - Excel VBA: Copy cell value to cmd 查找大于零的单元格值并复制到另一个工作表 - Find cell value greater than zero and copy to another worksheet Excel VBA:以单元格中的特定数字格式复制行单元格 - Excel VBA: copy row cell on specific number format in cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM