更改所有单元格中文本的函数

Function To Alter Text In All Cells

我想扫描 X 列和文本长度大于 11 的任何单元格 - 我想弹出一个消息框,要求用户更改它。一旦用户点击确定按钮,我就想替换 X 列中的所有这些值。这是我的语法,但它不会迭代每个单元格。一旦我输入一个新的重命名值,宏将继续询问我,但它似乎不会迭代 X 列下方的单元格。

我必须更改什么才能让这个宏功能如我所愿?

Function ShortenText()
Dim c As Range
Dim lRow As Long
Dim NewValue As String

    lRow = Cells(Rows.Count, 24).End(xlUp).Row

    For Each c In Range("X1:X" & lRow)
        Cells(c.Row, 2) = Len(c)
        If Len(c) > 11 Then
            NewValue = Trim(InputBox("What would you like to rename the value")
            Selection.Replace What:=c.Value, Replacement:=NewValue, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
      End If
    Next c

End Function

我试过你的代码。只做一点修改:

Function ShortenText()
Dim c As Range
Dim lRow As Long
Dim NewValue As String

    lRow = Cells(Rows.Count, 24).End(xlUp).Row

    For Each c In Range("X1:X" & lRow)
        Cells(c.Row, 2) = Len(c)
        If Len(c) > 11 Then
            NewValue = Trim(InputBox("What would you like to rename the value"))
            c.Select
            Selection.Replace What:=c.Value, Replacement:=NewValue, LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
      End If
    Next c

End Function

添加"c.Select"并关闭Trim函数“)”。