[英]Copy rows to bottom of sheet based on cell value and sorted ascending wise
I have an Excel sheet which is pulled from JIRA.我有一张从 JIRA 中提取的 Excel 表。 This sheet has variable rows each week.
该工作表每周都有不同的行。 Once it is pulled, I have a macro which performs various actions.
一旦它被拉出,我就有了一个执行各种动作的宏。 One of these is to move certain rows to bottom of the sheet based on a value present in Column 'F'.
其中之一是根据“F”列中存在的值将某些行移动到工作表底部。 In this particular case, if the value 'RCR' is present in column 'F', then that particular row should cut and paste at the bottom.
在这种特殊情况下,如果值“RCR”出现在“F”列中,则该特定行应剪切并粘贴在底部。
For this I have written the below code.为此,我编写了以下代码。 This code works well and does the job.
此代码运行良好并且可以完成工作。 But the issue is since it loops from bottom to top, the list of rows with 'RCR' values is in a descending manner.
但问题是因为它是从下到上循环的,所以具有“RCR”值的行列表是按降序排列的。 But I want the rows to be sorted in an ascending manner.
但我希望以升序方式对行进行排序。
If I use "1 to lastRowOne" in the For loop, then what happens is the row gets deleted after the move has been done, due to this, if the next row also has the value of 'RCR', that particular row is skipped because it takes the place of the deleted row.如果我在 For 循环中使用“1 to lastRowOne”,那么在移动完成后该行会被删除,因此,如果下一行也具有“RCR”的值,则跳过该特定行因为它取代了已删除的行。 So the macro moves to the row after that, which is causing the macro to miss certains rows having consecutive value = '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
Is there a way that this can be remedied?有没有办法可以解决这个问题?
Use Union()
to make a non-contiguous range, copy that range and then delete afterwards.使用
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.