简体   繁体   English

检查字符串是否包含某些没有 CStr 转换的数字 - Excel VBA

[英]Check if String contains certain Number without CStr conversion - Excel VBA

Is it possible to check if a number exists in a string using VBA?是否可以使用 VBA 检查字符串中是否存在数字? Say I have a number - 137785973201908000000000 - and it is in a cell formatted as Number, how do I convert it to a string to use the InStr function?假设我有一个数字 - 137785973201908000000000 - 它位于格式为数字的单元格中,如何将其转换为字符串以使用InStr函数?

At the moment, if I use CStr(number) it will convert it to - 1.37785973201908E+23 - which is not ideal.目前,如果我使用CStr(number)它会将其转换为 - 1.37785973201908E+23 - 这并不理想。 The string I am trying to find the number in looks like this - [137785973201908000000000] Product Shipped.我试图在其中查找数字的字符串如下所示 - [137785973201908000000000] 产品已发货。

When using If InStr(stringVar, CStr(numberVar)) > 0 Then it will not find the number because it is converting it to the scientific notation when converting to text.当使用If InStr(stringVar, CStr(numberVar)) > 0 Then它不会找到数字,因为它在转换为文本时将其转换为科学记数法。

Is there VBA coding that can stop this?是否有 VBA 编码可以阻止这种情况?

尝试使用Format$

If InStr(stringVar, Format$(numberVar, "0")) > 0

Use CDec .使用CDec Is this what you want?这是你想要的吗?

Debug.Print CDec(numbervar)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM