excel截断非常大的整数从HTML拉

我有一个VBA脚本,成功地从HTML中提取数据并将其转储到Excel表格中。 我的问题发生时,一些数据是一个非常大的整数(30位数字+数字)微软Excel存储这是默认的科学记数法,我遭受数据丢失。
所以数据存储在html这样的:

<tr> <td> 11111111111111111111111111 </td> </tr> 

所以我的脚本遍历行(tr),并为每个单元格(td)将值拉到我的工作表,如下所示:

 Set mydata = appIE.Document.getelementsbytagname("tr") x=1 For Each e In mydata Set mytd = e.getelementsbytagname("td") For Each c In mytd Cells(x, 2).Value2 = e.Cells(1).innerText x = x + 1 Next c Next e 

所以我相信错误发生是因为我使用.value2来存储数据在我的单元格,所以我尝试切换到.value,其中重现了错误,并没有运行的文本。 我怎样才能设置这个数据types长或正确的文本,而不会丢失数据?

在Excel VBA中能做的最好的方法是使用一个变体,并明确声明一个十进制types转换。 这样最多可以存储29位数字(我只用1来testing它,这与其他数字无关,但是在那里)。

你会想要做这样的事情:

 Sub Test() Dim InputString As String Dim DecimalHolder As Variant ' Put the string representation of your number into a string variable ' For my purposes, this was mostly to verify the input since the editor ' will convert this number to Scientific Notation otherwise. InputString = "11111111111111199999999999999" ' Using the variant variable, store the number by converting the string to a decimal. DecimalHolder = CDec(InputString) ' This now works, and is accurate DecimalHolder = DecimalHolder + 1 End Sub 

如果您的任何input数字超过29个字符,您将需要创build一个修剪到29个字符限制的方法。 如果在尝试将数字存储在DecimalHoldervariables中时不会出现溢出错误。

所有这些的原因是VBA授予每个数据types的字节数。 我们在这里使用变体,因为一个变体有16个字节可以使用(转换为十进制时减less到12),而doubletypes有8个,longtypes有4个,inttypes有2个。

如果最终你不需要数字,但是你需要一个数字的字符表示(如果这些“数字”正在服务于Id的function),那么你可以将该数字作为一个string来存储。 你将无法对string进行数字操作而不进行转换(尽pipe隐式转换可能只是做到了,而不是告诉你)。