简体   繁体   English

如果单元格 A1 大于 B1,则将行剪切并粘贴到第一个空行

[英]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逻辑

  1. Loop and identify the range.循环并确定范围。
  2. If found, then copy the range in 1 go.如果找到,则复制 1 go 中的范围。
  3. Finally clear the range which was copied (if copied).最后清除被复制的范围(如果被复制)。

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).因为您只想复制值,所以我们不能使用CutPasteSpecial 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.

相关问题 当多个单元格大于单元格(例如A1)中指示的值时,将其格式化为一行(例如B1至H1)? - Format multiple cells in a row (e.g. B1 to H1) when they are greater than a value indicated in a cell (e.g. A1)? 如果B1为空,则删除A1单元格 - Delete A1 cell if B1 is empty 如果A1为空白,则将B1中的文本移到下一行 - If A1 is blank move text in B1 to next row 将值从列B剪切并粘贴到下一个空行列A - Cut and Paste value from Column B to next empty row Column A 将“ A1,A2,A3 ..”添加到“ B1,B2,B3 ..”,然后行“ A”将值重置为零 - Adding “A1,A2,A3..” to “B1,B2,B3..” Then Row “A” resets value to Zero 如果A1 = A2,并且B1&gt; B2,则隐藏第2行。要使用哪些公式/规则以及在哪里? - If A1 = A2, and B1>B2 then hide Row 2. What formulas/rules to use and where? 检查B列中的下一个空行,并将字符串粘贴到相应的A单元格中 - Check for next empty row in column B and paste string into corresponding A Cell 如果单元格B1为“ x”,则拉出单元格A1,否则忽略 - If Cell B1 is “x”, pull Cell A1, otherwise ignore VBA根据单元格值将行剪切并粘贴到另一张工作表,并删除空行 - VBA cut and paste row to another sheet based on cell value and delete empty row 复制并粘贴到另一个工作表的第一个空白行,如果第一个单元格为空,则粘贴到上一行 - Copy and paste to another sheet first empty row, pastes over the previous row if first cell is empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM