Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a textbox of maxLength 8. the 1st two characters must be "PM" or "00". I tried split(), but didnt work.
Use substring() method
Dim s As String = TextBox1.Text.Substring(0, 2)
If s = "PM" Or s = "00" Then
MessageBox.Show("good!")
Else
MessageBox.Show("bad!")
End If
Or you can use StartsWith()
If TextBox1.Text.StartsWith("PM") OR TextBox1.Text.StartsWith("00") Then
'Do something
End If
Another option is to use regular expressions:
Dim re As New Regex("PM|00")
If re.IsMatch(TextBox1.Text) Then
'do something
End If
The benefit is that when you decide what to do with the other 6 characters, you can modify the above to capture and return those (in full or part), without having to rewrite your code. You can even process multiple occurrences of PM|00 in one string and capture them all.
Useful resource, a Regex sandbox:
https://www.regex101.com/
Maybe you can try this:
if textbox1.text like "PM*" or textbox1.text like "00*" then
Do something
else msgbox("You don't have pm or 00 to start with!")
end if
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to convert an old Fortran program with a lot of nested goto statements to VBA but I am getting error mentioned in the title. The way I am converting is each statement, I made it a function and instead of goto then i call the function or statement, but apparently that's not a proper way of doing it. What else can I do? Thanks in advance.
If you cannot see where the endless loop is, try adding a public counter to all of the functions. If it exceeds a given value, stop the program. Then try to debug with F8 to see the endless loop. I mean something like this:
Option Explicit
Public counter As Long
Public Sub TestMe()
While True
FunctionSomething
Wend
End Sub
Public Function FunctionSomething()
counter = counter + 1
If counter > 100 Then Stop
End Function
Now if you run TestMe it would stop on the 100th iteration.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a list of data, distributed with a non-homogeneous pattern, on 3 columns. The challenge is to write a vba code "smart enough" to copy and paste all these numbers on one column, putting them in order, one below the other one: 1, 2, 3, 4, 4.1, 4.2 etc.. without missing any of them.
Could someone help me in this task? Because I see a lot of complexity and I have no idea how to manage it. Thank you!
As I understand it, you're looking to order this in a specific way that isn't necessarily how Excel would sort by default. The values look like version numbers or nested task IDs.
Stack Overflow isn't really a "solve your problem for you" kind of place, but I can definitely get you started. Here's what you'll need to do:
Get all the values, preferably into a Collections object. Make sure to omit blank cells.
Convert each value into a new format which a) is sortable, and b) can be reverted to the original format. For example, let's say these are version numbers, and any given number can be as high as 999, and there can be up to 4 items (e.g. 123.10.15.9 is valid, as is 9.5). Convert these to the form 000000000000 (that's 12 0s). You can do this by using Split() to divide the values using the "." delimiter, then padding the value out. This might look something like this:
.
'Converts 1 => 001000000000
' 1.1 => 001001000000
' 2.4.7 => 002004007000
' 65.339.1 => 065339001000
Function ConvertToSortableVersionNumber(value As String) As String
'Add all the parts of the value (. delimited) to a collection
Dim vnPart As Variant
Dim error As Boolean: error = False
Dim Parts As Collection
Set Parts = New Collection
For Each vnPart In Split(value, ".")
'Make sure this can actually be formatted as needed.
If Len(vnPart) > 3 Then
error = True
Exit For
End If
'Add this part to the Parts collection
Parts.Add vnPart
Next vnPart
'Now make sure there are 4 parts total
If Parts.Count > 4 Then error = True
'Check for errors
If error Then
'There is an error. Handle it somehow
End If
'If there are less than 4 items in the collection , add some
While Parts.Count < 4
Parts.Add ("000")
Wend
'Now, assemble the return value
Dim retVal As String
Dim item As Variant
For Each item In Parts
retVal = retVal & Right(String(3, "0") & item, 3)
Next item
'All set!
ConvertToSortableVersionNumber = retVal
End Function
Sort the collection (this page has a good example of sorting a collection in memory).
Create an array with new values converting back to the original format (much easier since the inputs will be highly standardized).
Write the array to a new range--and you're done!
Take a stab at it. I think you'll find that Stack Overflow is much more helpful once you can show the work you've already done, what you've tried, and what specifically is giving you trouble.
Good luck!
If you have 2010 or later the following formula will do it:
=IFERROR(SUBSTITUTE(SUBSTITUTE(AGGREGATE(15,6,--SUBSTITUTE($A$1:$C$10,".","000")*--(1 & REPT("0",9-LEN(SUBSTITUTE($A$1:$C$10,".","000")))),ROW(1:1)),"0000",""),"000","."),"")
If you have 2007 or earlier than it will need to be an array formula:
=IFERROR(SUBSTITUTE(SUBSTITUTE(SMALL(IF($A$1:$C$10<>"",--SUBSTITUTE($A$1:$C$10,".","000")*--(1 & REPT("0",9-LEN(SUBSTITUTE($A$1:$C$10,".","000"))))),ROW(1:1)),"0000",""),"000","."),"")
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when leaving edit mode.
Column E is the first formula and Column F is the second.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to write a macro that inserts "!!!" at the beginning of a textbox if there is a "XX" located in any part of that text box. Ideally the macro will run this procedure for every textbox in the presentation, but I can figure out how to loop it through if someone can help me with the basic procedure.
For example, a text box with the following text:
I ate XX hamburgers on XX/XX/20XX
would become
!!!I ate XX hamburgers on XX/XX/20XX
I hope this can help.
Sub test()
Dim TestString As String
TestString = "I ate XX hamburgers on XX/XX/20XX"
Variable = InStr(1, TestString, "X")
If Variable > 0 Then
output = "!! " & TestString
End If
Debug.Print output
End Sub
Here TestString = your input string
The InStr function tests if "X" is present in the string, if it is then "!!" is joined to the variable "Output"
This should be quite easy to adapt?
i hope this will help you.
Sub test()
Dim s As String
s = "Test XX"
If InStr(1, s, XX, vbTextCompare) Then
s = "!!!" + s
End If
MsgBox s
End Sub
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am making a quiz with an answer box and a check my answer box.
I put the code in and then when I put in the answer it says "wrong try again" and when I write nothing in the box then it says "correct well done."
The code I put in is below:
Private Sub CommandButton1_Click()
If TextBox1.Value = motherboard Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Private Sub CommandButton1_Click()
If TextBox1.Value = "motherboard" Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Without Defining motherboard in another location, I am assuming you want the Literal answer to be the Text "motherboard" So you need to add quotes around the word.
The reason it accepts your answer when there isn't one is because when you add a variable that isn't defined it is defaulted with a value of nothing. So when you enter nothing it is a match to a variable that also equals nothing. By adding the quotes it makes it look for exactly that word.
You also might want to add some kind of filters to the incoming text to make it more likely for a correct answer that may have an extra space for a capital letter to be excepted.
Private Sub CommandButton1_Click()
If Trim(LCase(TextBox1.Value)) = "motherboard" Then
MsgBox "Correct. Well done!"
SlideShowWindows(1).View.Next
Else
MsgBox "Wrong answer. Try again."
End If
Adding the LCase command will make all entered text lower case. And Adding the Trim command will remove all white space before and after the word.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using Visual Basic, I want to replace a text within a String with another String. I do not know the location of this text within the String. How can I accomplish this?
Thanks for any help.
Try:
X = X.Replace("original"," toReplace")
Or this link:
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
' Input string.
Dim input As String = "Dot Net Not Perls"
' Use Regex.Replace with string arguments.
Dim output As String = Regex.Replace(input, "N.t", "NET")
' Print.
Console.WriteLine(input)
Console.WriteLine(output)
End Sub
End Module
Output
Dot Net Not Perls
Dot NET NET Perls
The replace method does not require you to know the location. You specify the original value, the string to look for, and the string to change it too. Very straight forward.
Dim aString As String = Replace("String to Search", "String to Find", "String to Replace With")