I have a problem when trying to type in my combo box (in a userform) in order to find a match. When I type a wrong letter/number it immediately gives a Mismatch Error and directs me to the VBA code. How can I avoid that? Is there something I can add to my code or to change in the properties? Because for the user it is common to type something wrong and I don't want to redirect the users to the code.
This is the code for my combo box:
Private Sub ComboBox3_Change()
If Me.ComboBox3.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("11")
Set ph = ThisWorkbook.Sheets("22")
Dim i As String
i = Application.Match((Me.ComboBox3.Value), sh.Range("A:A"), 0)
Me.TextBox8.Value = ph.Range("D" & i).Value
Me.TextBox13.Value = ph.Range("P" & i).Value
Me.TextBox41.Value = ph.Range("B" & i).Value
End If
End Sub
Private Sub UserForm_Activate()
Dim i As Integer
Me.ComboBox3.Clear
Me.ComboBox3.AddItem ""
For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox3.AddItem sh.Range("A" & i).Value
Next i
You need to use error handling statement to skip the part that is generating the error.
Private Sub ComboBox3_Change()
If Me.ComboBox3.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("11")
Set ph = ThisWorkbook.Sheets("22")
Dim i As String
or error go to MyHandler
i = Application.Match((Me.ComboBox3.Value), sh.Range("A:A"), 0)
Me.TextBox8.Value = ph.Range("D" & i).Value
Me.TextBox13.Value = ph.Range("P" & i).Value
Me.TextBox41.Value = ph.Range("B" & i).Value
End If
MyHandler:
' Expected behavior on error
End Sub
Related
so i have Sheet1 that is use to contain the list of my inventory data. what i want to do is in another sheet(Sheet2). i can search my Sheet1 data and display the data there ( for example when i type cheetos, only the cheetos item got display ). Help me guys, using VBA is okay or other method is also fine.
If your results don't have to be on a different sheet, you could just convert your data to a Table. Select Cells A1:D8 and click on Insert -> Table. Make sure "My table has headers" is clicked and voila!
Once formatted as a table, you can filter Product ID however you need.
If you do need to show these results in another sheet, VBA would be my go-to solution. Maybe something like this:
Public Sub FilterResults()
Dim findText As String
Dim lastRow As Long
Dim foundRow As Long
Dim i As Long
'If there's nothing to search for, then just stop the sub
findText = LCase(Worksheets("Sheet2").Range("D4"))
If findText = "" Then Exit Sub
'Clear any old search results
lastRow = Worksheets("Sheet2").Cells(Rows.Count, 4).End(xlUp).Row
If lastRow > 5 Then
For i = 6 To lastRow
Worksheets("Sheet2").Range("C" & i).ClearContents
Worksheets("Sheet2").Range("D" & i).ClearContents
Worksheets("Sheet2").Range("E" & i).ClearContents
Worksheets("Sheet2").Range("F" & i).ClearContents
Next i
End If
'Start looking for new results
lastRow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
foundRow = 6
For i = 2 To lastRow
If InStr(1, LCase(Worksheets("Sheet1").Range("B" & i)), findText) <> 0 Then
Worksheets("Sheet2").Range("C" & foundRow) = Worksheets("Sheet1").Range("A" & i)
Worksheets("Sheet2").Range("D" & foundRow) = Worksheets("Sheet1").Range("B" & i)
Worksheets("Sheet2").Range("E" & foundRow) = Worksheets("Sheet1").Range("C" & i)
Worksheets("Sheet2").Range("F" & foundRow) = Worksheets("Sheet1").Range("D" & i)
foundRow = foundRow + 1
End If
Next i
'If no results were found, then open a pop-up that notifies the user
If foundRow = 6 Then MsgBox "No Results Found", vbCritical + vbOKOnly
End Sub
I would recommend avoiding VBA for this process as it can be done easily with excel's functions. If you would like to do it via VBA one could just loop through the list of products and find a key word, adding it to an array if the "Cheetos" is contained in the specific cell value using a wildcard like so:
This could be modified to run upon the change of the D4 cell if needed, and of course some modifications could be done to ensure that formatting etc can be done to your liking.
Sub test()
Dim wb As Workbook
Dim rng As Range, cell As Range
Dim s_key As String, s_find() As String
Dim i As Long
Set wb = Application.ThisWorkbook
Set rng = wb.Sheets("Sheet1").Range("B2:B8")
s_key = wb.Sheets("Sheet2").Range("D4").Value
wb.sheets("Sheet2").Range("C6:F9999").clearcontents
i = 0
For Each cell In rng
If cell.Value Like "*" & s_key & "*" Then
ReDim Preserve s_find(3, i)
s_find(0, i) = cell.Offset(0, -1).Value
s_find(1, i) = cell.Value
s_find(2, i) = cell.Offset(0, 1).Value
s_find(3, i) = cell.Offset(0, 2).Value
i = i + 1
End If
Next cell
wb.Sheets("Sheet2").Range("C6:F" & 5 + i).Value = Application.WorksheetFunction.Transpose(s_find)
End Sub
I am new to VBA and my problem that I'm struggling with is to autofill a combobox in my form.
For example, my combobox has listed these values:
"apple", "tree", "juice"
I'm asking how to make it search through my list and when I write "app" to provide me with the "apple" result.
This is my code:
Private Sub ComboBox3_Change()
If Me.ComboBox3.Value <> "" Then
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("11")
Set ph = ThisWorkbook.Sheets("22")
Dim i As String
i = Application.Match((Me.ComboBox3.Value), sh.Range("A:A"), 0)
Me.TextBox8.Value = ph.Range("D" & i).Value
Me.TextBox13.Value = ph.Range("P" & i).Value
Me.TextBox41.Value = ph.Range("B" & i).Value
End If
End Sub
Private Sub UserForm_Activate()
Dim i As Integer
Me.ComboBox3.Clear
Me.ComboBox3.AddItem ""
For i = 2 To sh.Range("A" & Application.Rows.Count).End(xlUp).Row
Me.ComboBox3.AddItem sh.Range("A" & i).Value
Next i
I cannot reproduce your issue. I made a ComboBox with the items as shown below:
If I start typing App it looks like below, where it selected Apple automatically
So it does exactly what you are asking for by default.
I have problem with closing one userform and going to next. UserForm3 after clicking command button should be closed and UserForm4 should be shown. Unfortunately I get "Run time Error 91 object variable or with block variable not set". I've dug deep into internet and I am pretty sure that problem is with Userform4, although code for UserForm3 is highlighted as bugged. Basicly I want UserForm4 to be displayed and have all the textboxes filled with data from sheet "Log", based on choice from Combobox from UserForm3. Choice from UserForm3 is saved to cell E1 on "Log" Sheet.
Code from UserForm3
Private Sub CommandButton1_Click()
Sheets("Log").Range("E1") = ComboBox2.Text
Unload Me
UserForm4.Show <- ERROR DISPLAYED HERE
End Sub
In UserForm4 I want to find value from E1 in cells below and later on fill textboxes in Userform4 with data from the row, in which E1 value was found.
Code for UserForm4
Private Sub UserForm_Initialize()
Dim Name As String
Dim rng As Range
Dim LastRow As Long
Dim wart As Worksheet
wart = Sheets("Log").Range("E1")
LastRow = ws.Range("B3" & Rows.Count).End(xlUp).Row + 1
Name = Sheets("Log").Range("E1")
UserForm4.TextBox8.Text = Name
nazw = Application.WorksheetFunction.VLookup(wart, Sheets("Log").Range("B3:H" & LastRow), 1, False)
UserForm4.TextBox1.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox2.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox3.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox4.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox5.Text = ActiveCell.Offset(, 1)
UserForm4.ComboBox1.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox6.Text = ActiveCell.Offset(, 1)
UserForm4.TextBox7.Text = ActiveCell.Offset(, 1)
End Sub
The code below is to avoid to run-time errors mentioned in the code above, it's not debugged for the VLookup function part.
Option Explicit
Private Sub UserForm_Initialize()
Dim Name As String
Dim LastRow As Long
Dim wart As Variant
Dim ws As Worksheet
Dim nazw As Long
' set ws to "Log" sheets
Set ws = Sheets("Log")
With ws
wart = .Range("E1")
' method 1: find last row in Column "B" , finds last row even if there empty rows in the middle
LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row + 1
' method 2 to find last row, equivalent to Ctrl + Shift + Down
' LastRow = .Range("B3").CurrentRegion.Rows.Count + 1
' a little redundant with the line 2 above ?
Name = .Range("E1")
End With
With Me
.TextBox8.Text = Name
' ****** Need to use Match instead of Vlookup VLookup Section ******
If Not IsError(Application.Match(wart, ws.Range("B1:B" & LastRow - 1), 0)) Then
nazw = Application.Match(wart, ws.Range("B1:B" & LastRow - 1), 0)
Else ' wart record not found in range
MsgBox "Value in Sheet " & ws.Name & " in Range E1 not found in Column B !", vbInformation
Exit Sub
End If
.TextBox1.Text = ws.Range("B" & nazw).Offset(, 1)
.TextBox2.Text = ws.Range("B" & nazw).Offset(, 1)
.TextBox3.Text = ws.Range("B" & nazw).Offset(, 1)
.TextBox4.Text = ws.Range("B" & nazw).Offset(, 1)
.TextBox5.Text = ws.Range("B" & nazw).Offset(, 1)
.ComboBox1.Text = ws.Range("B" & nazw).Offset(, 1)
.TextBox6.Text = ws.Range("B" & nazw).Offset(, 1)
.TextBox7.Text = ws.Range("B" & nazw).Offset(, 1)
End With
End Sub
I have a column which may have N/As. I am running a loop which checks if there is N/A error and pops up MsgBox with location of an error.
What I have problem with is storing a value from corresponding column where there's an error.
Option Explicit
Dim i As Integer
Dim where As Variant
Dim numbers(1 To 20) As Variant
For Each i In Range("b2:b" & lastrow)
If IsError(i) = True Then
where = Range("B" & i.Row).Address
MsgBox "Missing data was found in " & where
numbers (i) = Range("D" & i.Row).Value
End If
Next i
There's something wrong with this code, as a get mismatch error (run time error '13').
Please point me in the right direction what I am doing wrong here.
Thanks
Edit: I quoted just part of code, thats why there's no Sub/End Sub
Private Const lastrow As Integer = 10
Sub GetErrors()
Dim oneCell As Range
Dim where As Variant
Dim numbers As Variant
where = vbCrLf
For Each oneCell In Range("b2:b" & lastrow).Cells
If IsError(oneCell.Value) = True Then
where = where & Range("B" & oneCell.Row).Address & vbCrLf
If (Not IsArray(numbers)) Then
ReDim numbers(0)
Else
ReDim Preserve numbers(UBound(numbers) + 1)
End If
numbers(UBound(numbers)) = Range("D" & oneCell.Row).Value
End If
Next oneCell
MsgBox "Error was found in : " & where
End Sub
So I am doing some coding for the payroll application. When I run the macro and enter EmployeeNumber in textbox1, I get sent to debugging and third row of code "c=application.worksheet...." is highlighted in yellow. Basically, when I enter employee number in textbox 1, the subsequent information of that employee should get populated by itself. Can someone please tell me what I am doing wrong in the code below?
Private Sub CommandButton1_Click()
Me.TextBox2.Enabled = True
Me.TextBox3.Enabled = True
Me.TextBox4.Enabled = True
Me.TextBox5.Enabled = True
Me.TextBox6.Enabled = True
Me.CommandButton2.Visible = True
Me.CommandButton1.Visible = False
End Sub
Private Sub CommandButton2_Click()
m = MsgBox("Do You Want To Update Employee Information?", vbQuestion + vbYesNo, "Confirm Update")
If m = vbNo Then Exit Sub
EmployeeNumber = Val(Me.TextBox1.Value)
c = Application.WorksheetFunction.CountIf(MasterData.Range("A:A"), EmployeeNumber)
If c = 0 Then Exit Sub
r = Application.WorksheetFunction.Match(EmployeeNumber, MasterData.Range("A:A"), 0)
MasterData.Range("B" & r).Value = Me.TextBox2.Value
MasterData.Range("C" & r).Value = Me.TextBox3.Value
MasterData.Range("D" & r).Value = Me.TextBox4.Value
MasterData.Range("E" & r).Value = Me.TextBox5.Value
MasterData.Range("F" & r).Value = Me.TextBox6.Value
Me.TextBox2.Enabled = False
Me.TextBox3.Enabled = False
Me.TextBox4.Enabled = False
Me.TextBox5.Enabled = False
Me.TextBox6.Enabled = False
Me.CommandButton2.Visible = False
Me.CommandButton1.Visible = True
End Sub
Private Sub CommandButton3_Click()
Unload.Me
End Sub
Private Sub Label1_Click()
End Sub
Private Sub Label2_Click()
End Sub
Private Sub Label5_Click()
End Sub
Private Sub TextBox1_Change()
Dim wks As Worksheet
Set wks = Worksheets("MasterData")
EmployeeNumber = Val(Me.TextBox1.Value)
c = Application.WorksheetFunction.CountIf(MasterData.Range("A:A"), EmployeeNumber)
If c = 0 Then
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Exit Sub
End If
r = Application.WorksheetFunction.Match(EmployeeNumber, MasterData.Range("A:A"), 0)
Me.TextBox2.Value = MasterData.Range("B" & r).Value
Me.TextBox3.Value = MasterData.Range("C" & r).Value
Me.TextBox4.Value = MasterData.Range("D" & r).Value
Me.TextBox5.Value = MasterData.Range("E" & r).Value
Me.TextBox6.Value = MasterData.Range("F" & r).Value
End Sub
Private Sub UserForm_Click()
End Sub
This must not compile - fix that first:
Syntax error in
r = Application.worksheerfunction.Match
Fixed:
r = Application.WorksheetFunction.Match
EDIT: Fix more syntax errors
Replace MasaterData with MasterData
MasaterData.Range("B" & r).Value = Me.TextBox2.Value
MasaterData.Range("C" & r).Value = Me.TextBox3.Value
MasaterData.Range("D" & r).Value = Me.TextBox4.Value
MasaterData.Range("E" & r).Value = Me.TextBox5.Value
MasaterData.Range("F" & r).Value = Me.TextBox6.Value
You can do simple troubleshooting yourself to find the problem by simplifying your code to get one item to work - then expanding the code that works.
The simplest way to troubleshoot VBA problems yourself is to add MSGBOX or DEBUG.PRINT statements to your code to trace what's happening
Currently the issue you have is that you have two locations that handle your text box - the one that's causing you a problem right now is in TextBox_Change - that's going to fire once for every character you type in.
You either want to remove that completely or move it to the TextBox AfterUpdate event - and then decide if you want to remove the duplicate code in the Command Click event
Well Apparently, the userform wasn't enabled "True/False" from properties. That was the reason, I couldn't type anything in textbox 1. I'd like to thanks DbMitch for teaching me the step by step debug approach. The form does its intended function now.