How to convert a "fraction" character into a decimal? - excel

I have a dataset where the format of data is like this:
10 ¾ AB 02/15/19
I'm trying to convert the ¾ into .75 so that the data looks like:
10.75 AB 02/15/19
I am thinking of trying to iterate through each character in the string, but once I do so how can I convert the ¾ into .75?

Simple set of string replacements:
Public Function numerify(s As String) As String
numerify = Replace(s, " " & ChrW(190), ".75")
numerify = Replace(numerify, " " & ChrW(188), ".25")
numerify = Replace(numerify, " " & ChrW(189), ".5")
End Function
NOTES:
We replace the space before the fraction as well as the fraction to get the desired result.There may be other uni-code "fractions" you may need to consider.

Related

Handing strings and copy paste variables

I have a code where I have an inputbox where I usually copy in some numbers.
I need vba to handle these and pass them to the cell as european format.
The format that comes in is US and comes as:
##,### ## ##
I use the following code to transform it to the format I want:
dim earned as variant
earned = Replace(Me.txtEarnedG.Value, ".", "")
earned = Replace(earned, ",", "")
earned = Replace(earned, " ", "")
earned = Left(earned, Len(earned) - 4) & "," & Right(earned, 4)
amount_earned = CDbl(earned)
This works perfectly and would transform
35,545 45 55 (US format with spaces, without a ".")
into
35545,4555 (EU format)
My issue comes when I try to transform a number that doesn't 2 digits in between one of the spaces like,
35,545 4 13 becomes 3554,5413 instead of 35545,0413
35,545 12 4 becomes 3554,5124 instead of 35545,1204
I was thinking of using Instr and right in some way. But I can't figure on how to introduce it in easily. (The number should always finish with 4 decimal places, but the rest may vary and can be bigger or smaller than thousands)
I think you could use the Split function as #Porcupine suggests and combine with Format to get the number format you wanted.
A simple example using your numbers below
Public Sub test()
Const TEST_NUM1 = "35,545 4 13"
Const TEST_NUM2 = "35,545 12 4"
Const TEST_NUM3 = "35,545 45 55"
FormatAsEU TEST_NUM1
FormatAsEU TEST_NUM2
FormatAsEU TEST_NUM3
End Sub
Public Function FormatAsEU(strNumber As String) As String
Dim varInput As Variant
varInput = Split(strNumber, " ")
FormatAsEU = Format(varInput(0), "#.#") & Format(varInput(1), "00") & Format(varInput(2), "00")
Debug.Print FormatAsEU
End Function
Debug Results:
> 35545.0413
> 35545.1204
> 35545.4555

How to trim spaces

I have text in Excel like this:
120
124569 abasd 12345
There are sapces both to the left and to the right side.
I copy this from Excel and paste as text. When I check this, it shows like this when I click on button.
Code:
abArray= abArray & "," & gridview1.Rows(i).Cells(2).Text
For k = 3 To 17
bArray= abArray& "," & Val(gridview1.Rows(i).Cells(k).Text)
Next
In abArray this shows as:
0, abasd ,12345,0,0,0,0,0
I want to remove/trim spaces both from left and right.
I have tried abArray.Trim() but this still show spaces.
If you want to remove all the spaces out of the end result consider String.Replace:
Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.
Example use:
Dim s As String = "0, abasd ,12345,0,0,0,0,0"
s = s.Replace(" ", "")
This would output:
0,abasd,12345,0,0,0,0,0
It may also be worth using a StringBuilder to join all your values together as this is good practice when looping as you are. At this point you could use String.Trim. This would preserve any spaces that are within your value. In order words it would only remove the spaces from the beginning and the end of the value.
Example use:
Dim sb As New StringBuilder
For k = 0 To 17
sb.Append(String.Format("{0},", gridview1.Rows(i).Cells(k).Text.Trim()))
Next
Dim endResult As String = sb.ToString().TrimEnd(","c)
endResult would output:
0,abasd,12345,0,0,0,0,0
You will have to import System.Text in order to make use of the StringBuilder class.
Use the VB.NET Trim function to remove leading and trailing spaces, change this one line of code:
abArray= abArray& "," & Val(Trim(gridview1.Rows(i).Cells(k).Text))
abArray.Trim() does not work because you did not give the Trim function anything to trim.
Try it like this
abArray = abArray & "," & gridview1.Rows(i).Cells(2).Text.Trim
For k = 3 To 17
abArray= abArray& "," & Val(gridview1.Rows(i).Cells(k).Text.Trim)
Next

Get the double part from string with vbscript

