【问题标题】:I need to delete the last 7 characters of a specific range of cells that are already selected我需要删除已选择的特定单元格范围的最后 7 个字符
【发布时间】:2015-07-02 21:19:41
【问题描述】:

我正在尝试删除最后 7 个字符,这些字符在我运行的报告中是乱码,但仅限于我在电子表格中选择的单元格。我不断收到运行时错误 13。

Sub Mitts()

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

End With
End Sub

谁能帮我找出问题所在?

【问题讨论】:

  • 欢迎来到 Stackoverflow! For Each i in ActiveWorksheet.Selection (New line) i.Value = Left(i.Value, Len(i.Value) - 7) (New line) Next i -- 用新行替换 "(new line") ;-)
  • 我应该在哪里添加?
  • 这在SubEnd Sub 行之间。删除其余部分。

标签: vba selection


【解决方案1】:

如果选择区域中有多个单元格,.Value 返回一个数组,而不是单个值。解决此问题的最佳方法是遍历选择中的所有单元格:

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

请注意,您还应该在尝试截断之前测试长度,以避免在空单元格或没有足够文本可截断的单元格上出现“无效的过程调用或参数”异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多