我需要删除已选定的特定单元格范围的最后 7 个字符

I need to delete the last 7 characters of a specific range of cells that are already selected

我正在尝试删除最后 7 个字符,这些字符在我 运行 的报告中是乱码,但仅适用于我在电子表格中选择的单元格。不过,我一直收到 运行-Time Error 13。

Sub Mitts()

With Selection
    .Value = Left(.Value, Len(.Value) - 7)

End With
End Sub

谁能帮我找出问题所在?

如果选择范围内有多个单元格,.Value returns 一个数组,而不是单个值。解决这个问题的最佳方法是遍历选择中的所有单元格:

Sub Mitts()

    Dim target As Range
    For Each target In Selection
        With target
            If Len(.Value) > 7 Then
                .Value = Left$(.Value, Len(.Value) - 7)
            End If
        End With
    Next target

End Sub

请注意,您还应该在尝试截断长度之前测试长度,以避免 'Invalid procedure call or argument' 空单元格或没有足够文本可截断的单元格出现异常。