I have a GUIDE GUI where I ask the user to enter in their name. It should execute an error dialog if they enter numerical characters, a mix of string and numerical characters, or a blank box.
The issue is that when I enter in numbers or a mix of string and numerical characters, then it is outputting Error Code II (1st elseif statement) instead of Error Code III (entering in numbers only) or Error Code IV (entering numbers and strings). Input would be greatly appreciated.
Here's essentially what I have:
if isempty(editString)
errordlg('Please enter a name into the text-box. We thank you in anticipation.',...
'Error Code I');
return
elseif char(editString) > 12
errordlg('Please enter a name that is less than 12 characters long. Thank you.',...
'Error Code II');
return
elseif isa(editString, 'integer')
errordlg('Please enter a name, not numbers. Thank you.', 'Error Code III');
return
elseif isa(editString, 'integer') && isa(editString, 'char')
errordlg('Please enter a name without mixing numbers & characters. Thanks.',...
'Error Code IV');
else
delete(gcf)
gui_02
end
Well, isa() function doesn' t work in this case because all you read from Edit Text is string in other words char. Thus, if you even write isa('123', 'integer'), function returns 0 not 1. Anyway, thanks to MATLAB there is a function: isstrprop() determines whether string is of specified category such as integer, char..
Check the code below:
if isempty(editString)
errordlg('Please enter a name into the text-box. We thank you in anticipation.', 'Error Code I');
return
elseif length(editString) > 12
errordlg('Please enter a name that is less than 12 characters long. Thank you.', 'Error Code II');
return
elseif ~isempty(find(isstrprop(editString, 'digit'), 1)) & isempty(find(isstrprop(editString, 'alpha'), 1))
errordlg('Please enter a name, not numbers. Thank you.', 'Error Code III');
return
elseif ~isempty(find(isstrprop(editString, 'digit'), 1)) & ~isempty(find(isstrprop(editString, 'alpha'), 1))
errordlg('Please enter a name without mixing numbers & characters. Thanks.', 'Error Code IV');
return
end
It doesn' t look elegant but works.
Related
Private Sub btnBillSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBillSearch.Click
If Me.txbBILL_NR.Text = "" Or Me.txbBILL_NR.TextLength >= 4 Then
Try
Search()
If Me.bsBillGrid.Current IsNot Nothing Then
Me.dgvBill.Focus()
End If
Catch ex As Exception
Utility.ExceptionManager.HandleException(ex)
End Try
Else
MessageBox.Show("enter at least 4 numbers")
End If
End Sub
I have this method for search button in my app.
So, I already wrote the If condition for where user enters a minimum number of characters is 4 to select entries.
I need to one more validation for % sign. I figured out, that is kinda startWidth(), but I do not understand how I can get those situations:
I think about using startWith/endWith or regex maybe.
Could anyone please give me some advice where to look at.
user adds “%” sign after txbBILL_NR - the system does not add additional “%” in the background,
txbBILL_NR with identical part number are found and reflected in the list.
the user has not added the “%” sign after the txbBILL_NR - the system adds an additional “%” in the background,
txbBILL_NR with identical no. parts are found and listed.
The "%" character does not need to be represented by the user;
3.user added “%” sign at the beginning or middle of txbBILL_NR - the system does not add an additional “%” in the background,
txbBILL_NR with identical part number are found and reflected in the list (no changes required);
The "%" system only needs to be marked if the user has entered at least 4 characters in the txbBILL_NR field (assuming that the invoice number cannot be shorter than 4 characters.
If the user has entered at least 4 characters (whether they are starting characters or from the middle) and "%" is added to the beginning or middle of these symbols, the system does not add an additional "%" in the background
the invoice (s) with the identical part (symbol) part are found and reflected in the list.
'Get raw user entry.
Dim searchText = txbBILL_NR.Text
'Get user entry without wildcards.
Dim cleanSearchText = searchText.Replace("%", String.Empty)
If cleanSearchText.Length = 0 OrElse cleanSearchText.Length >= 4 Then
If searchText = cleanSearchText Then
'There are no wildcards so append one.
searchText &= "%"
End If
'Use searchText here.
End If
I think this function meets your requirements.
Private Function SearchCriterium(ByVal Crit As String) As String
' 295
Const Mark As String = "%"
Dim Fun As String ' function return value
Fun = Crit
Do While (Len(Fun) >= 4) And (Right(Fun, 1) = Mark)
Fun = Left(Fun, Len(Fun) - 1)
Loop
If Len(Fun) < 4 Then
MsgBox "Enter at least 4 characters.", _
vbExclamation, "Invalid search crterium"
SearchCriterium = Crit
Else
If InStr(Fun, Mark) = 0 Then Fun = Fun & Mark
End If
SearchCriterium = Replace(Fun, Mark, "*")
End Function
Observe that it replaces the % sign with VBA's wild card character *. You can remove that feature if you don't need it at that point.
The function removes any existing trailing placeholder and then adds one if none exists elsewhere in the string. Here is a list of the tests I ran.
Private Sub Test_SearchCriterium()
Debug.Print SearchCriterium("Item")
Debug.Print SearchCriterium("It%em")
Debug.Print SearchCriterium("%Item")
Debug.Print SearchCriterium("Ite%")
Debug.Print SearchCriterium("It%%")
End Sub
I'm trying to find the carriage return\line break in various cells, by iterating over them and running the InStr function against the text in each of those cells. The below query is finding the line break correctly in most cells, but is failing in one of those cells.
The line break in those cells was added the same for all of them, and that is by hitting Alt+Enter.
Below is the function I am using:
InStr(startlocation, text, vbLf)
I also tried all of the below, but to no avail:
InStr(startlocation, text, Chr(10)) 'this seems to be identical in results to using vbLf
InStr(startlocation, text, Chr(13)) 'No results with this
InStr(startlocation, text, ALT + 10) 'I see this returning some results sometimes, not exactly sure for which character though
InStr(startlocation, text, vbCrLf) 'No results with this
Is there any other way to represent line break so I add it to my query?
The line break in cells, which is created with Alt + Enter is a vbLf.
Therefore the following should work.
InStr(startlocation, text, vbLf)
If it doesn't work that means you did something else wrong.
If you have the following data in cell A1 (Alt + Enter after 1 and 2)
Then InStr(1, Range("A1"), vbLf) returns 2.
For example you can use …
Dim ArrLines As Variant
ArrLines = Split(Range("A1"), vbLf)
to split these lines into an array like …
ArrLines(0) is 1
ArrLines(1) is 2
ArrLines(2) is 3
As others have confirmed, vbLf was indeed the correct character for identifying the line break introduced by ALT + Enter. My issue turned out to be caused by how the content of text was ending one character before the line break, therefore the InStr(startlocation, text, vbLf) on that string was not finding the line break. Extending text (which was created using the Mid function) by 1 took care of the issue. So:
Instead of: text = Mid(entryvalue, delimeterlocation, Len(entryvalue) - delimeterlocation)
I did this: text = Mid(entryvalue, delimeterlocation, Len(entryvalue) - delimeterlocation + 1)
I have a code which asks for an input between 1-3 using an InputBox which then runs a code depending on what input it is given. The problem i have is that i don't need that InputBox anymore, as the work i intend to do only uses one input which is 3.I tried removing the input box and just inserting 3 there but then the code doesnt do anything. I then tried to insert a default value in the box, but it still appears and needs me to click enter which is what i am trying to avoid. Pls how should i go about this problem.
My Code
Function GetTypeFile() As Integer
Dim strInput As String, strMsg As String
Dim Default
choice = 0
While (choice < 1 Or choice > 3)
Default = "3"
strMsg = "Type in the kind of entities to create (1 for points, 2 for points and splines, 3 for points, splines and loft):"
strInput = InputBox(Prompt:=strMsg, _
Title:="User Info", Default:=3, XPos:=2000, YPos:=2000)
'Validation of the choice
choice = CInt(strInput)
If (choice < 1 Or choice > 3) Then
MsgBox "Invalid value: must be 1, 2 or 3"
End If
Wend
GetTypeFile = choice
End Function
Your function returns the value to wherever it's called, so you could just use:
Function GetTypeFile() As Integer
GetTypeFile = 3
End Function
or just replace any calls to the function with the number 3.
So rather than something like:
Sub Test()
ThisWorkbook.SaveAs "MyFileName", GetTypeFile
End Sub
You'd have:
Sub Test()
ThisWorkbook.SaveAs "MyFileName", 3
End
Thank you all for your reply. I just understood what to do, by setting the variable which is dependent on the value in the inputbox to 3 instead of it being TypeDocument=GetTypeFile it is now TypeDocument=3 directly..I think that is what you all had been trying to say all the while. Thank you again.
I am trying to perform some operations over some data, but they are not working and I need to find a way to ignore those cells which don´t meet all the requirements.
Basically, I have a column where some cells have text + numbers in their content and other have only text. I search inside all of them and split TEXT in one column and NUMBER in another one. Then, I run a macro to find the matching text to each one in other column.
But when I try to split TEXT from NUMBERS, I search for the first "(", cause my number format is (10.10.10), but if it is not found, the cell value appears: #VALUE! (ok, it is expected cause the character was not found). Here is the problem: if I run my macro over "#VALUE! it crashs and don´t finish its execution.
I have tried to use
On Error GoTo
in my macro code, but for some reason it doesn´t not handle the "Run time error: Type Mismatch".
For contadorOr = 2 To colO
For contadorDes = 2 To colA
On Error GoTo cont
If InStr(1, Cells(contadorDes, colunaDestino).Value, Cells(contadorOr, colunaOrigem).Value) Then
If InStr(1, Cells(contadorOr, colunaOrigem + 4).Value, Cells(contadorDes, colunaDestino + 1).Value) Then
Cells(contadorOr, colunaOrigem + 5).Value = "Mesma versão"
End If
Exit For
End If
Next contadorDes
cont: Next contadorOr
Any suggestions? I can think of ignoring this error (when it´s happen, my variable contadorOr is incremented and go to no next value) or any way to avoid #VALUE! returned by my functions, but haven´t had success doing that.
Thanks in advance.
Instead of using error handling options you could check if cell which you are going to check/process doesn't return error. This is quite simple as presented below:
If IsError(Cells(contadorDes, colunaDestino).Value) Then
'to do anything if there is error
'usually...do nothing
Else
'do what you want if there is no error
End if
I found the article about putting excel cells into an email using the RangetoHTML function in VBA. It works like a charm, but now I’m facing a Problem.
If there are Umlaut (e.g.: ü, ä, ö) in the cells the result in the email shows strange symbols (e.g.: ä, …).
I looked up the written temp.htm file. On the first view of this file, it seems the umlaute are correctly written, but after looking through the file with an hex editor i found that the written symbols are not correct.
The function which writes the file is: PublishObjects.Add
So I hope someone can help me with this.
Edit: Added a testfile. Word and Office is needed.
Select the table and run the procedure SendMail.
You will always have problems with vba and foreign chars and the web.
EDIT:
Because you can't separate the cell values from the html the function below will unfortunately not work in this situation. BUT:
if you Save a copy of the document with western European windows encoding it will work.
(See comments below).
To be able to do that you press "Save As" and there is a dropdown on the left side of the save button (Tools) which will give you a dialog where you can change the encoding.
The image has ben lifted from technet and always save web.. is not necessary.
EOF EDIT:
This is a function I have used, Unfortunately can't remember who I got it from, But its from the olden days of vba and classic asp
Put your email cell formula into this function and it should work because all the letters are html encoded. Its slow and makes a bad overhead. But it will work.
Function HtmlEncode(ByVal inText As String) As String
Dim i As Integer
Dim sEnc As Integer
Dim repl As String
HtmlEncode = inText
For i = Len(HtmlEncode) To 1 Step -1
sEnc = Asc(Mid$(HtmlEncode, i, 1))
Select Case sEnc
Case 32
repl = " "
Case 34
repl = """
Case 38
repl = "&"
Case 60
repl = "<"
Case 62
repl = ">"
Case 32 To 127
'Numbers
Case Else
repl = "&#" & CStr(sEnc) & ";" 'Encode it all
End Select
If Len(repl) Then
HtmlEncode = Left$(HtmlEncode, i - 1) & repl & Mid$(HtmlEncode, i + 1)
repl = ""
End If
Next
End Function