I want to make for ever "1" in the text box do this code.
Instead it wont just see if there's any 1's or 2's
I tried this:
select case text box
case "1"
'do code
case "2"
'do code
end select
so if i typed 132421
it would not run my code i put for 1 and 2 it has to be just a 1 or a 2 in the text
It sounds like you want your code to execute multiple times for each occurrence of the digit 1, 2, etc. This loop would accomplish that:
For Each digit as Char in MyTextbox.Text
If digit = "1" Then
' do code
Else If digit = "2"
' do code
End If
Next
I'm guessing you want it to run specific code for each instance of those numbers? Like Nate said.
You will need to do a loop to run through each character in the textbox.
For Each Letter as Char in Textbox1.Text
Select Case Letter
Case "1"
' Do Code
Case "2"
' Do Code
End Select
Next
If you typed 132421, it would loop 6 times, though using only ones and twos only 4 will activate any code in this example.
Related
I have a string called Policynumber
I need to know if it begins with a 1 or a 2 so I can create an if statement for if it starts with 1 do something and if it starts with 2 to do something else.
I keep finding ways to do this for string text but not for a variable.
I have tried doing the following:
If policynumber Like "1*" Then
display.text = policynumber
End If
I am simply looking for possible ways to know what the first character is and therefore determine if it's a 1 or a 2. When I try using the variable name or even a textbox.text I get no result in the display textbox so I know it's not working.
I would use VBA's ASC() function for this purpose. This function returns the ASCII code of the first character of a string. ASCII for the character 1 is 49 and 2 is 50. Therefore ...
Dim PolicyNumber As String
Dim n As Integer
PolicNumber = "1ABC-45678910"
n = ASC(PolicyNumber)
MsgBox "Policy number starts with a " & Chr(n)
For testing the result you can use either If n = 49 Then or If Chr(n) = "1" Then.
I have a spreadsheet that has multiple lines within a cell, all with line breaks.
e.g.
Name: a
Age: 1
University: 1
Degree: 3
Year: 3
I am looking to extract (in this example) the University infomation that is contained within the cell and copy it into another cell in another column.
There are about 1000 records in my document so to copy and paste by hand will be time consuming.
Any help will be appreciated
Cheers
Joe
You could do this with an Excel formula.
Assuming your data is in column A, and you want the extraction in column B, and assuming you put a title in row 1, you could do as in the following image:
(Note that I have a semi-colon in the formula as list separator, use comma instead)
The formula in B2 is:
=MID($A2, FIND(B$1, $A2) + LEN(B$1),
FIND(CHAR(10), $A2 & CHAR(10), FIND(B$1, $A2)) - FIND(B$1, $A2) - LEN(B$1))
The formula has some duplication; here are some of the parts explained:
FIND(B$1, $A2) returns the position of the title in the text
FIND(B$1, $A2) + LEN(B$1) returns the position of what follows that title in the text
FIND(CHAR(10), $A2 & CHAR(10), FIND(B$1, $A2)) returns the position of a newline character following the title, making sure that if none is present, a position beyond the string length is returned
As long as you put the column titles to whatever sub-string you are looking for, you can copy/drag the same formula to other columns and rows.
If there is a single break between each line, then in B1 enter:
=TRIM(MID(SUBSTITUTE($A1,CHAR(10),REPT(" ",999)),COLUMNS($A:C)*999-998,999))
This assumes that:
the university line is the third line
you want the entire line
I'm providing an answer even though you haven't provided what attempts you've made so far, which is how questions on this site usually work...but today I'm feeling generous :)
Use a combination of MID and FIND formulas, like the following:
=MID(A1,FIND("University",A1),FIND("Degree",A1)-FIND("University",A1)-2)
I put your example text in Cell A1 for my test, and it returned University: 1. This however will only work if University is always followed by Degree in the text strings.
The other method would be to replace the last part of your MID statement (the part asking for length to return) with the exact number of characters to return, which in this case would be 13, like the following:
=MID(A1,FIND("University",A1),13)
This assumes that the integer associated with University is always 1 character in length.
Either way, a combination of the above two formulas should get you what you need. VBA should not be necessary in this case.
Lines in a cell value are separated by the line feed character vbLf, so to extract the information out of the cell value you can use String-Functions Mid(...) and InStr(...):
Dim cellValue as String
Dim extracedValue as String
Dim keyWord as String
Dim posStart as Integer, posEnd as Integer
extractedValue = "" ' Not necessary, but I prefer initialized variables
cellValue = ActiveSheet.Cells(1,1).Value ' Put your cell here
keyWord = "University" ' Put your keyword here
posStart = InStr(1, cellValue, keyWord) ' Find keyword in the string
If (posStart > 0) Then
posEnd = InStr(posStart, cellValue, vbLf) ' Find next line feed after the keyword
If (posEnd > 0) Then
extractedValue = Mid(cellValue, posStart, posEnd - posStart) ' Extract the value
End If
End If
I haven't tested the code, but you should get the idea.
I get a spreadsheet but sometimes they dont give me all the digits I need when running my macro. So i am trying to find a way to count how many digits are in a cell and then use msgbox to tell me to add a digit to the cell.
If Len(A2) <> 7 Then
MsgBox ("Add # to the end ")
Exit Sub
End If
I was also wondering if there is a way to use an input box to add the number to the end of the value in excel. Like if the numbers are 123456 i can put 7 into the input box and it changes the cell value to 1234567
This should work for Characters, But if other Characters are in the cell besides just numbers it counts everything:
If Len([A2]) <> 7 Then
[A2] = [A2].Value & InputBox("Add # to the end ")
End If
I figured out how too get only numbers in a text box with is code:
Dim smessage As String = String.Empty
If Not IsNumeric(Student_IDTextBox.Text) Then
smessage += "The ID must be Numeric!" + Environment.NewLine
End If
But I would like this textbox to have 2 letters and 3 numbers, do you know what the best way to programme this in vb?
please try masked textbox with custom mask.
set mask like- LLL00.
refer this link
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx
This is certainly not the best way, but I do not know if you will ever find the best way because best is subjective and often dependent on more than one situation as well as opinion.
Assuming a text box with the name txtinput and a label where you will be displaying the results named lblMessage and assuming you are using ASCII character input:
In the TextChanged event of txtinput you could have the following:
'Check if the length is greater than five, if it is truncate it.
If txtinput.Text.Length > 5 Then
txtinput.Text = Mid(txtinput.Text, 1, 5)
txtinput.Select(txtinput.Text.Length, 0)
End If
'counters for letters and numbers
Dim letters As Integer = 0
Dim numbers As Integer = 0
'Parse and compare the input
For Each c As Char In txtinput.Text
If Asc(c) >= 48 And Asc(c) <= 57 Then 'ASCII characters for 0-9
numbers += 1
ElseIf Asc(c) >= 65 And Asc(c) <= 90 Then 'ASCII characters for A-Z
letters += 1
ElseIf Asc(c) >= 97 And Asc(c) <= 122 Then 'ASCII characters for a-z
letters += 1
End If
Next
If letters = 2 And numbers = 3 Then
lblMessage.Text = "Correct Format"
Else
lblMessage.Text = "Incorrect Format"
End If
Using Linq:
If txtinput.Text.Length > 5 Then
txtinput.Text = Mid(txtinput.Text, 1, 5)
txtinput.Select(txtinput.Text.Length, 0)
End If
If txtinput.Text.Count(Function(x As Char) Char.IsLetter(x)) = 3 And txtinput.Text.Count(Function(x As Char) Char.IsNumber(x)) = 2 Then
lblMessage.Text = "Correct Format"
Else
lblMessage.Text = "Incorrect Format"
End If
If the ID must be 3 numerals and 2 characters than there is probably also a pattern to it (as with many license plates) and more important than the mere character type count. A masked text box is one way, counting the numerals and counting the letters is another.
If there is a pattern such as AAA-NN or AAANN, you could split the ID into 2 inputs one alpha, one numeric. This is often done with IDs in patterns such as a (US) social security number (NNN-NN-NNNN). RegEx could also probably be used to test the pattern.
If this is a login or otherwise a database app, rather than write too much code to simply test the pattern test the entry. You could collect whatever they type and do a simple query to see if the ID exists, which after all is far more important than the pattern.
A label on the form can tell them to use ###AA or whatever, but it seems silly to test the pattern and report a pattern error when you can simply tell them when it is invalid. After all, even if it has the correct pattern, it can still be an invalid ID.
Hey all i am probably overthinking this but how can i check the textbox for either a 655 or 699 in the first 3 numbers in the textbox?
The current code i how now works but displays an error if (im guessing) it doesnt find the other number in the textbox as well:
If Microsoft.VisualBasic.Mid(txtNumber.Text, 1, 3) <> 655 Or Microsoft.VisualBasic.Mid(txtNumber.Text, 1, 3) <> 699 Then
'ERROR
end if
What would i be doing incorrectly?
David
Like so:
If Left(txtNumber.Text, 3) = "655" OrElse Left(txtNumber.Text, 3) = "699" Then
' good?
End if
Although it looks like you might want an error if it's not either one, in which case just wrap the two test above in paran's and put a Not before them.
First, you're going to want to use Left, not Mid if it's the first 3 characters.
Second, you're checking a string against an integer.
Third, you're checking if it's not those 3 characters when I'm guessing you want to check if they are equal, so you'll want to change that, as well.
try
If Mid(txtNumber.Text, 1, 3) <> "655" And Mid(txtNumber.Text, 1, 3) <> "699" Then
'Code
End If