I'm trying to create a custom format such that the number will be displayed with commas as the thousands separator. Where I am struggling to find a solution (both through trial and error as well as searching) with a decimal point if the number has one, but without a decimal point if the number is whole.
Here's what I would like to achieve:
- "123" displays as "123"
- "1234" displays as "1,234"
- "1234.5" displays as "1,234.5"
- "1234.56" displays as "1,234.56"
- "1234.567" displays as "1,234.57"
Here's what I have tried so far, to no avail:
Print Format(1234, "Standard") 'Displays "1,234.00"
Print Format(1234, "#,###.##") 'Displays "1,234."
These are not the desired results, as it unnecessarily displays the decimal. How can I access the decimal point when needed, and avoid when not, all while having the thousands separator?
You may complete this task by using the custom format Function CustomFormat in Excel VBA as shown below:
Sub TestFormatString()
Debug.Print CustomFormat(123)
Debug.Print CustomFormat(1234)
Debug.Print CustomFormat(1234.5)
Debug.Print CustomFormat(1234.56)
Debug.Print CustomFormat(1234.567)
End Sub
Function CustomFormat(InputValue As Double) As String
CustomFormat = Format(InputValue, "#,###.##")
If (Right(CustomFormat, 1) = ".") Then
CustomFormat = Left(CustomFormat, Len(CustomFormat) - 1)
End If
End Function
The result shown below conforms to your requirements:
123
1,234
1,234.5
1,234.56
1,234.57
Note: as you didn't specify how to display the numbers less that 1 (for example, 0.123) then consider two possible solutions:
CustomFormat VBA Function shown above will display it as
.12
in order to display it as 0.12, just change the format mask from "#,###.##" to "#,##0.##" in CustomFormat Function, keeping the rest intact.
Hope this may help.
You can add Conditional Formatting for those with a Decimal then set the default Number Format with 0 decimals.
Add Conditional Formatting: with #,##0.## unless you want 0.5 as .5
Set Default Number Format:
Done
Related
I have a PowerPoint presentation, where I add an automatically updating date-time field to a text box (via Insert -> Text -> Date & Time, with checked "Update Automatically"). The textbox is resized so that the text (Wednesday, March 31, 2021) is split across two lines, for example:
Wednesday,
March 31, 2021
I would like to process the information line-wise, trying the following VBA:
Sub TryLines()
For Each Line In ActiveWindow.Selection.TextRange2.Lines
Debug.Print Line
Next Line
End Sub
If I call this function with the whole date-time field (distributed across two lines) selected, I get the following output:
Wednesday, March 31, 2021
Wednesday, March 31, 2021
I.e. PowerPoint recognizes that there are actually two lines, but is apparently not able to "break up" the date-time field into actual separate lines, and instead treats the date-time field as a monolith. A similar thing seems to happen for Characters.
Actual question: Is there a way to retrieve the text within a field line-wise? I would like to be independent of date format and of the actual wrapping.
Some more background, if helpful: At last, I would like to compute the polygon formed by the text-selection highlightning. Since I did not find a method to do this in TextRange2, I thought about splitting into lines and compute rectangles per line:
Sub TryToComputeLineBounds() ' call this while the date-time field is selected
For Each Line In ActiveWindow.Selection.TextRange2.Lines
Dim x(4) As Single, y(4) As Single
Line.RotatedBounds x(1), y(1), x(2), y(2), x(3), y(3), x(4), y(4)
Debug.Print Line
For i = 1 To 4
Debug.Print x(i) & " | "; y(i)
Next i
Next Line
End Sub
I realized that the measured points are the same per (what I assumed) line, and then found out that Lines does not actually give me the lines within a date-time field.
I think you're getting the Lines and Characters as a monolith because there is something special about inserted Date & Time that means it will never "break up". I tried your code with a text box that had some characters I typed in and it worked - I was able to read lines and characters one at a time.
So maybe make a temporary copy of the text box but without automatic date inserted, i.e. a Text value equivalent to the date, then use the copy to compute the polygon.
As Morboss surmised, a date field is read as one line regardless of how it appears. In the user interface, if you click on a multiline text, the insertion point appears where you click. Not so with a date: the first time you click on it the entire field is selected and no insertion point appears.
As a workaround, get the day name and date with string parsing:
Sub GetDayName()
Dim SpacePos As Long
Dim FieldText As String, DayName As String, DateValue As String
FieldText = ActiveWindow.Selection.TextRange2.Text
SpacePos = InStr(FieldText, " ")
DayName = Left(FieldText, SpacePos)
DateValue = Right(FieldText, (Len(FieldText) - SpacePos))
MsgBox DayName
MsgBox DateValue
End Sub
I'm working with Windows Forms Application in Visual Studio 2015, using .NET Framework 4.5.2. So I'm making a simple program which includes a label to display a value. The label (called lblMoney) displays the currency (in this case, $) and then the value (e.g 350). Together it looks like $350.
Now, I made another form with a textbox called txtCash and a button called bApply. You enter any integer (e.g 350) into txtCash. When you press bApply, the number in txtCash will add to the number in lblMoney. So, if you had 5 in lblMoney, and you entered 350 in txtCash, lblMoney would display 355.
Here's my code to add to the number:
My.Forms.VeilSideCash.lblMoney.Text = cstrx + txtCash.Text.ToString
The form VeilSideCash is the form that holds lblMoney.
Here's the code for cstrx:
Dim cstrx = "$" & Val(My.Settings.Money.ToString)
The problem here is that, instead of overwriting to lblMoney, the new number is just added after the original number. So if lblMoney has 5 and you enter 350 into txtCash, lblMoney looks like $5350.
How would I go about overwriting with the new number (adding to) instead of replacing?
Any help would be appreciated. Feel free to edit incase I messed something up while explaining.
The problem with your code is the Option Strict setting for your project. You have it set to Off and this allows your code to freely treat strings as they were numbers and try to use them in mathematical operations.
But, when you use the + operator between two strings, it doesn't matter if, for a human being, the two strings represent a number, the compiler see them as strings (cstrx, txtCash.Text) and thinks that you want to use the concatenation operator defined for strings (the +). Yielding wrong (for you) results .
I really suggest you to not use the automatic conversions made by the compiler on your code, instead, when you need to do math operations, always convert your strings to numbers and do the math with variables of numeric type (You could also change the Option Strict to On, but you should be prepared to solve a lot of problems in your current code)
Instead, with a proper numeric approach, you get the text inside txtCash and try to check if your user has correctly typed a decimal value.
Dim cash As Decimal
if Not decimal.TryParse(txtCash.Text, cash) Then
MessageBox.Show("Invalid cash value")
Return
End if
Now get the text of lblMoney and convert it back to a decimal number alerting the compiler that there is a currency symbol in the text to convert
Dim current As Decimal
current = decimal.Parse(lblMoney.Text, NumberStyles.Currency, CultureInfo.CurrentCulture)
Now you have two numbers and the + operator does what you expect. It adds the two numbers together. Finally you could write back the result with a proper currency formatting
Dim result as Decimal
result = current + cash
lblMoney.Text = result.ToString("C")
You need to remove the "$" and convert to a number. I used a decimal so you can include cents if you want:
Dim sum As Decimal
sum = Val(cstrx.Replace("$","")) + Val(txtCash.Text)
My.Forms.VeilSideCash.lblMoney.Text = sum.ToString()
Dim cstrx = sum.ToString("C")
Note that I used the "C" to format the sum as currency. That automatically puts the $ on for you, or uses other currency symbols for other countries.
I'm not a big VB.net user, so my syntax may be slightly off.
When you use the + operator with string unexpected results can occur. In this case the string with a $ cannot be implicitly converted so you should explicit convert it
Once you convert your strings to a number type you can then use the + operator and they can be implicitly converted back to a string.
My.Forms.VeilSideCash.lblMoney.Text = decimal.Parse(cstrx, NumberStyles.Currency)
+ decimal.Parse(txtCash.Text ,NumberStyles.Currency)
I have an issue with vba macros. Cell contains value 941144280284022000000. But when try to get this value in macros, variable is equal
9.41144280284022E+20. Is it possible to get "real" value?
Thanks.
Here is an example:
Sub dural()
MsgBox ActiveCell.Text
End Sub
This will insure that long strings of numerals are not converted to numbers.
EDIT#1:
This assumes that the cell actually displays the long string of numerals. If the cell displays ####, that is what the sub will pick-up. If the cell displays 9.41E+20, then that is what my sub will pick-up.
My sub will not necessarily pick-up the contents of the Formula Bar.
Try a simple :
Dim myVar as Variant
myVar = CDec(Range("A1").Value)
If you wish to store a value that looks like a number (or is a number) but has more than 15 digits of precision, then you must either format the cell as text before entering the value, or prepend the value with a single apostrophe to indicate to Excel that the value is to be treated as text and not a number.
If you don't do this, then as soon as the value is entered, there's a good chance it will be altered by Excel. The trailing zeros in the example value mean this does not happen in this specific case, but try changing that last 0 to 1 and you'll see what I mean.
You enter: 941144280284022000001
Excel converts this to: 941144280284022000000
More reading: https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel
I'm trying to create an Inputbox which has a number, with decimals, as the default value. I'm setting the Inputbox to the formula type, because the user might input a formula or reference a cell.
The problem is that the Inputbox seems to strip the comma and coerse the number to a string. I could fix this casting the number as a string with Format, and then going back to a number afterwards, but losing precision. And I'd like to understand what's going on.
The code is:
Sub test()
Dim Defolt As Double
Defolt = 1.1866701960364
Dim InputValue
InputValue = Application.InputBox("Value?", , Defolt, , , , , 0)
'for this example, the user just clicks OK to the default value
Debug.Print InputValue
End Sub
The results are these:
Thanks!
ps: the locale is Spanish. Excel version is Excel 2010 32bits.
Have a look here. The important part is right under the table:
You can use the sum of the allowable values for Type. For example, for an input box that can accept both text and numbers, set Type to 1 + 2.
and a little further down in the remarks:
If Type is 0, InputBox returns the formula in the form of text — for example, "=2*PI()/360". If there are any references in the formula, they are returned as A1-style references. (Use ConvertFormula to convert between reference styles.)
Try setting the type as 1 and see if you can still use a formula and number. The documentation leads me to think that you can (basically you get formula for free). Since you're setting the type to 0, you're getting back the default Text type.
I have UserForm1 which is a multipage userform and I am trying to access the information that was gathered through the form in a sub located in Module1. This sub will need to access several different values and do different things with those values so this is going to be a multipart question.
I have the below code in which I attempt to use one of the values as the upper limit of a For Next Loop. However the current problem is that when the code reaches this line it jumps to the Userform_Initialize routine.
For X = 1 To UserForm1.LocalOffer.Value
Second part of this question comes from inside the For Next loop from above. Where I have the below code. Which would ideally allow me to cycle through a series of similarly named Textboxes from the userform. Not even sure if that will work as the code keeps breaking before getting to that part.
Range("B" & X).Value = UserForm1.Controls("LocalTier" & Tier).Value
Last Part of this question if I have a Textbox in the userform that contains a date in the format 1/18/2015 is there a way for me to grab just a portion of that date say for instance just the Day or just the last digit of the year?
I am using Excel 2013 but the file will be ran on Excel 2007
Edit:
Turns out that problem 1 was fixed by not closing the userform with the X button but instead adding a line to hide the userform when you hit the last button. As it turns out my code for the second question worked just fine once i got past that. Only question left is the last one which I have no ideas on.
As from the comments, I see you don't need anymore to know about points 1 and 2, I will hence limit my answer to the point 3.
Last Part of this question if I have a Textbox in the userform that contains a date in the format 1/18/2015 is there a way for me to grab just a portion of that date say for instance just the Day or just the last digit of the year?
You can use either string manipulation, or date conversion.
Let's assume the Textbox is called myDateTextbox
String manipulation
Among the string manipulators that VBA provides, I would cite Left() and Right().
For example:
last_digit_of_the_year = Right(myDateTextbox.Text, 1)
will return you the last character of the string. On the other hand:
first_digit = Left(myDateTextBox.Text,1)
will return you the first digit of the string.
You can use the Len(myDateTextBox.Text) built-in to return the current length of the string.
Date conversion
You can simply convert your string into date using the CDate() function. Please note this function will return an error if you pass an invalid string. If your textbox contains 24/01/1990, you can first convert the string into a date:
myDate = CDate(myDateTextBox.Text)
Hence, you can retrieve day, month or year like this:
myYear = Year(myDate)
myMonth = Month(myDate)
myDay = Day(myDate)
Please note that CDate recognizes date formats according to the locale setting of your system.. Hence, if the format in the TextBox is not the same than the one of your system, then consider manipulating the string before to adapt it to the proper format. For example, if your system has settings DD/MM/YYYY and your textbox shows a MM/DD/YYYY type, you can "adjust it" as follows:
wrongFormat = myDateTextBox.Text
splittedDate = Split(wrongFormat,"/")
goodFormat = splittedDate(1) & "/" & splittedDate(0) & "/" splittedDate(2)
If wrongFormat was 1/18/2014 but your system would like to read it as 18/1/2014, it's now fine because goodFormat will be equal to 18/1/2014 after the split and re-build.