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)
Related
I would like to have a quick single line VBA code to pad a number in string form to always have a length of a multiples of 3's.
For example:
"12" becomes "012"
"1234" becomes "001234"
"1234567" becomes "001234567"
"12345678912345678" becomes "012345678912345678"
and so on.
The number string will not have any fraction or decimal points, so not to worry about that.
The length of the resulting string should be a multiple of 3 by adding 0's to the Left.
The number string should remain a string throughout the coding and should not be converted into a number using a numeric function such as Val, as the original string could be very long over 150 digits and using a numeric function could truncate or round the number and give undesirable results.
I had come up with the following as a possible solution by checking the string length but feel there is a better more efficient way of doing it in one (1) coding statement.
Result = String((3 - (Len(MyString) Mod 3)) Mod 3, "0") & MyString
After several attempts and your ideas, I have come up with the following using a single Mod Function:
MyString = String((Len(MyString) * 2) Mod 3, "0") & MyString
In general, a general formula would be:
MyString = String((Len(MyString) * (Multiples-1) ) Mod Multiples, "0") & MyString
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.
I need help with extracting 5-digit numbers only from one column to another in Excel 2010. These numbers can be in any position of the string (beginning of the string, anywhere in the middle, or at the end). They can be within brackets or quotes like:
(15478) or "15478" or '15478' or [15478]
I need to ignore any numbers that are less than 5 digits and include numbers that start with 1 or more leading zeros (like 00052, 00278, etc.) and ensure that leading zeros are copied over to the next column. Could someone help me with either creating a formula or UDF?
Here is a formula-based alternative that will extract the first 5 digit number found in cell A1. I tend to prefer reasonably simple formula solutions over VBA in most situations as formulas are more portable. This formula is an array formula and thus must be entered with Ctrl+Shift+Enter. The idea is to split the string up into every possible 5 character chunk and test each one and return the first match.
=MID(A1,MIN(IF(NOT(ISERROR(("1"&MID(A1,ROW(INDIRECT("R1C[1]:R"&(LEN(A1)-4)&"C[1]",FALSE)),5)&".1")*1))*ISERROR(MID(A1,ROW(INDIRECT("R1C[1]:R"&(LEN(A1)-4)&"C[1]",FALSE))+5,1)*1)*ISERROR(MID(A1,ROW(INDIRECT("R1C[1]:R"&(LEN(A1)-4)&"C[1]",FALSE))-1,1)*1),ROW(INDIRECT("R1C[1]:R"&(LEN(A1)-4)&"C[1]",FALSE)),9999999999)),5)
Let's break this down. First we have an expression I used twice to return an array of numbers from 1 up to 4 less than the length of your initial text. So if you have a string of length 10 the following will return {1,2,3,4,5,6}. Hereafter the below formula will be referred to as rowlist. I used R1C1 notation to avoid potential circular references.
ROW(INDIRECT("R1C[1]:R"&(LEN(A1)-4)&"C[1]",FALSE))
Next we will use that array to split the text into an array of 5 letter chunks and test each chunk. The test being performed is to prepend a "1" and append ".1" then verify the chunk is numeric. The prepend and append eliminate the possibility of white space or decimals. We can then check the character before and the character after to make sure they are not numbers. Hereafter the below formula will be referred to as isnumarray.
NOT(ISERROR(("1"&MID(A1,rowlist,5)&".1")*1))
*ISERROR(MID(A1,rowlist+5,1)*1)
*ISERROR(MID(A1,rowlist-1,1)*1)
Next we need to find the first valid 5 digit number in the string by returning the current index from a duplicate of the rowlist formula and returning a large number for non-matches. Then we can use the MIN function to grab that first match. Hereafter the below will be referred to as minindex.
MIN(IF(isnumarray,rowlist,9999999999))
Finally we need to grab the numeric string that started at the index returned by the MIN function.
MID(A1,minindex,5)
The following UDF will return the first five digit number in the string, including any leading zero's. If you need to detect if there is more than one five digit number, the modifications are trivial. It will return a #VALUE! error if there are no five-digit numbers.
Option Explicit
Function FiveDigit(S As String, Optional index As Long = 0) As String
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Pattern = "(?:\b|\D)(\d{5})(?:\b|\D)"
.Global = True
FiveDigit = .Execute(S)(index).submatches(0)
End With
End Function
As you may see from the discussion between Mark and myself, some of your specifications are unclear. But if you would want to exclude decimal numbers, when the decimal portion has five digits, then the regex pattern in my code above should be changed:
.Pattern = "(?:\d+\.\d+)|(?:\b|\D)(\d{5})(?:\b|\D)"
I just wrote this UDF for you , basic but will do it...
It will find the first 5 consecutive numbers in a string, very crude error checking so it just says Error if anything isn't right
Public Function GET5DIGITS(value As String) As String
Dim sResult As String
Dim iLen As Integer
sResult = ""
iLen = 0
For i = 1 To Len(value)
If IsNumeric(Mid(value, i, 1)) Then
sResult = sResult & Mid(value, i, 1)
iLen = iLen + 1
Else
sResult = ""
iLen = 0
End If
If iLen = 5 Then Exit For
Next
If iLen = 5 Then
GET5DIGITS = Format(sResult, "00000")
Else
GET5DIGITS = "Error"
End If
End Function
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 a newbie to programming and need some help with the basics.
I have a function which takes in an integer value. I want to be able to grab the first digit (or the first and second digits in some cases) of this integer and do something with it.
What is the best way in VB.NET to get the first digit of an integer (or the first and second)?
firstDigit = number.ToString().Substring(0,1)
firstTwoDigits = number.ToString().Substring(0,2);
int.Parse(firstDigit)
int.Parse(firstTwoDigits)
and so forth
I'm not well versed in VB syntax, so forgive me for the syntax errors:
dim i as integer
while i >= 10
i = i \ 10
end while
msgbox "i = " & i
Note, this prints the "first from the left" digit. Like, for "12345" it would print "1".
If you need the digits starting from the end of the integer, just get the modulu result for the tens or the hundreds, according to how many digits you need.
Dim n As Integer
n Mod 10
for the first digit, or:
n Mod 100
for the second and first digits.
If you need the first and second digits from the beginning of the number, there is another answer here which will probably help you.
for first digit you can use:
Dim number As Integer = 234734
Dim first = number.ToString.ToCharArray()(0)
for second digit you can use:
Dim number As Integer = 234734
Dim second = number.ToString.ToCharArray()(1)
This would work. You can use Math.ABS, absolute value, to eliminate negative. The number from left could be replaced by a function if you are using logic, like the overall length of the number, to determine how many of the leading characters you are going to use.
Dim number As Integer = -107
Dim result As String
Dim numberFromLeft As Integer = 2
result = Math.Abs(number).ToString.Substring(0, numberFromLeft)
This results in 10 it is a string but converting it back to a number is easy if you need to. If you need to keep track if it was positive or negative you could use the original value to apply that back to you parsed string.