My problem:
Cell A1: value =12 and the number property is general. Cell A2: value =12 and the number property is number with 2 decimal points, so the number is shown as "12.00"
How can I validate if both cells have the same number property, either in VBA
using macros or in any other way?
You can compare their NumberFormat
property:
If Range("A1").NumberFormat = Range("A2").NumberFormat Then
However, There are a number of different formats, including custom formats, that can be applied to cells that would make them look the same, without them having exactly the same NumberFormat value.
This means that comparing formats will never be as reliable as comparing the values. (It is also unusual to need to compare formats.)
You can use the NumberFormat
method.
Using this code, with 12
in A1 (formatted as general) and 12
in B1 (formatted as 12.00)
Option Explicit
Sub Stuff()
MsgBox Range("A1").NumberFormat
MsgBox Range("B1").NumberFormat
End Sub
Will return messages of "General" and "0.00" respectively.
So using
If Range("A1").NumberFormat = Range("B1").NumberFormat Then
-- do stuff here
End If
Will tell you if they are equal to one another or not.
It sounds like you are really concerned with the values of the cells. The .NumberFormat
property simply masks how the value appears, visually, to the user of the spreadsheet.
So you can easily do an equivalence test on the two values , like:
Sub Test()
MsgBox [A1].Value = [A2].Value
End Sub
Or you could do this in the worksheet formula:
=A1=A2
That formula should return either True
or False
, and should ignore the cell's formatting
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.