I am trying to create a search mask in Excel, where a search field is popped up (which matches with a column of the sheet), but having only written the first part of the code and wanting to try I ran and ... gives me an error.
Here's the error:
Here's the mask:
I’m translating the error: "Build error... Mismatch type."
Private Sub CommandButton1_Click()
Dim UltimaRiga As Long
If CheckBox1.Value = True And TextBox1.Value Is Not "" Then
UltimaRiga = Range("D1").End(xlDown).Row + 1
If Range("D2").Value Is Not "" And Range("D2").Value = TextBox1.Value Then
TextBox2.Value = "Presente"
If Range("J2").Value = "Disponibile" Then
TextBox3.Value = "Disponibile"
End If
End If
End If
End Sub
Related
Dears,
I want to make a simple userform to record some serial numbers into excel, it contains a textbox_serialNo., a command button “enter” and another command button “cancel”.
I made a validation control in that serialNo textbox so that only number can be entered. However, when I run the program and input some numbers into the textbox, both command buttons (the "enter" button named as label_enter,the "cancel" button named as label_cancel) have no reactions (e.g. the "cancel" button doesn't unload the form when press) , how should I correct the program? Below are the relevant codes, Thanks.
Private Sub TextBox_SerialNo_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsNumeric(TextBox_SerialNo.Value) Then
TextBox_SerialNo.BackColor = rgbYellow
End If
Cancel = True
End Sub
Private Sub TextBox_SerialNo_AfterUpdate()
If TextBox_SerialNo.Value <> "" Then
TextBox_SerialNo.BackColor = rgbWhite
End If
End Sub
Private sub label_enter_click()
sheet1.Select
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
ActiveCell.Value = ActiveCell.Offset(-1, 0).Value + 1
ActiveCell.Offset(0, 1) = TextBox_SerialNo.Value
TextBox_SerialNo.Value = ""
End Sub
Private Sub Label_Cancel_Click()
Unload Me
End Sub
Sorry to be posting as an answer, not enough rep.
Shouldn't Cancel=True be inside the if statement? You are locking it up regardless of entry being numeric or not as is.
Edit:
Actually upon further testing still not working proper. However, change event works better and you can get instant feedback for any non numerics.
Updated code would look like this, control names differ. I am used to working with .Text, same thing as .Value. Also, since I am not sure what you would do with an empty string, assumed it to be yellow background as well.
One concern would be, can you allow comma or period in there? Depending on locale settings, a decimal would also be considered a numeric.
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub cmdEnter_Click()
If TextBox1.BackColor = rgbYellow Then Exit Sub
test4.Range("A1").Value = TextBox1.Text
End Sub
Private Sub TextBox1_Change()
If Not IsNumeric(TextBox1.Text) Or TextBox1.Text = "" Then
TextBox1.BackColor = rgbYellow
Else
If TextBox1.Text <> "" Then
TextBox1.BackColor = rgbWhite
End If
End If
End Sub
Edit 2: I use this piece of code to check for only numbers (assuming number Ascii codes are standard). Maybe it can help.
Public Function isnumber(ByVal strValue As Variant) As Boolean
On Error Resume Next
Dim i As Long
isnumber = True
If Not strValue = "" Then
For i = 1 To Len(CStr(strValue))
If Asc(Mid(strValue, i, 1)) > 57 Or Asc(Mid(strValue, i, 1)) < 48 Then
isnumber = False
Exit For
End If
Next i
Else
isnumber = False
End If
On Error GoTo 0
Err.Clear
End Function
Edit 3: I have revised the TextBox1_Change event code so all invalid characters are stripped right away. However, in this state if you copy paste a serial no with a non-allowed char, it will strip them leaving only the numbers. Not sure if it is acceptable.
Private Sub TextBox1_Change()
If Not isnumber(TextBox1.Text) Or TextBox1.Text = "" Then
TextBox1.BackColor = rgbYellow
Dim i As Long
Dim strValue As String
strValue = ""
If Not TextBox1.Text = "" Then
For i = 1 To Len(CStr(TextBox1.Text))
If Not (Asc(Mid(TextBox1.Text, i, 1)) > 57 Or Asc(Mid(TextBox1.Text, i, 1)) < 48) Then
strValue = strValue & Mid(TextBox1.Text, i, 1)
End If
Next i
End If
TextBox1.Text = strValue
Else
If TextBox1.Text <> "" Then
TextBox1.BackColor = rgbWhite
End If
End If
End Sub
This is a question mainly to understand and learn, then to find a solution, since i know what causes the problem and made a workaround.
I get Run-time error '-2147417848 (80010108)': Method 'Value' of object 'Range' failed.
I have a main UserForm with a ListBox, it gets filled from a table. Then i have a secondary UserForm to make new entries in said table. That table is on my sheet shData("Daten") and is called "mainData". If just run the secondary form to make new entry, all is fine. But if i start the secondary form from main form, i get the error.
This is the code in main form.
Private Sub newEntryButton_Click()
newEntry.Show
End Sub
Private Sub UserForm_Activate()
mainListUpdate
End Sub
This is the code in secondary form
Private Sub doneButton_Click()
Dim n As Long
n = cRow + 1
shData.Range("A" & n).Value = nameBox.Value
shData.Range("B" & n).Value = paraBox.Value
shData.Range("C" & n).Value = hStartBox.Value
shData.Range("D" & n).Value = bdayBox.Value
If OptionButtonStat.Value = True Then
shData.Range("E" & n).Value = "Ja"
shData.Range("G" & n).Value = statPlaceBox.Value
ElseIf OptionButtonAmb.Value = True Then
shData.Range("F" & n).Value = "Ja"
End If
Me.Hide
End Sub
Private Sub OptionButtonStat_Change()
If OptionButtonStat.Value = True Then
Me.statPlaceBox.Visible = True
Me.statPlaceLab.Visible = True
Else
Me.statPlaceBox.Visible = False
Me.statPlaceLab.Visible = False
End If
End Sub
Public Sub UserForm_Activate()
Me.statPlaceBox.Visible = False
Me.statPlaceLab.Visible = False
End Sub
and this is the code in a module.
Public cRow As Long
Public cCol As Long
Public Sub mainListUpdate()
If shData.FilterMode = True Then
shData.ShowAllData
End If
cRow = shData.Range("A1").CurrentRegion.Rows.Count
cCol = shData.Range("A1").CurrentRegion.Columns.Count
formMain.listMain.ColumnCount = cCol
formMain.listMain.ColumnHeads = True
formMain.listMain.RowSource = "mainData"
End Sub
The error occurs on the following line in secondary form code: shData.Range("A" & n).Value = nameBox.Value. If i change formMain.listMain.RowSource = "mainData" in the module code to formMain.listMain.RowSource = "Daten!A2:G", the problem disappears. Again, the code works fine with the table as RowSource, if i just use the secondary form, just need to add cRow = shData.Range("A1").CurrentRegion.Rows.Count.
I hope, i explained it well enough. Can somebody please explain, why i have this problem? It was very frustrating to search for the issue and i really would like to use the original code.
I have a simple macro created and works well on windows, however, when I ran the on a MAC, there is a type mismatch error. What is wrong with below code? it happened at line If IsError(Application.Match(myValue, value_range, 0)) Then:
Sub HideAndSeek()
Rows("3:5676").Select
Selection.EntireRow.Hidden = True
myValue = InputBox("Type in an ID")
If myValue = "" Then Exit Sub
value_range = Columns("A:A")
If IsError(Application.Match(myValue, value_range, 0)) Then
MsgBox "No ID found"
Exit Sub
Else
index_num = Application.WorksheetFunction.Match(myValue, value_range, 0)
End If
Rows(index_num).RowHeight = 120
End Sub
The following is my VB code. I want to count all the distinct records about "Peter" in a spreadsheet without duplication.
When I run the code, "Run-time error '13':Type Mismatch" always appear. I fail to debug. What's wrong with my code?
Private Sub CheckBox5_Click()
Dim myarray As Variant
myarray = WorksheetFunction.If(Range("C7:C266") = "Peter", 1 / (WorksheetFunction.CountIfs(Range("C7:C266"), "Peter", Range("F7:F266"), Range("F7:F266"))), 0)
If CheckBox5.Value = True Then
TextBox6.Value = WorksheetFunction.Sum(myarray) + 1
End If
If CheckBox5.Value = False Then
TextBox6.Value = ""
End If
End Sub
The error you are getting is a result of the way the IF function is called. The first term must be a logical result, but you cannot call the value of a multi-cell Range (ie Range("C7:C266")). To solve this problem, I think you will need to loop through each of the cells and act on them accordingly, although there may be a more clever solution using something other than IF that I am not aware of
You can do it like this:
Sub findPeter()
Dim ws As Worksheet
Dim peterCount As Long
Set ws = Worksheets("nameofyoursheet")
With ws
For i = 7 To 266
If .Cells(i, 3) = "Peter" Then
peterCount = peterCount + 1
End If
Next
End With
If CheckBox5.Value = True Then
TextBox6.Value = peterCount + 1
End If
If CheckBox5.Value = False Then
TextBox6.Value = ""
End If
End Sub
peterCount is the sum of all occurences of the value Peter.
I've got an excel UserForm with two TextBox and a Button. How can I delete the value of the TextBox without using TextBox1.Value = "" or TextBox1.Text = "" ?
I can't use these two operations because then the instruction: TextBox2.Value = TextBox1.value*2
give me back an error. (The error is Runtime error 13)
Thanks.
Try looking at the problem a different way. If box1 is empty then why would you want to multiply it by 2? In this case you should error check the value of box1 before you multiply it by box2. The code below checks if Textbox1 is a number and is not empty before multiplying it.
Private Sub CommandButton1_Click()
'Error check: does textbox1 have a number and is not null
If IsNumeric(TextBox1.Value) And TextBox1.Value <> vbNullString Then
TextBox2.Value = TextBox1.Value * 2
Else
TextBox2.Value = "Box1 is empty or invalid"
End If
End Sub
Valid Number:
Invalid or empty: