如何使用VBA从整个Excel列中删除空格? [英] How to remove spaces from an entire Excel column using VBA?

查看:259
本文介绍了如何使用VBA从整个Excel列中删除空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除在"A"列的每个字段中具有的空格,该空格位于字符串的末尾,某些字符串具有3个空格,其他4个.在运行代码时,我不知道没有错误,所以我认为我有一个错误,因为运行时什么也没发生.

I'm trying to delete the spaces that I have in each field of the column "A" this spaces are at the end of the string some of the string has 3 spaces others 4. When I run the code, I don't have error, so I think I have a bug because nothing happened when is running.

Dim result As String
Last = Cells(Rows.Count, "A").End(xlUp).Row
    For i = Last To 1 Step -1
      If (Right(Cells(i, "A").Value, 4)) Like "    " Or (Right(Cells(i, "A").Value, 3)) Like "   " Then
        result = Replace(Cells(i, "A"), " ", "")
    End If
Next i

推荐答案

当前,您实际上并没有在更新循环中的单元格;

Currently you are not actually updating the cell in the loop, you just;

result = Replace(Cells(i, "A"), " ", "")

您应该:

Cells(i, "A") = Replace(Cells(i, "A"), " ", "")

更好

Cells(i, "A") = rtrim$(Cells(i, "A"))

这将删除所有正确的空格.您可能也可以删除if检查.

Which will remove all right spaces. You can probably remove the if check as well.

这篇关于如何使用VBA从整个Excel列中删除空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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