What is the difference between .text, .value, and .value2? - excel

What is the difference between .text, .value, and .value2? Such as when should target.text, target.value, and target.value2 be used?

.Text gives you a string representing what is displayed on the screen for the cell. Using .Text is usually a bad idea because you could get ####
.Value2 gives you the underlying value of the cell (could be empty, string, error, number (double) or boolean)
.Value gives you the same as .Value2 except if the cell was formatted as currency or date it gives you a VBA currency (which may truncate decimal places) or VBA date.
Using .Value or .Text is usually a bad idea because you may not get the real value from the cell, and they are slower than .Value2
For a more extensive discussion see my Text vs Value vs Value2

In addition to the answer from Bathsheba and the MSDN information for:
.Value
.Value2
.Text
you could analyze the following tables for better understanding of differences between these three properties.

target.Value will give you a Variant type
target.Value2 will give you a Variant type as well but a Date is coerced to a Double
target.Text attempts to coerce to a String and will fail if the underlying Variant is not coercable to a String type
The safest thing to do is something like
Dim v As Variant
v = target.Value 'but if you don't want to handle date types use Value2
And check the type of the variant using VBA.VarType(v) before you attempt an explicit coercion.

Regarding conventions in C#. Let's say you're reading a cell that contains a date, e.g. 2014-10-22.
When using:
.Text, you'll get the formatted representation of the date, as seen in the workbook on-screen:
2014-10-22. This property's type is always string but may not always return a satisfactory result.
.Value, the compiler attempts to convert the date into a DateTime object: {2014-10-22 00:00:00} Most probably only useful when reading dates.
.Value2, gives you the real, underlying value of the cell. In the case for dates, it's a date serial: 41934. This property can have a different type depending on the contents of the cell. For date serials though, the type is double.
So you can retrieve and store the value of a cell in either dynamic, var or object but note that the value will always have some sort of innate type that you will have to act upon.
dynamic x = ws.get_Range("A1").Value2;
object y = ws.get_Range("A1").Value2;
var z = ws.get_Range("A1").Value2;
double d = ws.get_Range("A1").Value2; // Value of a serial is always a double

.Text is the formatted cell's displayed value; .Value is the value of the cell possibly augmented with date or currency indicators; .Value2 is the raw underlying value stripped of any extraneous information.
range("A1") = Date
range("A1").numberformat = "yyyy-mm-dd"
debug.print range("A1").text
debug.print range("A1").value
debug.print range("A1").value2
'results from Immediate window
2018-06-14
6/14/2018
43265
range("A1") = "abc"
range("A1").numberformat = "_(_(_(#"
debug.print range("A1").text
debug.print range("A1").value
debug.print range("A1").value2
'results from Immediate window
abc
abc
abc
range("A1") = 12
range("A1").numberformat = "0 \m\m"
debug.print range("A1").text
debug.print range("A1").value
debug.print range("A1").value2
'results from Immediate window
12 mm
12
12
If you are processing the cell's value then reading the raw .Value2 is marginally faster than .Value or .Text. If you are locating errors then .Text will return something like #N/A as text and can be compared to a string while .Value and .Value2 will choke comparing their returned value to a string. If you have some custom cell formatting applied to your data then .Text may be the better choice when building a report.

