vba - 检查字符串是否包含在另一个字符串中
问题描述
我的一根弦有问题,我很想得到一些帮助。通过迭代,我得到了那些 htmlelement innertex:
31-12-2017
31-12-2016
2015 年 31 月 12 日等
我希望在这些元素中搜索通过输入框插入的一个字符串,但该字符串仅包含年份(例如 2017 年)。我尝试使用 instr 和 instrrev 并且在这两种情况下,即使存在字符串也不匹配。
这是我的代码的一部分:
Dim setdata as String
Dim datacheck as mshtml.ihtmlelement
Dim datachecks as mshtml.ihtmlelementCollection
Setdata = application.inputbox 'etc.
Datachecks = iedoc.getelementbyclassname("example") 'it finds all the date in the page (I checked through debug.print)'
For each datacheck in datachecks:
If instr(setdata, datacheck.innertext, vbtextcompare) then 'i also tried instrrev'
Debug.print "ok"
Else
Debug.print "no"
End if
Next datacheck
即使通过输入框插入 2017 和元素 collection.contains 31-12-2017,我也总是没有。你能帮助我吗?
解决方案
你是不是混淆了顺序。对我来说,您的代码似乎正在“setdata”中寻找“datacheck”,而我的理解是您正在“datacheck”中寻找“setdata”。
Dim a as String 'The string to look for
Dim b as String 'The string to look in
Dim c as Long
a = "A"
b = "aabb AbAb BAAb"
c = InStr(b,a) 'Function looking for the first occurrence of a in b
Print.Debug c 'This will give the value of 6, the location "A" first appears in string b.
BR FNK
推荐阅读
- linux - Route53 over VPN IPSec 和 Openvpn
- python - 找到html元素bs4的孩子的最快方法
- vue.js - Vuex:具有动态命名空间的 createNamespacedHelpers
- html - 为什么 flex-basis 需要设定高度?
- apache-spark - 计算 StreamingKMeans 的成本
- microsoft-cognitive - 微软翻译自定义模型部署失败
- php - Phpunit 使用 zend 框架 3 和学说,模拟不起作用
- c# - 具有泛型返回类型的可空引用类型
- docker - 无法连接到 keycloak 管理面板
- javascript - 如何在 mat-select 中设置自动对焦?