跳到主要内容

如何从Excel中的单元格中删除非数字字符?

假设您有一个包含大量此类数据的工作表 ABCD4578124YUIOH,而您只想删除非数字字符  阿布杜伊 但将数字字符保留在单元格中。 当然,您可以一一删除那些字符,但是在这里,您可以按如下所示快速去除单元格中的非数字字符:

使用VBA代码删除非数字字符

一键删除非数字字符的便捷工具


使用VBA代码删除非数字字符

要使用VBA代码删除某个范围内的非数字字符,请执行以下操作:

1. 点击 开发商 > Visual Basic中 打开 适用于应用程序的Microsoft Visual Basic 窗口。 在里面 适用于应用程序的Microsoft Visual Basic 窗口中,单击 插页 > 模块,然后将以下代码复制并粘贴到模块中:

VBA:删除所有非数字字符

Sub RemoveNotNum()
'Updateby Extendoffice
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    xOut = ""
    For i = 1 To Len(Rng.Value)
        xTemp = Mid(Rng.Value, i, 1)
        If xTemp Like "[0-9]" Then
            xStr = xTemp
        Else
            xStr = ""
        End If
        xOut = xOut & xStr
    Next i
    Rng.Value = xOut
Next
End Sub

2。 然后点击 按钮运行代码,在弹出 KutoolsForexcel. 对话框中,选择要删除的非数字字符的范围,然后单击 OK 按钮。 看截图:

doc删除非数字字符1个副本

3。 然后,将删除所选范围内的所有非数字字符。

doc删除非数字字符2

如果数字带有小数点,则可以使用以下VBA:

Sub RemoveNotNum()
'Updateby Extendoffice
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    xOut = ""
    For i = 1 To Len(Rng.Value)
        xTemp = Mid(Rng.Value, i, 1)
        If xTemp Like "[0-9.]" Then
            xStr = xTemp
        Else
            xStr = ""
        End If
        xOut = xOut & xStr
    Next i
    Rng.Value = xOut
Next
End Sub

您可以看到如下屏幕截图所示的结果:

doc删除非数字字符3


一键删除非数字字符的便捷工具

要删除范围内的非数字字符, Kutools for Excel删除字符 实用程序可以一键完成它。

Kutools for Excel : 带有300多个便捷的Excel加载项,可以在30天内免费试用. 

安装后 Kutools for Excel,请执行以下操作:

1。 突出显示您要删除非数字字符的范围。 请点击 库工具 > 文本 > 删除字符.

doc删除非数字字符4

2。 在 删除字符 对话框,检查 非数值 在选项 删除字符 部分,然后单击 OK or 使用 按钮。 并且非数字字符会立即从选择中删除。 看截图:

doc删除非数字字符5

点击下载Kutools for Excel并立即免费试用!

有关更多详细信息 删除字符 Kutools for Excel,请访问 删除字符功能描述。


演示:从单元格中删除数字,字母,不可打印或字母数字字符

Kutools for Excel:具有300多个方便的Excel加载项,可以在30天内免费试用,没有任何限制。 立即下载并免费试用!

相关文章:

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (13)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
HI Thanks for the coding, i am getting the syntax error highlighted on For i = 1 To Len(Rng.Value) and the debug Sub RemoveNotNum() is highlighted in yellow.. kindly advice.
This comment was minimized by the moderator on the site
The VBA works great. I modified it to always work on a column, as shown:

For Each Cell In Range("B2:B" & ActiveSheet.UsedRange.Rows.Count)

xOut = ""

For i = 1 To Len(Cell.Value)

xTemp = Mid(Cell.Value, i, 1)

If xTemp Like "[0-9]" Then

xStr = xTemp

Else

xStr = ""

End If

xOut = xOut & xStr

Next i

Cell.Value = xOut

Next

I also used this to sort a column by the last digit, changing the last part to Cell.Value = Right(xOut, 1) then sorting it numerically.
This comment was minimized by the moderator on the site
Send me it on my email id
This comment was minimized by the moderator on the site
Hi, I want to use this macro but want to pre-define a range. Can you please let me know how to pre-define the range. Thanks, Samit
This comment was minimized by the moderator on the site
Just wanted to let you know that if the string starts with zero(s), or starts with letters, followed by zero(s), followed by the rest of the string this will remove all of the beginning zeroes. This is weird because it doesn't remove the zero(s) if they are between other non-zero numbers within the string, only if they start the string or are the first numbers after the initial letters in a string. Example. 0060100 would come out as 60100 PFF057726 would come out as 57726. Let me know if you have an explanation for this and can think of a solution. Thank you.
This comment was minimized by the moderator on the site
Thanx Guys for such a nice work. The script is awesome and it worked for. Keep it up guys.
This comment was minimized by the moderator on the site
Use this Formaula for replacing the existing character with the new one =SUBSTITUTE(text,old_text,new_text,[instance_num]) for ex-=SUBSTITUTE(TRIM(G1),"/","") Wish U all Happy Easter 2015
This comment was minimized by the moderator on the site
I received an error when I hit the > run button in the developer: Compile Error: Expected:end of statement and the word "non" in non-numeric is highlighted
This comment was minimized by the moderator on the site
Wow. that worked , exactly what I needed. Thanks,
This comment was minimized by the moderator on the site
Awesome Worked GREAT!!!!!!!!
This comment was minimized by the moderator on the site
wouldn't be better to replace the input-box method by just setting WorkRng like this: Set WorkRng = Intersect(ActiveSheet.UsedRange, Selection) that way if user select a entire column it wouldn't generate any error.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations