簡體   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