I want to play a little with the Textbox. how do I count the items of a textbox? Example: The first line contains the characters: 12 14 16 18 so there are 4 characters but i have one code, and show me 8 character, not 4. how do I display this Count in another textbox? So how do all the characters look into? limited space or comma.
secondTextBox.Text = firstTextBox.Text.Where(Function(x) Not Char.IsWhiteSpace(x)).Count()
this code takes every single digit, I want to take it as a integer. i.e. 12, 14, 16, 18, as a integer.
Try something like this:
Dim number As Integer
secondTextBox.Text = firstTextBox.Text.Split(", ".ToCharArray, StringSplitOptions.RemoveEmptyEntries).Where(Function(x) Integer.TryParse(x, number)).Count
There are lots of ways of doing this, but the simplest which doesn't take account of humans would depend on two things
If all the numbers are on one line and just contain one space between the numbers and no spaces after the last number, just count the number of spaces
To do this in your code you would write
secondTextBox.Text = (firstTextBox.Text.Where(Function(x) Char.IsWhiteSpace(x)).Count() + 1).ToString
If the numbers are on separate lines with no blank lines inbetween the numbers or before or after the numbers, then you would use
secondTextBox.Text = firstTextBox.Lines.Count
When it comes to text searches it may help to take a look at RegEx (Regular Expressions). From your question, it seems you want to count the number of words in a user input. If so, check this question and its first answer.
Related
I need to generate statement numbers in Access 2016. These numbers need to be 5 digits long, and where the statement number is less than 5 digits, I need to pad the number with leading zeros.
As a starting point, the first statement number will not be padded. So if the last statement number is 96, the next statement number needs to be 00097.
Is it possible (I assume in VBA) to cater for both cases, where the number is 5 digits long, or where it is less.
I was thinking of firstly stripping the leading zeros from the last statement number (if any existed), incrementing the remaining number by one, and then adding leading zeros to make the length of the number 5, but I'm not sure of the most efficient way of doing this.
If it has a leading zero, it will not be a number, but a String. However, as far as it is not quite a bit difference, something like this works:
Sub TestMe()
Dim cnt As Long
Dim myString As String
For cnt = 98 To 120
myString = Format(cnt, "00000")
Debug.Print myString
Next cnt
End Sub
This is what you get in the immediate window:
00098
00099
00100
00101
Yes, just first convert to a number:
NextValue = Format(Val(CurrentValue) + 1, "00000")
You could also use:
NextStateStr = Right("00000" & LastStateNum + 1, 5)
I am needing to create a formula that removes the leading 1 from a 11 digit phone number as shown below.
I would also like the formula to exclude any 10 numbers that doesn't have a leading 1.
I am trying to have my list all the same with just 10 digits.
Try this if you are sure the value are clean and no spaces and dash
=IF(LEN(A1)=11, RIGHT(A1,10),A1)
Try,
=IFERROR(--TEXT(--RIGHT(E2, 10), "[>1999999999]0000000000;;;"), "")
Use this simple formula:
=--Right(E2,10)
The Right() Function turns it into text so the -- turns it back to a number.
(Current Sort Sample:)
2-1203-4
2-1206-3
2CM-
3-1610-1
3-999
…
AR3021-A-7802
AR3021-A-7802-1
B43570-
B43570-3
I am working on an 8000+ record parts list. The challenge I am running into is that different manufactures of the parts are using many varying formats for their part numbers. “Part Number” is the field I wish to sort my entire worksheet on. (There are about 10 columns of data in this worksheet.)
My methodology for attacking this challenge was to count the number of characters to the left of any “-“ and count the total number of numeric characters in the field. (I also set “Part Numbers” that started with a non-numeric character to a count value of 99 for both count calculations so those would sort after the numeric values.) From this, I was able to sort on the values to the left of the “-“ using .the MIN of the two counts. (My “Part Numbers” are in Column B and I have a header row which means that my first “Part Number” is in cell B2.)
This method worked up to a point. My challenge is that I need to subsequently sort values after the “-“ character as is illustrated by the erroneous sort of “3-1610-1” being followed by “3-999”
One of the limitations I see is that sorting with Data Sort only gives three columns to sort on. To sort on just the characters to the left of the “-“ is costing me those three columns. So, I am unable to repeat the whole process of counting values after the “-“ character and subsequently sorting with Data Sort after running the primary sort.
Has the sort of many differing formats of a field such as “Part Number” been solved? Is there a macro that can be applied to this challenge? If so, I would be grateful for your input.
This data is continuously updated with new part numbers so the goal here is to be able to add those additional part numbers to the bottom of the worksheet and use a macro to correctly resort the appended list.
For the record, I am not married to my approach. After all, it didn’t solve my challenge!
Thank you,
Darrell
Place this procedure in a standard code moule:
Public Sub PartNumberSortFormat()
Dim i&, j&, f, vIn, vOut
vIn = [b2:index(b:b,match("*",b:b,-1))]
vOut = vIn
For i = 1 To UBound(vIn)
f = Split(Replace(vIn(i, 1), " ", ""), "-")
For j = 0 To UBound(f)
If IsNumeric(f(j)) Then
f(j) = Format$(f(j), "000000")
Else
f(j) = String$(6 - Len(f(j)), "0") & f(j)
End If
Next
vOut(i, 1) = Join(f, "-")
Next
Columns(1).Insert xlToRight
[a1] = "SORT COLUMN"
[a2].Resize(UBound(vOut)) = vOut
Columns(1).EntireColumn.AutoFit
End Sub
After running the procedure, you will notice that it has inserted a new column A on your worksheet and your data has been scooted over to the right by one column.
This new column A will contain a copy of your part numbers, reformatted in such a fashion to allow normal sorting.
Now select all of the data INCLUDING this new column A and sort A-Z on column A.
After the sort, you may delete the new column A.
This works by padding all characters surrounding dashes to six zeroes.
My Thoughts:
Excel 2010 onwards lets you sort using as many columns as you like. (Not sure about 2007). Don't know which version you have!
You could use the formula SUBSTITUTE to remove all "-" from the part number then sort on the number that remains, which gives you a order more like the one you are wanting.
eg
Value =SUBSTITUTE(B2,"-","")
3-15 315
3-888 3888
3-999 3999
3-1610 31610
3-2610 32610
3-1610-1 316101
3-2610-3 326103
It's not exactly what you need though!
Combine this with other formulas (or a VBA function) to manipulate you part number to be more sortable.
You could use FIND to find the position of the first "-" and extract the numbers before it into one column.
Similarly using FIND, MID and LEN you could extract the numbers between a part number two "-".
I suspect if will be best to write a VBA function to convert a part number into a "sortable value". This might splitting the part number into it's component bits (ie each bit being the text between the "-")
(VBA function split might useful for this. It creates an array.
If you know the formats of ALL the part numbers that can be delivered, you can code accordingly.
I suspect you code will take a numbers like and convert them as shown
AB123-456-78 AB12300456007800
AB12-45-7 AB12000450007000
AB12-45 AB12000450000000
ie padding with zeros each component of the part number
The key to sorting the TEXTUAL values into the order you want is understanding how textuals values get sorted! Do some experiments. Then create zero (or "9") padded numbers that sort the numbers as you required.
I hope this helps.
While not a technical answer to the Excel question, I am a logistician working with extremely large data sets of part numbers - always varying in format. The standard approach used in my field is to "ignore" (remove) special characters from the P/N and append the (clean) P/N to the 5-digit CAGE (manufacturer) code to create a "unique" CAGE + (clean) P/N code for sorting, lookup, etc. Create a column for that construct.
I want to take an integer and display it in digits no matter what the value, e.g Int = 1 to be displayed as 001. No the integer will never be more than 3 digits. Its probably simple and im just missing the obvious.
CStr(Format(Int, 000)
I am concatenating the result as a string. Thanks
This will always show three digits:
MsgBox Format(iMyInt, "000")
I think all you needed was double-quotes:
CStr(Format(Int, "000")
I am having a value "90672.40 91796473.18" in one variable.
I need to store the "90672.40" in an another variable and remaining in another variable.
and also I need to retrieve a numbers from the 40th position to end.
kindly help me..
for your information,
I am having a variable named lsline.
Value of lsline is:
lsline = 15-OCT-08 OTHERS 90672.40 91796473.18
i need to retrieve a number from 40th position, but i dont want the numbers after the space, i just want "90672.40" to store in a another variable.
Use Substring to get part of a string if you don't want to use Split. The following returns 8 characters starting from number 40:
lsline.Substring(40, 8)
Just split into an array
Dim values = Isline.Split(" ")
Dim number = CDec(value[2])