I really hate to ask what I feel like should be able to be resolved by a quick run to a search engine, but I have looked to no avail.
I am extracting some data from a CSV that really should be expressed as a decimal but is not (it should 757.46 not 75746 for example). Using Format(expresion, "Currency"), Format(expression, "Fixed") and so on, does not have the desired effect. The functions just append a decimal point and extra zeroes (75746.00 instead of 757.46). Is there any an easy way to indicate to the Module to take an integer, add a decimal with two numbers to right and variable numbers to the left, and not add anything extra. I am sure there is a way to do this by converting to string, using sub strings and the like, but I would like to avoid that if possible.
Thanks!
If using Format(expresion, "Currency") is giving you 75746.00 as the cell, then Format(expression/100, "Currency") should give the right result. You would also want to put in a check that expression did not already have a decimal point
Dividing the numbers you've been given by 100 and then formatting as currency should work.
Use the code below, it will loop through the rows and put the orig values from Column A in Column B:
Sub ChangeOrigFormat()
Dim lrow As Long
' modify to your last row , or use dynamic last row method
For lrow = 2 To 10
' put result on Column B
With Cells(lrow, 2)
' looking in original values in Column A >> modify according to your needs
.Value = Cells(lrow, 1) / 100
' using a decimal with 2 digits after the "."
.NumberFormat = "0.00"
End With
Next lrow
End Sub
Related
Here in Brazil we use a 20 number string to identify judicial cases, in a standardized manner, separated by a dots and dashes.
The problem is in most systems we use, when you export to excel it takes away the dots and dashes, thus creating a problem when exporting that data to a system that actually needs the dots and dashes to work (some poorly coded excuse for a legal system).
What I'm trying to do is use VBA to call a function that fixes the number so I can export it.
The way the standardized number works is like this:
0010159-24.2015.8.10.0001
(CaseNumber-VerifyingDigit.Year.Court.State.City)
When I export my current data do Excel, it shows like this:
00101592420158100001
And I need it to go back to the format above!
I'm 100% stuck though, would love some input.
Edit:
To clarify some stuff.
The string is always 20 numbers long, its a standardized number set by our National Council of Justice, it never changes. All the cases follow that 20 number string format.
The cell with the number is always in text, so it doesn't show as scientific notation.
If I got you correctly, you want to change the values in a range so that they are in the needed format (with dots and dashes) so then you can export it somewhere else?
Maybe something like that can help? I assume you have your 00101592420158100001 in a cell A1.
Sub x()
Range("A1").Value = Format(Range("A1"), "#######-##.####.#.##.####")
End Sub
EDIT 1: As the number in cell A1 starts with 00 I assumed that you have cell A1 formatted as text. So that the macro proposed by me will only add a certain dashes or dots.
EDIT 2: Why the code above was not working is the fact that in VBA the "#" character represents a displayed character, and "#" works for numbers.
So, as suggested in a comment below, the subroutine like this:
Sub x()
Range("A1").Value = Format(Range("A1"), "#######-##.####.#.##.####")
End Sub
will do the trick.
But as you want to choose a range to be formatted I would suggest using something more like that:
Sub forhenriquef()
Dim cell As Range
Dim rng As Range
On Error Resume Next
Set rng = Application.InputBox("Choose the range, OK?", "Macro for henriquef", Selection.Address, , , , , 8)
If rng Is Nothing Then Exit Sub
For Each cell In rng
cell.Value = Format(cell, "#######-##.####.#.##.####")
Next cell
End Sub
This solution isn't VBA but we can make one, however if you're just looking to reformat the text and it is as you show with no variation (I sense a date is in there and the length of this string may vary), this should work for you.
Assuming you data starts in cell A1, you can put this formula in row 1 somewhere and get the desired format.
=LEFT(A1,7)&"-"&RIGHT(LEFT(A1,9),2)&"."&RIGHT(LEFT(A1,13),4)&"."&LEFT(RIGHT(A1,7),1)&"."&LEFT(RIGHT(A1,6),2)&"."&RIGHT(A1,4)
Or as Scott had mentioned you can use the shorter:
=TEXT(LEFT(A1,9),"0000000\-00\.")&TEXT(MID(A1,10,20),"0000\.0\.00\.0000")
I am trying to write a simple formula to count how many times a particular name appears in a column. I am using COUNTIF as it is a pretty straight forward process but I cannot work out how to make it happen for a name in particular. This is the case:
The column named Age will display cells with one or more names, separated by commas in case there are more than one value. Putting "Young" as an example is easy to tell the COUNTIF formula to give me the number representing how many times this word appears, either being the only value in cell or as a part of a cell with a longer string value by giving the formula the "Young" statement.
The problem comes when I want the formula to count how many times "Mature" appears in my column. I cannot work out the way to make it count only when it says "Mature" without also taking all the "Early_Mature" or "Semi_Mature"
I know this is easy for whoever knows the basics of Excel so I don't think there is need to give more details.
Thanks
Most of the times I succeed solving such problems by adding the same delimiter (of our string) at the beginning and end of the main string.
So since your data is at COL:Y, you may create a new helper COL:Z and enter this formula:
="," & Y1 & ","
I did not use any spaces before or after comma since your data seems not having any space. Depending on your case, you may have to use spaces.
Now your string is wrapped with commas, which you may alter COUNTIF formula to such:
=COUNTIF(Z:Z,"*,"&B1&",*")
* characters are jokers which stand for "anything" in this context.
With an UDF. Code goes in a standard module added by opening the VBE with Alt + F11 then right-click in project explorer and add module.
Code
Option Explicit
Public Function GetCount(ByRef selectRange As Range, ByVal searchTerm As String) As Long
Application.Volatile
With selectRange
Dim arr(), joinedString As String, i As Long, outputCount As Long
arr = .Value
joinedString = Join(Application.WorksheetFunction.Transpose(Application.WorksheetFunction.Index(arr, 0, 1)), ",")
Dim arr2() As String
arr2 = Split(joinedString, ",")
For i = LBound(arr2) To UBound(arr2)
If Trim$(arr2(i)) = "Mature" Then
outputCount = outputCount + 1
End If
Next i
End With
GetCount = outputCount
End Function
Usage in sheet
To get the number of occurrences of Mature excluding those that have prefix you can use this array formula:
=SUM(((LEN(A2:A7)-LEN(SUBSTITUTE(A2:A7,"Mature",""))) / LEN("Mature"))-((LEN(A2:A7)-LEN(SUBSTITUTE(A2:A7,"_Mature",""))) / LEN("_Mature")))
Please take note that this formula is applied with Ctrl + Shift + Enter.
Given that your range is in Y:Y column, just change the range to one you need.
An alternative would be to change "Mature" to "Fully_Mature". Then you could just use Countif().
You'd have to do this in steps:
1) Change "Early_Mature" to "E_M"
2) Change "Semi_Mature" to "S_M"
3) Change "Mature" to "Fully_Mature"
4) reverse of step 1).
5) reverse of step 2).
My current project is to rewrite a program that was written in 98. Their solution then was to do some in Excel, do this next part in Word, and then back to Excel in the 3rd step. The program syncs 2 files, and their solution is just dandy for 20 files that need to sync - however, now there are around 1,000 files to sync - so I'm working on streamlining the process. The output from the files ranges between X.X and X.XXXXX - I formatted them with
Columns("A:A").Select
Selection.NumberFormat = "0.00000"
To make the file easier to read. But the 2nd part is to append a comma, and then a letter regarding the numbers relation: not necessary for the question at hand. When I try to append a comma, Excel attempts to "fix" it and outputs it incorrectly. For example:
0.42400
0.87200
1.31600
1.75200
Becomes:
.424,
.872,
1.316,
1.752,
When it should stay at X.XXXXX. Here is what I have tried:
Changing Excel's options so that decimal identifier is '.' and thousands is ':'.
`Range("A" & ictr) = Format(Range("A" & ictr) & ",")
Selection.NumberFormat = "0.00000,"
Other forums/Google/Bing
I'm really assuming this can be done in Excel, I just cannot find a solution other than re-opening Word, and then back to Excel
Any ideas?
Since you state that the
2nd part is to append a comma, and then a letter regarding the numbers
relation
you're going to need a formula, not just a formatting option. In your VBA code, select the cells next to the original values, and add this line of code:
Selection.FormulaR1C1 = "=TEXT(RC[-1],""#0.00000"") & "","""
This gets you the formatting and the comma (regardless of the length of the number to the left of the decimal point) -- but to add that last letter, you'll obviously have to modify the formula to meet your specific needs.
You could insert a column and use a formula. Something like this should work
Dim TotalRows As Long
TotalRows = ActiveSheet.UsedRange.Rows.Count
Columns("B:B").Insert
With Range(Cells(1, 2), Cells(TotalRows, 2))
.Formula = "=LEFT(""'""&A1&REPT(""0"",5),7)"
.Value = .Value
End With
Columns("A:A").Delete
I have an excel file, i need to do is, format the cell depending on another cell.
If cell "S1" is having 3 decimals cell "T1" should be have the same number of decimals and it is calculated field.
ex:
S1 = 11.123 ---- T1 = 1.099
I need to do for the entire column.
I am able count the number of decimals on the cell by placing the code on worksheet_change. But i have no idea how to format it.
Please help in this regards.
Thanks in advance
Since you wanted an example, here it is :) But please note that this is not an answer. It's just that the comments will not be able to hold all this data plus it will ruin the formatting as well. Having said that, it is almost as good as an answer as it covers almost every aspect of what you want...
My Assumptions:
Data is in Sheet1
Col S and Col T are identical
Logic to achieve what you want
Get the last row of Col S. For example see this
Loop though the cells in Col S and check the number for decimals
Use .Numberformat for format the cells in Col T based on the number of decimals in respective cell in Col S
Few code snippets
Looping Through cells in Col S
For i = 1 To LastRow
If Sheets("Sheet1").Range("S" & i) .... Then
End If
Next i
Setting the number format of cell T in the above loop
'~~> n below is the number of decimal places that you want
Sheets("Sheet1").Range("T" & i).NumberFormat = "0." & String(n, "0")
Incorporate all these and then try to come up with a code. Let use know where you are stuck and we will take it from there.
I have (what seems like it should be) a simple problem. I need to be able to tell excel (in vba) that a cell's contents are numeric, but I don't really want to apply any formatting to it. I need my trailing zeros left how they are. Am I missing something incredibly simple?
Update:
I'm getting xml data from a query and am placing it into the spreadsheets. But lets say one of the cells has 589.950000 I need to keep those additional zeros on display for the user (don't ask me why, they just want the precision) but excel converts it to 589.95. I also need for them to be able to do spreadsheet calculations on that cell (so I can't format it as text).
Update 2:
Further Clarification. Lets say I have 4 values that I'm going to place into a column
595.56000
15.00
90.00050
1919.120000000
is there one numeric format that I can apply to that column that will display those exact numbers?
You can't do this with one custom format.
What you can do is make a macro that does the input for you and modifies each cell format as it puts the value into it. You can use the Macro Recorder to get a handle on where to start with this.
I think you can do:
Val(Range("A1").Value)
Which will evalulate the text to a number
You mean you want to use the value of the cell as a numerical one but you don't want to change the real val of the cell ?
In vba, you can use CInt(yourvar) to convert a string to number. Thus, the value you get will sure be an integer.
[edit] Btw, if you want to display or set back the value to a cell with the format you want, you can use the Format ( expression, [ format ] ) function of Excel vba
[edit 2]
As you cannot predict how many zeros you will have, i can't see any number format that would work. You'd probably better use the Text format as Lance Robert suggested. To use formula on text cells, you can use the VALUE formula to convert the cell to use it as a number. If you need it on a range, you may have to use array formulas like this : {=SUM(IF(ISTEXT(A1:A4);VALUE(A1:A4);A1:A4))}.
Please let us know if you want some help on these formulas.
Regards,
Max
If you know the number of decimal places you need, you can use something like the following:
Range("A1").NumberFormat = "0.000000"
I suppose you could even get fancy and check the number of trailing 0s in the code and adjust the formatting as required.
UPDATE:
Here's a VBA function that takes the number as a string and returns the appropriate NumberFormat string.
Private Function trailing(strNum As String) As String
'From number entered as string, returns Excel Number format to preserve trailing zeroes in decimal.
Dim decpt As Integer
Dim aftdec As Integer
Dim strTmp As String
decpt = InStr(strNum, ".")
If decpt = 0 Then
strTmp = "0"
Else
aftdec = Len(strNum) - decpt
strTmp = "0."
If aftdec <> 0 Then
For i = 1 To aftdec
strTmp = strTmp & "0"
Next
End If
End If
trailing = strTmp
End Function
Can't believe I just stumbled on this...
I know it's old but might as well give very simple answer:
Custom format and simply use # symbol. Will be treated as integer and left exactly as typed in to cell.