I want to create a barcode with vbscript that will be decoded from my company's erp (my company devides the nubers with 10000.
The barcode should have this type of look:
99XXXXXXXXXQQQQQQQQPPPPP where: X is my barcode, Q is quantity, and P is the price. With concatenation I have:
Result = 99 & [sheet$.BARCODE] & right("00000000" & quantity*10000, 8) & right("00000" & VBScript1*10000,5)
Now VBScript1 has this style, because it is used elsewhere in the program:
VBScript1 = "PRICE: "& FormatCurrency([sheet$.TIMH SAKOYLAKI]/[sheet$.SAKOYLAKI TWN]*1.3*(Round((40*CDbl(zyg))/CDbl([sheet$.GR/40 TEM]))),2)
so the output of VBScript1 is like Price: $0,40
Now my question is how to extract from the string the number only and then multiply it by 10000, in order to use it above?
For my example I want Price: Price: $0,40 to be used as 04000 in barcode.
Use Split() on $ to get the numerical part of (e.g.) "Price: $0,40", deal with the decimal comma, pad on the left:
>> s = "Price: $0,40"
>> p = CDbl(Replace(Split(s, "$")(1), ",", "."))
>> t = Right(String(5, "0") & p * 10000, 5)
>> WScript.Echo t
>>
04000
>>
Like Marc B said, I tried this and it worked nice and easy :)
Result =[...]right("00000" & CDbl((Mid(VBScript1,7,7)))*10000,5)[...]

Preserving leading 0's in string - number - string conversion

I am working on a macro for a document-tracking sheet at work. I use a button that prompts the user to enter in the document number and I'd like to specify a default number based on the following numbering convention. The first two characters of the document number are the latter two year digits (15 in this case), then there is a "-" followed by a five digit serialization.
My current code looks at the last-entered document and increments those last 5 characters, but chops off any leading zeroes, which I want to keep. This is an extraction of the code to generate this default number (assuming the variable "prevNCRF" is the previous document name found in the doc):
Sub codeChunkTester()
Dim prevNCRF, defNCRFNum As String
Dim NCRFNumAr() As String
'pretend like we found this in the sheet.
prevNCRF = "15-00100"
'split the string into "15" and "00100" and throw those into an array.
NCRFNumAr() = Split(prevNCRF, "-")
'reconstruct the number by reusing the first part and dash, then converting
'the "00100" to a number with Val(), adding 1, then back to a string with CStr().
defNCRFNum = NCRFNumAr(0) & "-" & CStr(Val(NCRFNumAr(1)) + 1)
'message box shows "15-101" rather than "15-00101" as I had hoped.
MsgBox (defNCRFNum)
End Sub
So can anyone help me preserve those zeroes? I suppose I could include a loop that checks the length of the string and adds a leading zero until there are 5 characters, but perhaps there's a better way...
Converting "00100" to a Double using Val turned it into 100, so CStr(100) returns "100" as it should.
You need to format the string to what you want it to look like:
defNCRFNum = NCRFNumAr(0) & "-" & Format(Val(NCRFNumAr(1)) + 1, "00000")
If you need to parameterize the length of the string, you can use the String function to generate the format string:
Const digits As Integer = 5
Dim formatString As String
formatString = String(digits, "0")
defNCRFNum = NCRFNumAr(0) & "-" & Format(Val(NCRFNumAr(1)) + 1, formatString)
Here is that loop solution I mentioned above. If anyone's got something better, I'm all ears!
prevNCRF = "15-00100"
NCRFNumAr() = Split(prevNCRF, "-")
zeroAdder = CStr(Val(NCRFNumAr(1)) + 1)
'loop: everytime the zeroAdder string is not 5 characters long,
'put a zero in front of it.
Do Until Len(zeroAdder) = 5
zeroAdder = "0" & zeroAdder
Loop
defNCRFNum = NCRFNumAr(0) & "-" & zeroAdder
MsgBox (defNCRFNum)
defNCRFNum = NCRFNumAr(0) & "-" & Format(CStr(Val(NCRFNumAr(1)) + 1), String(Len(NCRFNumAr(1)), "0"))

How to reduce all whitespace in a string to single spaces?

Using VB6 without any additional references such as Regex, how can I convert a string so that all whitespace in a string is reduced to single spaces?
eg.
" A B C D E"
would be converted to
"A B C D E"
Function NormalizeSpaces(s As String) As String
Do While InStr(s, String(2, " ")) > 0
s = replace(s, String(2, " "), " ")
Loop
NormalizeSpaces = s
End Function
(derived from: http://www.techrepublic.com/article/normalizing-spaces-in-vb6-strings/5890164)

Resources