vba - How to remove text before a certain character?
问题描述
Let's say I have the following values:
1 min 7 sec
23 sec
6 hours 10 min 14 sec
I want to return:
7 sec
23 sec
14 sec
Is there a way to do this in VBA?
Sec, Min, or Hours may not always be present if a value for it is not used.
I.e. if there are 60 seconds, it will just say 1 min. If there are 60 minutes exactly, it will just say 1 hour.
解决方案
这是一种使用正则表达式的方法。它查找数字空间秒,如果找不到则返回连字符。修改以适应。
修改代码以纳入@Mathieu Guindon 的建议。+
寻找一个或多个实例,*
寻找零个或多个。
Function Regex1(v As Variant) As String
With CreateObject("vbscript.regexp")
.Global = True
.Pattern = "\d+\s*sec"
If .Test(v) Then
Regex1 = .Execute(v)(0)
Else
Regex1 = "-"
End If
End With
End Function
推荐阅读
- java - E/BluetoothAdapter:蓝牙绑定器为空
- symfony - Symfony4正确处理参数配置设置的方法
- c# - 使用 Ajax Post 调用 Web API 时是否可以附加证书?
- css - Webpack 不会从 node_modules 的 vue 组件中提取 css 到通用文件
- javascript - 如何正确设置 Google API 凭据和地理编码限制
- android-espresso - 连续调用 MockWebServer 进行测试
- loops - 使用 do loop-SAS 在数据步骤中定义一个带有变量的过滤器
- sql-server - TSQL - 在输出 AUTO、TYPE、ELEMENTS 时向 XML 添加属性
- python-3.x - 从 pyinstall --onedir 程序创建安装程序
- git - 克隆存储库,然后从原始更改中更新