简体   繁体   English

如果符合条件,则VBA可复制和粘贴多个单元格

[英]VBA to Copy and Paste Multiple Cells If Meet Criteria

how to move cells if criteria is met following also the cells right next to it using loop? 如果同时满足条件,那么如何使用循环移动紧靠其旁边的单元格,该如何移动单元格呢? say the criteria is (Len=43) <- Acct# 说条件是(Len = 43)<-Acct#

Column#  |Date     |  Country   |  Acct#   |   Fruit     |   Price   |  Qty  
1        |11/02/16 |  India     |  5002xxx |   apple     |   $9      |   5  
2        |12/02/16 |  5001xxx   |  Orange  |   $8        |   10      |

I need to move the column 2 acct# to it's right column following the columns right next to it. 我需要将第2列acct#移到它旁边右列之后的右列。 how to use it with loop and len? 如何在loop和len中使用它? what is the better approach? 有什么更好的方法? Thanks and cheers! 谢谢,加油!

Update 更新

 Sub Findandcut2()
    Dim row As Long

    For row = 2 To 1000

        If Range("M" & row).Value Like "*5027.1227000.0000.0000.000.0000.0000.0000*" Then

            Range("N" & row).Value = Range("M" & row).Value
            Range("M" & row).Value = ""
        End If
    Next

End Sub

so in Column M is where the Acct# is stored and the following cells right next to it I have to offset but with the code above I've got is just deleting the value is Column N and the cells right next to it is not moving. 因此在M列中是存储Acct#的位置,其旁边的以下单元格我必须偏移,但是使用上面的代码,我只是删除了N列的值,而其右侧的单元格没有移动。

you may want to change your code like the (commented) following one: 您可能要更改代码,例如以下代码(注释):

Sub Findandcut2()
    Dim cell As Range

    For Each cell In Range("M2", Cells(Rows.Count, "M").End(xlUp)) '<--| loop through all column "M" cell from row 2 down to last not empty row
        If cell.Value Like "*5027.1227000.0000.0000.000.0000.0000.0000*" Then
            With Range(cell, cell.End(xlToRight)) '<--| reference the range from current column M cell rightwards till the one preceeding first not empty one
                .Offset(, 1).Value = .Value '<--| copy values
            End With
            cell.ClearContents '<--| need to clear the cell you started offset values from?
        End If

    Next cell
End Sub

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM