【发布时间】:2011-01-27 06:26:53
【问题描述】:
我想在 Excel 2011 中将与特定搜索字符串匹配的所有单元格向右移动一个单元格,但我完全是 Excel 菜鸟。
例如,使用搜索字符串“A”:
A A A _ _ B A A _
进入:
_ A A A _ B _ A A
不是:
_ A _ A _ B _ A
谢谢!
【问题讨论】:
我想在 Excel 2011 中将与特定搜索字符串匹配的所有单元格向右移动一个单元格,但我完全是 Excel 菜鸟。
例如,使用搜索字符串“A”:
A A A _ _ B A A _
进入:
_ A A A _ B _ A A
不是:
_ 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
确保在运行此宏之前保存文件的备份。如果它没有执行您想要的操作,则无法撤消。
【讨论】: