简体   繁体   English

Excel VBA:获取单元格的值,独立于单元格的格式

[英]Excel VBA: get value of cell, independent of cell's formatting

In Excel VBA, Range("A1").Value should return the underlying value of the range A1 on the worksheet.在 Excel VBA 中, Range("A1").Value应返回工作表上范围 A1 的基础值。 But I'm getting a different value if the cell is formatted as Accounting.但如果单元格格式为会计,我会得到不同的值。

How do I get the actual underlying value of the cell?如何获得单元格的实际基础值?

Worksheet工作表

Make a new document, enter the following values in cells:创建一个新文档,在单元格中输入以下值:

  • A1: 0.00001 A1:0.00001
  • A2: =A1 A2:=A1
  • A3: =A1=A2 A3:=A1=A2

As you'd expect, A3 results in TRUE .如您所料,A3 结果为TRUE Now change the formatting of A2 to Accounting, using 2 decimal places.现在将 A2 的格式更改为会计,使用 2 个小数位。 A2 now reads $ 0.00 , but the underlying value is still 0.00001 , so A3 is still TRUE . A2 现在读数$ 0.00 ,但基础值仍为0.00001 ,因此 A3 仍为TRUE

VBA VBA

Make a new module and add in the following function:制作一个新的模块并添加以下 function:

Function f(addr As String)
    f = Range(addr).Value
End Function

As you can see, this just gets the value of a range using the Value method of the Range object.如您所见,这只是使用Range object 的Value方法获取范围的值。

Worksheet工作表

Back to the worksheet.回到工作表。 Enter the following values:输入以下值:

  • B1: =f("A1") B1: =f("A1")
  • B2: =f("A2") B2: =f("A2")
  • B3: =B1=B2 B3:=B1=B2

A1 and A2 have the same underlying value, but B1 and B2 don't, even though they're both calculated using the Value method of A1 and A2 . A1A2具有相同的基础值,但B1B2没有,即使它们都是使用A1A2Value方法计算的。

The expression in A3 ( =A1=A2 ) is accessing the actual underlying value of A1 and A2 . A3 ( =A1=A2 ) 中的表达式正在访问A1A2的实际基础值。 How do I access these values in VBA?如何访问 VBA 中的这些值?

It initally came up TRUE for me as well because I added the formatting after I entered the formulas.它最初对我来说也是正确的,因为我在输入公式后添加了格式。

To repro - re-edit B2.重现 - 重新编辑 B2。

To get the underlying value you need to use the VALUE2 property which seems to ignore the formatting:要获取基础值,您需要使用似乎忽略格式的 VALUE2 属性:

Function f(addr As String)
    f = Range(addr).Value2
End Function

The issue with using VBA and Value with currency formatted cells beyond 4 decimals points is covered well by this post at Dick's blog使用 VBA 和 Value 以及超过 4 个小数点的货币格式单元格的问题在 Dick 的博客上的这篇文章中得到了很好的介绍

If you type a number into an unformatted cell in Excel, that number is stored in the Double data type.如果您在 Excel 中的未格式化单元格中键入一个数字,该数字将以双精度数据类型存储。 When you format that number, you show it in a specific way, but you don't change the number.当您格式化该数字时,您会以特定方式显示它,但不会更改该数字。 For instance, if you format the number 1 as a Comma style, you get 1.00.例如,如果将数字 1 格式化为逗号样式,则会得到 1.00。 The underlying data is still a 1, you're just showing it differently.基础数据仍然是 1,您只是以不同的方式显示它。 The Date format and the Currency format are two exceptions to this rule.日期格式和货币格式是此规则的两个例外。

When you format something as a Date or Currency, you actually change the underlying data type (of the value stored in the Value property).当您将某些内容格式化为 Date 或 Currency 时,您实际上更改了基础数据类型(存储在 Value 属性中的值)。 When it comes to the Date format, this is semantics because you can switch between the Date and Double data types without any change to the data.对于 Date 格式,这是语义,因为您可以在 Date 和 Double 数据类型之间切换而无需对数据进行任何更改。 Not so much with the Currency data type.与 Currency 数据类型无关。 Currency only supports four decimal places , so jamming a Double with, say, five decimals into a Currency data type will result in a different number. Currency只支持四位小数,因此将 Double 与,比如说,五位小数插入 Currency 数据类型将导致不同的数字。

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

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