Check if item in split string is empty? [duplicate] - excel

Note. Check if the TextBox1 is empty is easy by using TextBox1.Value = "".
But the problem is when the user hit the spacebar, TextBox1 will still recognize it as a value. In such case, my data will appear as an empty cell with 1 space inside. So my question is, is there any method to check TextBox1.value for empty and also not consist of space whether there are 1 or more space? Million thanks to all.

A common trick is to check like this:
trim(TextBox1.Value & vbnullstring) = vbnullstring
this will work for spaces, empty strings, and genuine null values

Most terse version I can think of
Len(Trim(TextBox1.Value)) = 0
If you need to do this multiple times, wrap it in a function
Public Function HasContent(text_box as Object) as Boolean
HasContent = (Len(Trim(text_box.Value)) > 0)
End Function
Usage
If HasContent(TextBox1) Then
' ...

Here is the code to check whether value is present or not.
If Trim(textbox1.text) <> "" Then
'Your code goes here
Else
'Nothing
End If
I think this will help.

You can use the following code to check if a textbox object is null/empty
'Checks if the box is null
If Me.TextBox & "" <> "" Then
'Enter Code here...
End if

Related

visual basic: user inputs a % sign after text

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

Userform textbox fills in if Vlookup finds the information related to the numbers inserted

I have an userform where people have to fill in with data. If the data already exists, when they put the information in the DocumentTitleBox, other textboxes should automatically fill in.
My code works with letters, but not with numbers.
For example, when I put "aaa", it returns the vlookup values. But if I put "123", it won't do anything, even though there is a vlookup for it.
I cannot figure it out why. This is part of my code:
Private Sub DocumentTitleBox_Change()
On Error Resume Next
Result = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 2, False)
FIND = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 1, False)
On Error GoTo 0
If FIND = DocumentTitleBox.Value Then
NameBox.Value = Result
End If
Thank you in advance!
I always use this kind of thing. Could be cleaned up and stuff but I like the flexibility and I change stuff all the time so this works for me.
Private Sub DocumentTitleBox_Change()
If IsNumeric(DocumentTitleBox.Value) Then
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value + 0, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
Else
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
End If
If Not Find Like "Not Present" Then
NameBox.Value = Find
Else
NameBox.Value = ""
End If
End Sub
PS: I don´t know how to avoid the match functions odd behaviour with strings/numbers so I just go with the +0 and IsNumeric trick. One thing to note is case sensitivity, adjust that as needed, right now its not.
If DocumentTitleBox is a text box, try using DocumentTitleBox.Text instead of DocumentTitleBox.Value.

How to Evaluate a Variable Value from a String?

I've been absolutely tearing my hair out over this. Is there a simple way to pull a variable value from a string? I'm trying to use this to cleanly check the value of multiple similar variables. Example:
Dim BoxString As String
For i = 1 To 100
BoxString = "response = Report" & i & "Box.value"
Application.Evaluate (BoxString)
If response = True Then
'...
End If
Next i
For context, I'm using this to evaluate which boxes on a UserForm were selected, and perform an action for each one that was.
Thanks so much in advance!

cancel or hide input box in vba

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.

Excel UDF not appearing in drop down menu

I wrote a User Defined Fucntion in Excel. It works great with no issues. I even wrote a description for it under the object properties menu.
The problem is, my UDF never shows up in the Excel drop down menu that appears when I start to type a function. I want the user to be able to see my UDF, named removeNumbers, when they go into a cell and start to type out a function.
I would also like them to be able to see the description which I wrote, just like the standard Excel functions.
And finally, is there a way that I can provide a description for each argument which my function takes as input?
Here is the actual code, although I don't think it will be necessary to answer my questions.
Function removeNumbers(sInput As String, sChoice As Boolean) As String
Dim sSpecialChars As String
Dim i As Long
If (sChoice = True) Then 'if true is selected, will remove all number including 0
sSpecialChars = "0123456789" 'This is your list of characters to be removed
For i = 1 To Len(sSpecialChars)
sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
Next
End If
If (sChoice = False) Then 'if false is selected, will remove all numbers excluding zero
sSpecialChars = "123456789" 'This is your list of characters to be removed
For i = 1 To Len(sSpecialChars)
sInput = Replace$(sInput, Mid$(sSpecialChars, i, 1), "")
Next
End If
removeNumbers = sInput
End Function
To make the function appear in the drop-down you must place it in a standard module rather than the worksheet code area.
Another poster has already covered the need for the code to be in a standard module. With regards the argument descriptions, you should look at the MacroOptions code in this answer - although it only works in Excel 2010 or later.
For Excel 2007 and earlier, the only solution I have seen is in an article by JK Pieterse. This involves using the ExecuteExcel4Macro and looks a bit complicated.

Resources