在VBA中选定的单元格上执行修剪功能 [英] Perform trim function on the selected cells in VBA

查看:161
本文介绍了在VBA中选定的单元格上执行修剪功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否有办法可以运行一个简单的功能(TRIM删除重复的空格字符)在一个Excel宏,所以它直接在单元格上执行,所以我不必创建一个



它应该在所选列选定的单元格上工作

  Sub trim()

ActiveCell.FormulaR1C1 == trim(R1C1)

End Sub


解决方案

  Sub TrimAndFit()
Dim r As Range

With Application.WorksheetFunction
对于每个r在相交(选择,ActiveSheet.UsedRange)
r.Value = .Trim(r.Value)
下一个r
结束
End Sub

这将适用于所有选择 ed单元格。我们使用 Intersect()来提高成效。


I am trying to figure out if there is a way I could run a simple function (TRIM to remove duplicate space characters) in an Excel macro so it is performed directly on the cell and so I don't have to create an extra column with the function that TRIMs the previous column.

It should work on selected column or just selected cells

Sub trim()

 ActiveCell.FormulaR1C1 = "=trim(R1C1)"

End Sub

解决方案

Give this a try:

Sub TrimAndFit()
    Dim r As Range

    With Application.WorksheetFunction
    For Each r In Intersect(Selection, ActiveSheet.UsedRange)
        r.Value = .Trim(r.Value)
    Next r
    End With
End Sub

This will work on all Selected cells. We use Intersect() to improve performance.

这篇关于在VBA中选定的单元格上执行修剪功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