[英]If cell A1 is greater than B1, cut and paste row to first empty row
If cell in column I1-I14 is greater than cell in column J1-J14, I want to cut the entire row and paste values to the first empty row.如果 I1-I14 列中的单元格大于 J1-J14 列中的单元格,我想剪切整行并将值粘贴到第一个空行。 (From row 16 and down.)
(从第 16 行开始。)
If cell i is greater than cell j, cut row and paste values to first empty row (row 16 in this example)如果单元格 i 大于单元格 j,则剪切行并将值粘贴到第一个空行(本例中为第 16 行)
This code just pastes in the first row:此代码仅粘贴在第一行:
Sub Knapp6_Klicka()
Dim i As Long
Dim j As Long
j = 1
For i = 3 To 500
If Cells(i, 9).Value > Cells(i, 10).Value Then
Cells(i, 12).EntireRow.Cut Sheets("Blad1").Range("A" & j)
j = j + 1
End If
Next i
End Sub
I tried to combine the paste with two different solutions.我试图将糊状物与两种不同的解决方案结合起来。
One like this, where I recorded a macro and went to the last cell, then up to the first empty cell:像这样,我录制了一个宏并转到最后一个单元格,然后到第一个空单元格:
Range("A1048576").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
And one solution that I found on an Excel community:我在 Excel 社区找到了一个解决方案:
Sub compareresult()
Dim row1 As Integer
Dim row2 As Integer
row2 = 1
For row1 = 8 To 500
If sheet1.Cells(row1, 11).value > sheet1.Cells(row1, 9).value Then
sheet1.Cells(row1, 1).EntireRow.Copy Sheets(11).Cells(row2, 1)
row2 = row2 + 1
End If
Next row1
End Sub
If cell in column I1-I14 is greater than cell in column J1-J14, i want to cut entire row and paste values to the first empty row.
如果 I1-I14 列中的单元格大于 J1-J14 列中的单元格,我想剪切整行并将值粘贴到第一个空行。 (From row 16 and down)
(从第 16 行开始)
Here is a method which doesn't cut and paste in a loop.这是一种不会在循环中剪切和粘贴的方法。 Since you are not deleting the row or "cutting and inserting" the row, here is a simple approach.
由于您没有删除行或“剪切和插入”行,因此这是一种简单的方法。 The below code follows a basic logic
下面的代码遵循基本逻辑
Logic逻辑
Code代码
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim rngToCopy As Range
Dim i As Long
'~~> Change this to relevant sheet
Set ws = Sheet1
With ws
'~~> Loop and identify the range
For i = 2 To 14
If .Range("I" & i).Value2 > .Range("J" & i).Value2 Then
If rngToCopy Is Nothing Then
Set rngToCopy = .Rows(i)
Else
Set rngToCopy = Union(rngToCopy, .Rows(i))
End If
End If
Next i
'~~> If found then copy and clear
If Not rngToCopy Is Nothing Then
rngToCopy.Copy .Rows(16)
rngToCopy.Clear
End If
End With
End Sub
EDIT:编辑:
To incorporate new edits合并新的编辑
Works perfectly: Thanks.
完美运行:谢谢。 .) I failed to fully describe my problem.?
.) 我没有完全描述我的问题。? What i also need is to paste it as special (only paste the value and not the formulas).
我还需要将其粘贴为特殊(仅粘贴值而不是公式)。 Do you got any quick solution for that?
你有什么快速的解决方案吗? – Johl 5 hours ago
– 乔尔 5 小时前
Replace代替
rngToCopy.Copy .Rows(16)
to至
rngToCopy.Copy
DoEvents
.Rows(16).PasteSpecial Paste:=xlPasteValues
Have a try with this.试试这个。
It's based on the range you gave.它基于您提供的范围。 Skipped over row 1 since you have headers in it.
跳过第 1 行,因为其中有标题。
Dim i As Long, lRow As Long, ws As Worksheet
Set ws = Sheets("Blad1") 'Your sheet name
lRow = ws.Range("I" & Rows.Count).End(xlUp).Row + 1 'Finding the last row
If lRow < 16 Then lRow = 16 'The starting row you want to cut to
For i = 2 To 14 'Your range of rows to check
If ws.Range("I" & i) > ws.Range("J" & i) Then
ws.Range("I" & i).EntireRow.Cut ws.Range("A" & lRow) 'Cutting the whole row so you use column A to cut to
lRow = lRow + 1 'Move down 1 row for where to cut to
End If
Next i
Edit:编辑:
Because you only want the values to copy accross we can't use Cut
and PasteSpecial xlValues
so instead we will duplicate the value of the entire row to the new location, then clear the row (filling in for the cutting part).因为您只想复制值,所以我们不能使用
Cut
和PasteSpecial xlValues
,所以我们会将整行的值复制到新位置,然后清除该行(填充剪切部分)。 If clear is too much we can just ClearContents
to remove the values in the cells instead of the formatting if that happens.如果 clear 太多,我们可以只
ClearContents
来删除单元格中的值,而不是在发生这种情况时删除格式。 Make sure to always save before running VBA code for the first time.确保在第一次运行 VBA 代码之前始终保存。
Dim i As Long, lRow As Long, ws As Worksheet
Set ws = Sheets("Blad1") 'Your sheet name
lRow = ws.Range("I" & Rows.Count).End(xlUp).Row + 1 'Finding the last row
If lRow < 16 Then lRow = 16 'The starting row you want to cut to
For i = 2 To 14 'Your range of rows to check
If ws.Range("I" & i) > ws.Range("J" & i) Then
ws.Range("A" & lRow).EntireRow.Value = ws.Range("I" & i).EntireRow.Value 'Copying the values over
ws.Range("I" & i).EntireRow.Clear 'Clear the row
lRow = lRow + 1 'Move down 1 row for where to cut to
End If
Next i
Your code is doomed to failure because you do not take into consideration that you are cutting the found row.您的代码注定要失败,因为您没有考虑到您正在剪切找到的行。 Think about what that means.
想想这意味着什么。 Your row with the In,Out is row 15 and you wish to paste to row 16. If you cut row 5 (for example) then rows 15 and 16 will become rows 14 and 15. It also means that your next row (which you think will be row 6) will actually be what was row 7 before the cut.
您的 In,Out 行是第 15 行,您希望粘贴到第 16 行。如果您剪切第 5 行(例如),则第 15 和 16 行将变为第 14 和 15 行。这也意味着您的下一行(您认为将是第 6 行)实际上将是削减前的第 7 行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.