簡體   English   中英

根據單元格值將行復制到工作表底部並按升序排序

[英]Copy rows to bottom of sheet based on cell value and sorted ascending wise

我有一張從 JIRA 中提取的 Excel 表。 該工作表每周都有不同的行。 一旦它被拉出,我就有了一個執行各種動作的宏。 其中之一是根據“F”列中存在的值將某些行移動到工作表底部。 在這種特殊情況下,如果值“RCR”出現在“F”列中,則該特定行應剪切並粘貼在底部。

為此,我編寫了以下代碼。 此代碼運行良好並且可以完成工作。 但問題是因為它是從下到上循環的,所以具有“RCR”值的行列表是按降序排列的。 但我希望以升序方式對行進行排序。

如果我在 For 循環中使用“1 to lastRowOne”,那么在移動完成后該行會被刪除,因此,如果下一行也具有“RCR”的值,則跳過該特定行因為它取代了已刪除的行。 所以宏移動到之后的行,這導致宏錯過某些具有連續值 = 'RCR' 的行。

Dim wsOne As Worksheet
Dim lastRowOne As Long
Dim lastRowTwo As Long

Set wsOne = ActiveWorkbook.Sheets("Status")

lastRowOne = wsOne.Cells(wsOne.Rows.Count, 1).End(xlUp).Row
lastRowTwo = wsOne.Cells(wsOne.Rows.Count, 1).End(xlUp).Row + 1

For I = lastRowOne To 1 Step -1
    If wsOne.Range("F" & I).Value = "RCR" Then
        wsOne.Rows(lastRowTwo).Value = wsOne.Rows(I).Value
        wsOne.Rows(I).EntireRow.Delete
    End If
Next

有沒有辦法可以解決這個問題?

使用Union()制作一個不連續的范圍,復制該范圍,然后刪除。

    Dim wsOne As Worksheet
    Dim lastRowOne As Long
    Dim i As Long
    Dim rng As Range
    
    Set wsOne = ActiveWorkbook.Sheets("Status")
    
    lastRowOne = wsOne.Cells(wsOne.Rows.Count, 1).End(xlUp).Row
    With wsOne
        For i = 1 To lastRowOne
            If wsOne.Range("F" & i).Value = "RCR" Then
                If rng Is Nothing Then
                    Set rng = .Rows(i)
                Else
                    Set rng = Union(rng, .Rows(i))
                End If
            End If
        Next
        rng.Copy .Range("A" & lastRowOne + 1)
        rng.Delete
    End With

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM