VBA Currency to Text Conversion - vba

I have a list of currency values in one WorkSheet such as $4,250.00 that I want to compare to the equivalent text/string; e.g. "$4,250" in another Worksheet. When I convert the currency value to text prior to the InStr comparison, I lose the "$" and "," resulting in "4250" which makes the comparison false. Is there a way to convert the currency value to text and not lose the format?
Thanks

A numeric value can be formatted to currency in VBA using Format(value, "Currency"), e.g. Format(Range("A1").Value, "Currency").
Also, the .Text property will return the formatted string, so Range("A1").Text would give you a string of $4,250.00 if cell A1 contained 4250 and was formatted in a standard currency format.
In Excel itself, I would suggest using a formula such as =TEXT(A1,"$#,##0.00").

Related

Convert string to double in word (vba)

I am trying to convert a text in a word document to be a double, so I can do currency formatting on it. I receive this text from a mail merge. How would I create a macro that can receive this text and return it as a number?
I'm unfamiliar with word, and VBA script. What I have made so far is
Function stringToDouble(baseString As String)
Dim num As Double
num = Val(baseString)
stringToDouble = num
End Function
I'm not sure how I would call this macro. Because it takes a parameter it does not show up in the macro table.
I may be completely off on how to convert text to a double in word, but any help is appreciated.
Thanks. Please comment for any clarifications.
You don't need a macro for this!!! All you need do is learn how to use formatting switches in Word fields.
To control number & currency formatting in Word, add a numeric picture switch to the mergefield. To do this:
select the mergefield;
press Shift-F9 to reveal the field coding. It should look something like {MERGEFIELD MyData};
edit the field so that you get {MERGEFIELD MyData # $,0.00} (or whatever other numeric format you prefer - see below);
position the cursor anywhere in this field and press F9 to update it.
Note 1: The '# $,0.00' in the field is referred to as a numeric picture switch. Other possibilities include:
# 0 for rounded whole numbers
# ,0 for rounded whole numbers with a thousands separator
# ,0.00 for numbers accurate to two decimal places, with a thousands separator
# $,0 for rounded whole dollars with a thousands separator
# "$,0.00;($,0.00);'-'" for currency, with brackets around negative numbers and a hyphen for 0 values
Note 2: The precision of the displayed value is controlled by the '0.00'. You can use anything from '0' to '0.000000000000000'.
If you use a final ';' in the formatting switch with nothing following, (eg # "$,0.00;($,0.00);") zero values will be suppressed. Note that this suppresses 0s resulting from empty fields and from fields containing 0s.
Note 3: If you use a decimal tab or right-aligned tab to align the values, wrap the switch in quotes (i.e. # "$,0.00") and insert a tab into the field code after the $ sign, you can have the values output with the decimal alignment occurring after the $ sign.
For more Mailmerge Tips & Tricks, see: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html
If you want to convert the number to a Double data type then try this:
Function StringToDouble(ByVal baseString As String) As Double
StringToDouble = VBA.CDbl(baseString)
End Function
If you're only concerned about formatting currency, convert the string to the Currency data type like this:
Function StringToCurrency(ByVal baseString As String) As Currency
StringToCurrency = VBA.CCur(baseString)
End Function
You will still need to format the number but both functions give you a number that can be formatted.
Here's an example that also gives you a string formatted as USD (e.g. $4,999.75). It requires the StringToCurrency function above.
Sub test()
Dim stringNum As String
stringNum = "4,999.754501"
Debug.Print "stringNum=" & stringNum ' outputs 4,999.754501
Dim currencyNum As Currency
currencyNum = StringToCurrency(stringNum) ' outputs 4999.7545
Debug.Print "currencyNum=" & currencyNum
Dim formattedString As String
formattedString = Format$(currencyNum, "$#,##0.00")
Debug.Print "formattedString=" & formattedString ' outputs $4,999.75
End Sub

Excel macro populates a date cell but its not equal to a real date cell

I have a function that returns an ODate (date in a double data type) from a called API.
Private Function CoreCompute(....)
....
CoreCompute = oXmlHttp.ResponseText //sample return: a double value 41902, which is equivalent to 2014/09/20
End Function
When this is called to a cell with a Format of Date, it is not transformed to a date value and not equal to a true date cell.
How can I output a value in a cell which can be compared to an actual date cell value?
PS. Q17 is actually Q16
Try CoreCompute = CDbl(Trim(oXmlHttp.ResponseText))
This probably works because your response is a string and it could have whitespace included.
Trim will remove the whitespace
CDbl will ensure that value is converted to a number type.

Populate DataGridView Column With a Double forcing to 2 Decimal Places, with Sort

Sorry for the weird title but I didn't know how to phrase it any better.
I'm trying to populate a column on my DataGridView with a number that has 2 decimal places.
To do this I'm using
Dim _Size as Double = 1.2345
DataGridView1.Item(0, 0).Value = Format(_Size, "0.00")
This populates the data correctly, but when sorting by this column, it treats the item as a string.
I found on the net, that if you convert the data being entered to a number type (double, integer etc..), it will then sort it like a number instead of a string.
This works brilliantly, but any values that are integers with 2 decimal places (i.e. 1.00) are changed to 0 decimal places.
So, if I had the following values
1.2345
2.2345
3.2345
4.2345
5.0011
and I formatted as 2 decimal places they would then become
1.23
2.23
3.23
4.23
5.00
if I then converted them back to doubles they then become
1.23
2.23
3.23
4.23
5
Is there any way of populating a DataGridView with these values formatted as 2 decimal places but keeping the double type so that the column sorts correctly?
I hope I've explained this clearly.
Any help would be greatly appreciated
You are converting your values to string by using the legacy VB Format function:
Function Format(Expression As Object, Optional Style As String = "") As String
When it is converted, the other decimals are lost. Use the Format Property of the DefaultCellStyle for that column to specify the number of decimal places you want (N2 for 2 decimals). Unlike VB's Format, this acts like a "DisplayAs" without altering the value or changing the Type.
In the Properties pane, select Columns to start the column editor
Pick the column
Click the DefaultCellStyle property
For Format, click the ... button to select from a list. Numeric and 2 Decimals results in N2
If you store a numeric values in that column, only 2 decimal places will display, but the actual/original value will still be available and sorting will using the numeric value rather than a text sort.

VB.NET Code to Change Excel 2007 Column Cell Format to Number no Decimal

VB.NET code to change Excel 2007 Column Cell Format from Text to a Number no Decimal.
Right now Column F shows values that look like
3E+13
3E+13
Manually changing it to a Number with No Decimal shows the correct value.
30000046605562
30000041582875
Of course I would like to automate this.
Select the rows (maybe programmatically) and perform
Selection.NumberFormat = "0"
To select all cells in the sheet call
Cells.Select

vb.net currency display has four zeros instead of two

when i get the money field from sql server to vb.net code, i always get 1.0000 instead of 1.00. how do i convert this to 1.00 in vb.net?
TD = New HtmlTableCell
If Not SqlDR("Price") Is DBNull.Value Then
TD.InnerHtml = SqlDR("Price")
Else
TD.InnerHtml = "0.00"
End If
SQLDR is my sql data reader
That is because SQL Server stores the MONEY field with 4 decimal places. To see it with 2, use the String.Format method.
String.Format("{0:c}", 10) ''result: $10.00
String.Format("{0:N2}", 10) ''result: 10.00
See these pages for more ways to format your numbers
Standard Numeric Format Strings
Custom Numeric Format Strings
You need to format the data when you output it:
myMoney.ToString("0.00");
Are you sure you aren't confusing the display of the value with the actual value?
1.0000 and 1.00 are the same value.
If you want to display only a certain number of places when converting a value to a string, you should look at the Custom Numeric Format Strings section in the MSDN documentation to figure out the format string to pass to the ToString method on the Decimal, Double, Single structures, or the static Format method on the String class.