vba - 格式化字符串中的数字而不在 Excel 中提取它们
问题描述
所以这是我的问题:我想格式化在字符串中找到的数字。例如:
输入:'123.64 ABC'
结果:'124 ABC'
或者
输入:'10.23% ABC'
结果:'10% ABC'
我试过谷歌,但似乎所有问题都是关于提取数字,而不是在字符串中格式化它们。我猜 Excel 格式化工具的输入需要数字类型,因此它们不适用于包含字符串的单元格。那么这可能吗?我正在考虑拆分字符串->格式化数字->然后写回字符串。有人可以为此提出解决方案吗?谢谢你。
解决方案
你可以从这个开始
Sub teste()
txt = "123.64 ABC"
pos = InStr(1, txt, " ", vbTextCompare) 'search for space
num = Left(txt, pos - 1) 'get number until space
'in my case i have to replace a "." by "," to be a number, but you have to test what happens in your case
num = Replace(num, ".", ",", 1, 1)
numR = Round(num) ' round number
Final = numR & " " & Mid(txt, pos + 1, 500) ' join round number and rest of string
End Sub
祝你好运
推荐阅读
- javascript - 向 Javascript 事件函数添加更多参数
- arrays - 将包含字符串键的 JSON 文件解码为 [(Date,Double)]
- oracle - Oracle 使用 UTL_RAW.cast_to_raw 插入 RAW
- javascript - JS 文件中的 TS 警告:“类型 X 上不存在属性 X”:是否可以编写更干净的 JavaScript?
- c - C 中的这两个代码块看起来非常相似,但具有不同的效果。造成差异的原因是什么?
- php - wp 一体式迁移:将上传文件大小限制增加到 +100MB
- r - 修改对象时分配的变量正在改变 - ggplot
- java - 如何在后台向导中配置自定义通知消息
- discord.js - 我怎样才能运行这个命令 X 次(!command [X times])?不和谐机器人,discord.js
- r - R:使用两个 id 列和 value 列创建矩阵