2016-05-17 119 views
0

我有这个代码是从另一个工作表中提取值。 这些值是文本,但提取后的Excel将它们转换为编号不管是什么,即使设置为文本格式的列,即使使用保持excel从自动格式化字符串到VBA编号

Format(value , "#") 

的问题是,他们是大的数字,它是不断变化的任何数字高于15计数到0,我需要数字,因为它们是。

它们不在参考列切(J,3),所以它在此过程中改变

Dim i, col1, col2 As Integer 
i = 2 
col1 = 4 
col2 = 5 
    For j = 2 To 2205 
     If Cells(j, 3).Value <> "" Then 
      If Len(Cells(j, 3)) = 20 Then 
        Cells(i, col1).Value = Format(Cells(j, 3).Value, "#") 
        Cells(i, col2).Value = Cells(j, 3).Row 
        Cells(j, 3).Value = "" 
        i = i + 1 
      End If 
     End If 
    Next j 
    i = 2 
    col1 = col1 + 2 
    col2 = col2 + 2 

回答

1

Excel有的15 digits of precision的限制,所以在十进制的任一侧数超过15个数字将不会以所需的精度进行存储。在插入值之前,您是否尝试过设置单元格格式?设置值之前,请使用行Cells(i, col1).NumberFormat = "@"

Dim i, col1, col2 As Integer 
    i = 2 
    col1 = 4 
    col2 = 5 
     For j = 2 To 2205 
      If Cells(j, 3).Value <> "" Then 
       If Len(Cells(j, 3)) = 20 Then 
         Cells(i, col1).NumberFormat = "@" 
         Cells(i, col1).Value = Format(Cells(j, 3).Value, "#") 
         Cells(i, col2).Value = Cells(j, 3).Row 
         Cells(j, 3).Value = "" 
         i = i + 1 
       End If 
      End If 
     Next j 
     i = 2 
     col1 = col1 + 2 
     col2 = col2 + 2 
+0

作为一个补充说明:您仍然能够与值作为数字工作,如果你施放它们作为[十进制](http://www.techonthenet.com/excel/formulas/cdec.php )。例如:'CDec(Cells(i,col2).Value)+ 2'。数字的位数仍然有限制,但我无法找到有关限制的文档。但是,我能够通过将26位数字添加到另一个26位数字来进行测试。 – Tim

+1

@Tim,正确。 VBA与excel没有相同的限制...要在工作表中显示数字,您需要使用NumberFormat属性将文本格式设置为文本格式,但在VBA中,您可以将值转换为十六进制等,然后使用他们是精确的数字。这里是关于VBA数据类型及其局限性的参考。 http://www.quepublishing.com/articles/article.aspx?p=339929&seqNum=2 –

+1

不错!现在我知道限制是29位数字。 :D真的......谁需要超过29位数字?即使[NASA将PI降至15](http://www.jpl.nasa.gov/edu/news/2016/3/16/how-many-decimals-of-pi-do-we-really-need/ )...嗯...我想知道他们是否选择了15,因为他们使用Excel? xD – Tim