Excel VBA changing decimal from point to comma - excel

When Im working with decimals in arrays, VBA is changing the decimal point into comma:
And, since the comma is already the separator, VBA is separating the decimals into two integer numbers(e.g. 0,01 into 0 and 01, and 0.21 into 0 and 21
Im using the split/join technique since I need to merge several arrays depending on an index.
Is there any way to stop VBA converting decimals from point to comma?
Thanks

Use Str and text values, as Join always returns text:
Join(Array(Str(Val / 100), "0.021", "2")

Related

Formatting number with a bunch of zeroes

Somewhat simple problem:
I need to turn a column A, which contains numbers with up to 1 decimal (20, 142, 2.5, etc.) to a string with a specific format, namely 8 whole digits and 6 decimal digits but without the actual decimal period, like so:
1 = 00000001000000
13 = 00000013000000
125 = 00000125000000
46.5 = 00000046500000
For what it's worth, the input data from column A will never be more than 3 total digits (0.5 to 999) and the decimal will always be either none or .5.
I also need for Excel to leave the zeroes alone instead of auto-formatting as a number and removing the ones at the beginning of the string.
As a makeshift solution, I've been using =CONCATENATE("'",TEXT(A1,"00000000.000000")), then copying the returning value and "pasting as value" where I actually need it.
It works fine, but I was wondering if there was a more direct solution where I don't have to manually intervene.
Thanks in advance!
=TEXT(A1*1000000,"0000000000000") I think that's what you mean.

Excel formula to generate series for 3 digit

I am looking for generating alpha numeric series of 3 digits as below:
0A0 to 9Z9
i.e. 0A0, 0A1,... 0A9, 0B0.... 9Z9
Is there any way in excel to generate the above sequence?
I tried many formulas but not able to do with alpha numeric.
You can try, in some cell:
=INT((ROWS($1:1)-1)/260)&CHAR(MOD(INT((ROWS($1:1)-1)/10),26)+65)&MOD(ROWS($1:1)-1,10)
Then fill down to a total of 2600 rows
EDIT: If you want to repeat the sequence after 2600 rows, then try:
=MOD(INT((ROWS($1:1)-1)/260),10)&CHAR(MOD(INT((ROWS($1:1)-1)/10),26)+65)&MOD(ROWS($1:1)-1,10)
It's easy!
You're trying to =CONCATENATE() a series made of random integers and random strings.
a RANDOM INTEGER from 0 to 9 --------------- =RANDBETWEEN(0;9)
a RANDOM CHARACTER from A to Z --------- =CHAR(RANDBETWEEN(65;90)) ¹
a RANDOM INTEGER from 0 to 9 --------------- =RANDBETWEEN(0;9)
¹ See more at ASCII Character Map
Final Formula:
=CONCATENATE(RANDBETWEEN(0;9);CHAR(RANDBETWEEN(65;90));RANDBETWEEN(0;9))
So, we used:
=CONCATENATE()
=RANDBETWEEN()
=CHAR()
An ASCII Character Map
Or if you're not interested in random numbers and chars, should be easy to create a column with the necessary numbers to generate a specific sequence.
But.... if you want all combinations possible using these variables, or in numbers, all the 2500 combinations possible, you should create a macro. I mean... it's doable using formulas, but is way easier to accomplish this using vba.

Extracting decimal numbers from a string in Excel

I've tried lots of searches for this but I'm still not coming up with anything that works.
I have a range of strings in Column A
Amend.Clause_1.1.AddMCQ
Amend.Clause_1.1.AddNo
Amend.Clause_1.1.AddRepeat
Amend.Clause_1.13.AddRepeat
Amend.Clause_1.13.AddTitle
Amend.Clause_1.13.AddUTQ
Amend.Clause_2.8.Heading_Edit
Amend.Clause_2.8.MCQ
Amend.Clause_2.8.Remove
Amend.Clause_4.26.AddUTQ
Amend.Clause_4.26.Heading_Edit
Amend.Clause_4.26.MCQ
Amend.Clause_5.15.AddMCQ
Amend.Clause_5.15.AddNo
Amend.Clause_5.15.AddRepeat
As you can see, the numbers always start in the same place, after the underscore "_" at position 13.
I need to extract the decimal numbers from these strings into a new column so I'm left with 1.1, 1.13, 1.14, 4.26 etc.
I've tried all sorts of combos of MID, LEFT, LEN, RIGHT but to no avail, trying to find the position of the last period.
Could anyone explain how to accomplish this? Ideally I'd like to do this without VBA.
Thanks
Here you are:
=VALUE(MID(A1,SEARCH("_",A1)+1,SEARCH(".",A1,SEARCH(".",A1,SEARCH("_",A1)+1)+1)-(SEARCH("_",A1)+1)))
Here's what inside =VALUE(MID(...)):
A1 - the whole string itself
SEARCH("_",A1)+1 - find the number starting position - right after "_".
SEARCH(".",A1,SEARCH(".",A1,SEARCH("_",A1)+1)+1)-(SEARCH("_",A1)+1) - find number length - position of second "." after first "." minus number starting position.
Try with three functions:
=MID(A1,14,FIND("#",SUBSTITUTE(A1,".","#",3))-14)
Try this - If the position of _ is not necessarily 13.
=MID(A1,FIND("_",A1,1)+1,FIND("¬¬",SUBSTITUTE(A1,".","¬¬",LEN(A1)-LEN(SUBSTITUTE(A1,".",""))))-FIND("_",A1,1)-1)
Or this if the _ is always 13
=MID(A1,14,FIND("¬¬",SUBSTITUTE(A1,".","¬¬",LEN(A1)-LEN(SUBSTITUTE(A1,".",""))))-14)
Use This:
=VALUE(TRIM(LEFT(SUBSTITUTE(RIGHT(A1;LEN(A1)-FIND("_";A1));".";REPT(" ";LEN(A1));2);LEN(A1))))
assuming value is in A1
Far from ideal, but with a shorter formula than the solutions offered so far:
=SUBSTITUTE(A1,".","_",3)
Catch is that formulae would then need to be converted to values, parsed with delimiter _ (being careful to ensure Column data format is Text) and surplus columns deleted.
When the string Amend.Clause_1.1.AddMCQ is in A1
=Find(".",A1,Find(".",A1)+1)
will give the position of the second decimal point, then you should be able to extract the decimal number.
The syntax is
FIND(find_text, within_text, [start_num])

When putting string that into Excel spreadsheet it puts to Scientific notation or rounds up

I am putting a string into excel. The string is often only numeric digits but can have alpha characters or hypens etc.
When I don't set the number format or set it like this
(Where xlSheet(0) is Excel.Worksheet)
xlSheet(0).Columns("N:N").EntireColumn.Columns.NumberFormat = "#"
It outputs in scientific notation.
When I use this code:
xlSheet(0).Columns("N:N").EntireColumn.Columns.NumberFormat = "0"
It rounds up the number to the nearest 100,000 so that the last five digits are 0's when they shouldn't be.
Should be: 1539648751235678942
But is: 1539648751235600000
The cells that have a hyphen or a letter aren't affected and work fine.
Any help would be greatly appreciated.
EDIT:
I add the data like this:
I loop through and put in xlSheet(0).Cells(i, 14) = rs!value_number
Where rs is my ADODB.Recordset
EDIT2: Herbert Sitz got it by adding an apostrophe before the text! Thanks everyone.
I think problem is that the number you're trying to enter can't be accommodated exactly by Excel. Excel has limitations on what numbers it display/represent because of the way numbers are stored internally. In Excel's case numbers are limited to 15 digit precision (see http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx ), which is not enough to represent your number.
You can enter the number as a string ("152..42") and all digits will be displayed, but you won't be able to perform exact mathematical operations with it.
For numbers, Excel can only handle 15 significant digits.
If you want to store a number that is more than 15 digits long without losing data, you have to store the data as text.
Doing what you've been doing will resolve the issue:
You can do either of the following to add your numbers as text:
xlSheet(0).Cells(i, 14).Numberformat = "#"
xlSheet(0).Cells(i, 14) = rs!value_number
Or
xlSheet(0).Cells(i, 14) = "'" & rs!value_number

Excel VBA stop Auto Rounding

I have price column in which the prices are displayed in 3 decimals and 4 decimals places, for example 123.456 or 123.4357.
So irrespective of the number of decimal places i want the value to be with only two decimals.
So i am selecting the column and in the VBA i am using Selection.NumberFormat = "0.00"
Which is resulting me the rounded value i.e when i format 123.456 and 123.4357 to 0.00 i am getting 123.46 and 123.44 but i want it to be 123.46 and 123.43.
So just wondering is there a way we can just trim the values instead of rounding.
Please give me some sample examples.
Thank you in advance.
Excel has a built-in function, trunc, that should do the trick for you.
This is what I placed in a1:b2.
123.456 =trunc(A1,2)
123.4357 =trunc(A2,2)
This will display
123.456 123.45
123.4357 123.43
You could treat the result as a string and extract the number of characters you need as in:
dim a as double
dim s as string
a = 123.4357
s = MID(a,1,FIND(".",a))&MID(a,FIND(".",a)+1,2)
I don't believe there is a built in mask that will truncate without rounding, you could instead use another column containing =TRUNC(A1, 2).

Resources