Excelmacros将与expression式单元格匹配的单元格移动到右侧

我想在Excel 2011中将所有与特定searchstring匹配的单元格移动到右侧,但我是一个完全的Excel noob。

例如,searchstring“A”:

AAA _ _ BAA _ 

成:

 _ AAA _ B _ AA 

不:

 _ A _ A _ B _ A 

谢谢!

 Sub MoveRight() Dim rFound As Range Dim lFirstCol As Long Dim rSearch As Range Const sSEARCH As String = "A" Set rSearch = Sheet1.Cells 'Find the first instance in the right most column Set rFound = rSearch.Find(sSEARCH, rSearch.Cells(1), xlValues, xlWhole, xlByColumns, xlPrevious, True) 'If a cell was found If Not rFound Is Nothing Then 'Record the column so we know when to stop lFirstCol = rFound.Column 'loop through all the found cells Do rFound.Offset(0, 1).Value = rFound.Value rFound.ClearContents Set rFound = rSearch.FindPrevious(rFound) 'stop when it wraps back around and finds the first 'instance that you moved one cell to the right Loop Until rFound.Column > lFirstCol End If End Sub 

确保在运行此macros之前保存文件的备份。 如果它没有做你想做的事情,那就没有撤销。