【问题标题】:Excel macro to move cells matching expression one cell to the rightExcel宏将匹配表达式的单元格向右移动一个单元格
【发布时间】: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 vba


    【解决方案1】:
    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
    

    确保在运行此宏之前保存文件的备份。如果它没有执行您想要的操作,则无法撤消。

    【讨论】:

    • 一段漂亮优雅的代码,比我要写的要干净得多。我喜欢使用“查找”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多