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)[...]
Related
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
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.
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"))
I'm just wondering if there is a way to replace all blank spaces after a certain character in a string. Basically a string like;
str = "This is a test - 1, 2, 3, 4, 5"
I would like essentially remove all of the spaces after the '-'. I understand how to do the
replace(str," ","")
but that will remove every space, and I want to keep the 'This is a test -" intact for readability to the user. I have used
Instr(str,"-")
to get the position of that character but do not know how to then enact the replace function on the rest of the string starting from that point.
I would used regex but if you just want to use string functions I think this is what you are asking
str = "This is a test - 1, 2, 3, 4, 5"
chrPos = Instr(str,"-")
lStr = Left(str, chrPos + 1)
rStr = Replace(str , " " , "", chrPos+1)
wscript.echo lStr & rStr
The result is This is a test - 1,2,3,4,5
VBScript REPLACE function has a start parameter but it does not work the way you expect. You must therefore isolate the portion on which you want to perform the replacement:
Dim parts
parts = Split("This is a test - 1, 2, 3, 4, 5", "-", 2) ' returns array with 2 items (max)
Debug.Print parts(0) & "-" & Replace(parts(1), " ", "") ' replace and concatenate
I have a string and I want to replace every space in this string with a + I tired this by using:
tw.Text = strings.Replace(tw.Text, " ", "+", 1)
But that didn't worked for me...any solutions?
For example the string could look like:
The answer of the universe is 42
Use strings.ReplaceAll
tw.Text = strings.ReplaceAll(tw.Text, " ", "+")
If you're using an older version of go (< 1.12), use strings.Replace with -1 as limit (infinite)
tw.Text = strings.Replace(tw.Text, " ", "+", -1)
Documentation on strings.Replace(): http://golang.org/pkg/strings/#Replace
According to the documentation, the fourth integer parameter is the number of replacements. Your example would only replace the first space with a "+". You need to use a number less than 0 for it to not impose a limit:
tw.Text = strings.Replace(tw.Text, " ", "+", -1)
If you are using this in a query, the QueryEscape method provided by net/url is the best solution: https://golang.org/pkg/net/url/#QueryEscape
import "net/url"
tw.Text = url.QueryEscape(tw.Text)