2016-12-05 47 views
0

如果满足条件并遵循使用循环旁边的单元格,如何移动单元格? 说的标准是(LEN = 43)< - 科目#复制和粘贴多个单元格的VBA如果满足标准

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#到它的下面紧挨着它的列右侧立柱。如何使用循环和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是科目#的存储位置,紧邻它下面的细胞我有偏移,但与上面的代码,我有只是删除值是列N,紧挨着它的单元格不移动。

+1

Stack Overflow是不是我的代号网站。如果您的代码无法正常工作,我们将帮助解决特定问题。请将您当前的代码发布在原始文章中,并告诉我们它做错了什么。 –

+1

哦,是的。抱歉。以上是我的代码, – Anne

回答

0

你可能要改变像你的代码(注释)以下之一:

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 
+0

作品就像一个魅力!谢谢!!! – Anne

+0

不客气 – user3598756

相关问题