Out of curiosity, I wanted to see how Value performed against Value2. After about 12 trials of similar processes, I could not see any significant differences in speed so I would always recommend using Value. I used the below code to run some tests with various ranges.
If anyone sees anything contrary regarding performance, please post.
Sub Trial_RUN()
For t = 0 To 5
TestValueMethod (True)
TestValueMethod (False)
Next t
End Sub
Sub TestValueMethod(useValue2 As Boolean)
Dim beginTime As Date, aCell As Range, rngAddress As String, ResultsColumn As Long
ResultsColumn = 5
'have some values in your RngAddress. in my case i put =Rand() in the cells, and then set to values
rngAddress = "A2:A399999" 'I changed this around on my sets.
With ThisWorkbook.Sheets(1)
.Range(rngAddress).Offset(0, 1).ClearContents
beginTime = Now
For Each aCell In .Range(rngAddress).Cells
If useValue2 Then
aCell.Offset(0, 1).Value2 = aCell.Value2 + aCell.Offset(-1, 1).Value2
Else
aCell.Offset(0, 1).Value = aCell.Value + aCell.Offset(-1, 1).Value
End If
Next aCell
Dim Answer As String
If useValue2 Then Answer = " using Value2"
.Cells(Rows.Count, ResultsColumn).End(xlUp).Offset(1, 0) = DateDiff("S", beginTime, Now) & _
" seconds. For " & .Range(rngAddress).Cells.Count & " cells, at " & Now & Answer
End With
End Sub

