繁体   English   中英

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

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

如果同时满足条件,那么如何使用循环移动紧靠其旁边的单元格,该如何移动单元格呢? 说条件是(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      |

我需要将第2列acct#移到它旁边右列之后的右列。 如何在loop和len中使用它? 有什么更好的方法? 谢谢,加油!

更新

 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

因此在M列中是存储Acct#的位置,其旁边的以下单元格我必须偏移,但是使用上面的代码,我只是删除了N列的值,而其右侧的单元格没有移动。

您可能要更改代码,例如以下代码(注释):

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