Trying to write power of (small numbers) with VBA code - string

I can't figure out how to write power of to cells in Excel. I can use chr(185),chr(178) and chr(179) to display the power of 1,2 and 3 but I can't do any other number.

You have 2 choices:
using unicode characters
using cell formatting
I think the formatting method is to be preferred for normal use because it is pretty flexible (e.g. easy to superscript a whole algebraic expression like x^(n-1)).
However, if you are ultimately exporting to a CSV or some other format then Excel formatting will not carry over, in which case you should use method 1. In addition, using this method allows the superscript characters to be passed around in formulae, compared to actual cell formatting that cannot be.
Using Unicode characters
Use the ChrW(charcode) function.
charcode can be the decimal or hexadecimal number for the unicode character.
The characters are:
SuperscriptHex Dec
0 2070 8304
1 00B9 185
2 00B2 178
3 00B3 179
4 2074 8308
5 2075 8309
6 2076 8310
7 2077 8311
8 2078 8312
9 2079 8313
(reference to unicode table: http://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts)
You can use &H as a literal indicating the chars that follow should be interpreted as hexadecimal, like ChrW(&H2077).
Using cell formatting
A Range object has a Characters property that returns a Characters object that allows different Font objects to different parts of the string. Normally, Range.Font is applied to the whole text.
Note that Font also has lots of other properties you can apply against the substring, like bold/size etc.
Examples:
'Method 1 - Unicode
ActiveCell.Value = "x" & ChrW(&H2079)
'Method 2 - Formatting
With ActiveCell.Offset(1, 0)
.Value = "x9"
.Characters(2, 1).Font.Superscript = True '2,1 is a substring from 2nd char for 1 char
End With

Related

Need to Extract Sale Value & Currency Symbol from a text string (Excel Not Google Sheets) [duplicate]

20/11/2022 12:00:52 2 X 15.95 15.95 USD 57 5 689 5 689 1 4111 0 Amazing Lego Team
I need to get the position of No 4111 in the above text string, As an excel beginner any help will be greatly appreciated. Thanks.
All of the Text Strings will have a 4 digit number like 4111 which i have to get the position for.
Have tried using this formula to get four digit number in another column, LOOKUP(10^15,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),5)+0) but I am looking to get position instead.
I have tried using lookup but I could only go so far as a beginner.
Use the formula you provided =LOOKUP(10^15,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),5)+0) to identify the number, and use FIND to get the position of the number.
Cell A1="20/11/2022 12:00:52 2 X 15.95 15.95 USD 57 5 689 5 689 1 4111 0 Amazing Lego Team"
=FIND(LOOKUP(10^15,MID(A1,ROW(INDIRECT("1:"&LEN(A1))),5)+0),A1)
=58
This looks to be in a space (or some other character) delimited text string. If you have a bunch of rows of data like this, use the Text to Columns feature on the Data tab. Use the Delimited option and then click the check box next to space (or other if it's something custom not in the available options) and Excel will split the data into columns for you.
For your string example, if you know that your 4 digit is started 58 char from the beginning of the string, use =MID(A1,58,4) A1 is the cell with your string.
MID function returns a specific number of characters from a text string, starting at the position you specify, based on the number of characters you specify.
=LET(t,TEXTSPLIT(A1,," "),
FIND(FILTER(t,(LEN(t)=4)*(ISNUMBER(--(t)))),A1))
It splits the string at every space and filters the result for being of length of 4 characters and not being an error if converted to number.

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.

replace value in a string with different values where the string changes

I have a 5 character code that needs to be converted to a 4 character code. Additionally, the 5th character is either a 1, 2 or 5, and I need to convert them to 1, 5 or 9. As an example, if my query returns '20155', I need to translate that to '2159'. So far I have:
select substr(fieldname,1,1) || substr(fieldname,3,2) || substr(fieldname,-1,1) as newfieldname
This converts it from 5 to 4 characters. What I don't know how to do is also change the last character to the new value as described above.
A sample of what I want it to achieve is:
20141 becomes 2141
20142 becomes 2145
20145 becomes 2149
20151 becomes 2151
20152 becomes 2155
20155 becomes 2159
Any assistance would be appreciated. I am not a computer programmer - I am a functional analyst that has to validate over 500,000 rows of data, each row containing the fieldname above.
You can use "case when ":
Case when 2 then 5 , etc...

Concatenante and include leading zeroes

So currently I am pulling numbers from a spreadsheet that automatically formats their numbers into a certain amount of digits using special number formatting, so that it outputs with leading zeroes.
Example:
(zipcode formatting) 6 digits -
12345 becomes 012345
(Chinese (PRC) formatting) 6 digits -
12 becomes 000012
(Chinese (Taiwan) formatting) 3 digits -
0 becomes 000
My concatenate would put them together with periods between them; however, it does the base numbers instead of the formatting it should be doing.
=CONCATENATE(A1,".",B1,".",C1)
012345 000012 000 becomes 12345.12.0 instead of 012345.000012.000
Is there a function or something that will work to make it pull the converted special number instead of the one typed? It should ALWAYS be 6 digits.6 digits.3 digits.
Try this:
= TEXT(12345,"######000000") & "." & TEXT(12,"######000000") & "." & TEXT(0,"###000")

Converting number strings to Values in Excel

After scraping a website I ended up with values with dashes in the data like 7-6, 12-5, 3-12. I was able to separate the data into to their own columns making the previous examples being 7, 6 , 12,5 3,12, but the data has turned from values that you can add and subtract to something like a string. Is there a way to make the strings into values.
0 15
1 2
5 6
4 8
3 2
2 1
If i go through each cell and double click the strings it converts to values, but I cant do that to 55000 cells.
you can convert a text string that represents a number to number format using the Value() formula
Example:
B1 = Value(A1))
if A1 = 15 in text format
You can select the whole columns and convert to number. Go to menu Data > Convert and then, follow the wizard.

Resources