Value2 is almost always the best choice to read from or write to an Excel cell or a range... from VBA.
Range.Value2 '<------Best way
Each of the following can be used to read from a range:
v = [a1]
v = [a1].Value
v = [a1].Value2
v = [a1].Text
v = [a1].Formula
v = [a1].FormulaR1C1
Each of the following can be used to write to a range:
[a1] = v
[a1].Value = v
[a1].Value2 = v
[a1].Formula = v
[a1].FormulaR1C1 = v
To read many values from a large range, or to write many values, it can be orders of magnitude faster to do the entire operation in one go instead of cell by cell:
arr = [a1:z999].Value2
If arr is a variable of type Variant, the above line actually creates an OLE SAFEARRAY structure of variants 26 columns wide and 999 rows tall and points the Variant arr at the SAFEARRAY structure in memory.
[a1].Resize(UBound(arr), UBound(arr, 2).Value2 = arr
The above line writes the entire array to the worksheet in one go no matter how big the array is (as long as it will fit in the worksheet).
The default property of the range object is the Value property. So if no property is specified for the range, the Value property is silently referenced by default.
However, Value2 is the quickest property to access range values and when reading it returns the true underlying cell value. It ignores Number Formats, Dates, Times, and Currency and returns numbers as the VBA Double data type, always. Since Value2 attempts to do less work, it executes slightly more quickly than does Value.
The Value property, on the other hand, checks if a cell value has a Number Format of Date or Time and will return a value of the VBA Date data type in these cases. If your VBA code will be working with the Date data type, it may make sense to retrieve them with the Value property. And writing a VBA Date data type to a cell will automatically format the cell with the corresponding date or time number format. And
writing a VBA Currency data type to a cell will automatically apply the currency Number Format to the appropriate cells.
Similarly, Value checks for cell currency formatting and then
returns values of the VBA Currency data type. This can lead to
loss of precision as the VBA Currency data type only recognizes
four decimal places (because the VBA Currency data type is really just a 64-bit Integer scaled by 10000) and so values are rounded to four places,
at most. And strangely, that precision is cut to just two decimal
places when using Value to write a VBA Currency variable to a worksheet range.
The read-only Text property always returns a VBA String data type. The value returned by Range.Text is a textual representation of what is displayed in each cell, inclusive of Number Formats, Dates, Times, Currency, and Error text. This is not an efficient way to get numerical values into VBA as implicit or explicit coercion is required. Text will return ####### when columns are too thin and it will slow down even more when some row heights are adjusted. Text is always VERY slow compared to Value and Value2. However, since Text retains the formatted appearance of cell values, Text may be useful, especially for populating userform controls with properly formatted text values.
Similarly, both Formula and FormulaR1C1 always return values as a VBA String data type. If the cell contains a formula then Formula returns its A1-style representation and FormulaR1C1 returns its R1C1 representation. If a cell has a hard value instead of a formula then both Formula and FormulaR1C1 ignore all formatting and return the true underlying cell value exactly like Value2 does... and then take a further step to convert that value to a string. Again, this is not an efficient way to get numerical values into VBA as implicit or explicit coercion is required. However, Formula and FormulaR1C1 must be used to read cell
formulas. And they should be used to write formulas to cells.
If cell A1 contains the numeric value of 100.25 with a currency number formatting
of $#,##0.00_);($#,##0.00) consider the following:
MsgBox [a1].Value 'Displays: 100.25
MsgBox TypeName([a1].Value) 'Displays: Currency
MsgBox [a1].Value2 'Displays: 100.25
MsgBox TypeName([a1].Value2) 'Displays: Double
MsgBox [a1].Text 'Displays: $ 100.25
MsgBox TypeName([a1].Text) 'Displays: String
MsgBox [a1].Formula 'Displays: 100.25
MsgBox TypeName([a1].Formula) 'Displays: String
MsgBox [a1].FormulaR1C1 'Displays: 100.25
MsgBox TypeName([a1].FormulaR1C1) 'Displays: String

Related

Convert numbers to non-numbers in Excel

I notice that numeric values like 123456 can be considered as numbers or non-numbers in Excel. Mixing numbers and non-numbers may result in unexpected results of = or XLOOKUP.
For instance, in the following worksheet, the formula of D3 is =ISNUMBER(C3) and the formula of D4 is =ISNUMBER(C4). Their values are not the same. Then =C3=C4 in another cell will return FALSE; =XLOOKUP(C3,C4,C4) will return #N/A.
So one solution to avoid such surprises is that I would like to convert all these numeric values from numbers to non-numbers, before applying formulas on them.
Does anyone know if it is possible to undertake this conversion by manual operations (select the range, then...)?
Does anyone know how to achieve this conversion by a subroutine in VBA (select the range, then run the VBA subroutine, then the selected range will be converted)?
If you firstly write numbers in a range, let us say "C:C", formatted as General, any such a cell will return TRUE when you try =ISNUMBER(C4).
If you preliminary format the range as Text and after that write a number, this will be seen by Excel as a String (non-numbers, as you say...) and =ISNUMBER(C4) will return False.
Now, if you will try formatting the range as Text after writing the numbers these cells will not be changed in a way to make =ISNUMBER(C4) returning FALSE. In order to do that, you can use TextToColumns, as in the next example:
Private Sub testTextToCol()
Dim sh As Worksheet, rng As Range
Set sh = ActiveSheet
Set rng = sh.Range("C:C")
rng.TextToColumns Destination:=rng, FieldInfo:=Array(1, 2)
End Sub
It will make the existing =ISNUMBER(C4), initially returning TRUE, to return FALSE...
Of course you cannot compare apples to oranges, thus strings are not comparable to integers/longs/numbers. Make sure that all you compare are apples.
In a routine this would be s.th. like
Option Explicit
Sub changeFormat():
' Declare variables
Dim Number As Variant
Dim check As Boolean
'Converts the format of cells D3 and D4 to "Text"
Range("D3:D4").NumberFormat = "#"
'Assign cell to be evaluated
Number = Range("D3")
Debug.Print Number 'Prints '123'
check = WorksheetFunction.IsText(Trim(Sheets("Tabelle1").Cells(4, 3)))
Debug.Print check 'Prints True
'Converts the format of cells D3 and D4 to "Numbers"
Range("D3:D4").NumberFormat = "0.00"
'Compare Cells
If Range("D3").NumberFormat = Range("D4").NumberFormat Then Range("D5").Value = "Same Format"
End Sub
Also see the docs

Returning 5-digit date in Debug.Print without formatting the cell

Background:
I am looking to get the 5-digit date from a cell which is formatted to display a date.
The 5-digit date should only be the immediate window (via debug.print).
My testing with results in the Script section (bottom).
I have a feeling that the answer will involve datediff()+2 based on the testing I did, but I can't figure out why that +2 is needed and don't want to just add that in if it's wrong in the future.
Issue:
I don't seem to be able to display the correct 5-digit date (as displayed with "general" format) by means of Debug.Print.
Question:
How do you get the 5-digit date, most often displayed with General-formatting, to display in the immediate window (debug.print) without first converting the cell's .numberformat?
Script:
Dim rng As Range, d1 As String, d2 As String
Set rng = Cells(1, 1) 'value = 20190101
Debug.Print Format(rng.Value, "#") 'returns 20190101
Debug.Print rng.NumberFormat = "General" 'returns "false"
Debug.Print Format(rng.value, "General") 'returns "Ge0oral" due to the "n" being recognized for a format
d1 = "1900/01/01"
d2 = rng.Value
Debug.Print 40729 + DateDiff("d", d1, d2) 'returns 84195
Debug.Print DateDiff("d", d1, d2) 'returns 43466
rng.NumberFormat = "General"
Debug.Print rng.Value 'returns 43468, but required formatting the cell
Range.Value2 doesn't use the Date type, so you can just Debug.Print rng.Value2.
There are two methods that come to mind when you are wanting to print the numerical representation of the date without changing the formatting of the cell.
The best way to do it is using the .Value2 property.
Debug.Print Rng.Value2
The 'less recommended' approach is by using the CDbl() function. I would use this moreso for variables and .Value2 for ranges.
Debug.Print CDbl(Rng.Value)
Or for variables
Dim d as Date
d = #01/01/2019#
Debug.Print CDbl(d)
Debug.Print CLng(d) 'If not using time - date only
Nice layout Cyril =)
To complement the other answers: Whereas Value2 is the more appropriate way to go, another very simple possibility is:
Debug.Print rng*1

Paste a value in Excel exactly as is visible

I have a simple request, to paste the data exactly as visible in Excel.
I have a list of dates in mm/yyyy format, but Excel keeps adding mm/dd/yyyy which is throwing off my analysis. It's formatted to show simply mm/yyyy but the actual cell value keeps getting set to mm/01/yyyy.
How can I simply copy/paste the value to be mm/yyyy.
I've tried Range("A1").Value = Range("A1").Value, but of course that just keeps the same info.
Yes, in my case since it's dates, I can do a kludgy function that takes the left three characters, and combines with the rightmost four. However, that really just gets the date number returned. I tried on G4 and get 4171730. Plus, I'd like to know how to do this with other types of cell values too (strings, numbers, etc.).
save the value and the format then set the cell as text and assign the formatted value:
Sub test()
Dim t As Variant
t = Range("A1").Value2
Dim x As String
x = Range("A1").NumberFormat
Range("A1").NumberFormat = "#"
Range("A1").Value = Format(t, x)
End Sub
This also works
Sub test()
Dim t As String
t = Range("A1").Text
Range("A1").NumberFormat = "#"
Range("A1").Value = t
End Sub
Range("A1").Value = Format(yourdate, "mm/yyyy")

Excel VBA date copying different for no apparent reason?

Simple task with confusing alternate results.
I'm copying a range of data using:
WS.Range("A2:Z" & lRow).Copy
ThisWorkbook.Worksheets("Import").Range("A2:Z" & lRow).PasteSpecial xlPasteValues
The first column from the copy sheet is a date in format 12/05/2017 01:00:00 (note the double space in between date and time)
In one instance of this the date values are pasted across fine and come out as dates - great!
In another instance of this the date values are pasted but come out as 14/05/2017 01:00 and these aren't registering as dates, rather as a text string.
I noticed I could go through the dates cell by cell and press enter which converted them to dates, so I tried using .range("A1:A100").value = .range("A1:A100").value to no avail.
I suspect it may have something to do with the day-month-year format as opposed to being month-day-year (since it works for a sheet that starts on 12-may but not on 14-may) but (1) could there be another difference, (2) why does pressing ENTER work fine and (3) how can I emulate pressing ENTER on my whole range of cells (bearing in mind .value = .value doesn't work)
In short: Type conversion (fixing the values before they cause trouble on your worksheet in the first place) is absolutely the best way to go.
In long:
Let's start with these two values:
22.05.2017 12:00
22.05.2017 12:00
I'll place the first in A1 and the second in B1. Note that Excel will often try to do the type conversion, so in this case I'll manually enforce A1 to contain text values by formatting the cell as such after-the-fact.
Let's verify:
Using the Immediate window, we can see that the compiler recognizes the content of A1 as a pure text value, while it recognizes the content in B1 as a date value.
The solution you need is to ensure that any text values are converted to date values:
Option Base 1
Sub pasteTextAsDate()
Dim dateArr As Variant
Dim rng As Range
Set rng = ThisWorkbook.Worksheets(1).Range("A1:A3")
' In the line below, we fill the variant variable with the content of the range, casting the variable into an array of variants
dateArr = rng
' For the sake of proving this code works, we'll start by printing the content and what type it is
For Each s In dateArr
Debug.Print s & " - " & TypeName(s)
Next s
For i = 1 To UBound(dateArr)
' This is where we loop through the array and cast any string values to date values
dateArr(i, 1) = CDate(dateArr(i, 1))
' Here we verify for ourselves that the conversions are OK
Debug.Print dateArr(i, 1) & " - " & TypeName(dateArr(i, 1))
Next i
' And here we print the result to the worksheet
ThisWorkbook.Worksheets(1).Range("C1:C3").Value = dateArr
End Sub
Result:

How do I get the format of a cell in VBA

When iterating through cells in a worksheet, how can I get what the format setting on the cell is? Because based on this, I would like to build a SQL statement to either add the single ticks or not to the value retreived
Sounds like you need the VarType() function. Vartype(Range("A1"))
OK, so you don't want to know the format setting for the cell, but whether the value is numeric.
Can you just call IsNumeric(Range("A1")) and quote it if False?
Based on your comment that some numbers are stored as text in the DB, you are not going to solve this by a simple formula. Can't you just quote the values as you build your SQL statement?
Try using the following in VBA:
Range("A1").NumberFormat = "0.00" 'Sets cell formatting to numeric with 2 decimals.
Range("A1").Formula = "=Text(6, " & """0.00""" & ")" 'Looks like a number _
' but is really text.
Debug.Print WorksheetFunction.IsNumber(Range("A1")) 'Prints False
Range("A1").Value = 6 'Puts number into the cell, which also looks like 6.00
Debug.Print WorksheetFunction.IsNumber(Range("A1")) 'Prints True
This should tell you if the value is really text or really a number, regardless of the cell's formatting properties.
The key is that the intrinsic Excel IsNumber() function works better for this purpose than the VBA function IsNumeric. IsNumber() tells you whether the cell's value is a number, whereas IsNumeric only tells you if the cell is formatted for numeric values.
I don't think there's any property of a cell that indicates whether the cell actually contains a numeric value, although VarType() might help, it gets tricky because Excel will allow a number-formatted cell to contain string, and a text formatted cell to contain numeric values, without overriding the NumberFormat property.
In any case you likely need some independent test to figure out whether a cell IsNumeric (or other criteria) AND whether its NumberFormat is among an enumerated list which you can define.
Sub numFormat()
Dim cl As Range
Dim numFormat As String
Dim isNumber As Boolean
For Each cl In Range("A1")
numFormat = cl.NumberFormat
isNumber = IsNumeric(Trim(cl.Value))
Select Case numFormat
Case "General", "0", "0.0", "0.00" ' <--- modify as needed to account for other formats, etc.
If isNumber Then
Debug.Print cl.Address & " formatted as " & numFormat
End If
Case Else
'ignore all other cases
End Select
Next
End Sub
I don't think the format of the cell is the important thing. Rather, it's the data type of the field in your database. If you have the string 'foobar' in a cell and you create an INSERT INTO sql statement that attempts to put that into a Long Integer field, it's going to fail regardless of tickmarks.
Conversely, if a cell contains a numeric value (like 100) that needs to go into a VARCHAR field, it will need tickmarks (like '100').
If you're using ADO, check the Type property of the Field object to determine the data type. Use this list http://support.microsoft.com/kb/193947 to see what the types are. Then set up the SQL statement according to the field type.

Resources