Userform VLOOKUP not working - excel

I’m trying to extract the employee name based on the employee id with a VLOOKUP formula in a User Form.
The code below inst working.
Private Sub CommandButton2_Click()
Label4.Caption = Sheet1.Application.WorksheetFunction.VLookup(TextBox1.Text, Range("A:B"), 2, False)
End Sub

The provlem here is when there is no match found. That's the cause of the error message. Here is the
the VBA code you should use:
Private Sub CommandButton2_Click()
On Error Resume Next
Label1.Caption = Sheet1.Application.WorksheetFunction.VLookup(TextBox1.Text, Range("A:B"), 2, False)
If Err.Number <> 0 Then
Err.Clear
Label1.Caption = "not found"
End If
End Sub

Related

How do I avoid Userform Combobox runtime error?

Hopefully a simple question. I have some simple code for a combo box that runs during Combobox_Change().
Private Sub ComboBox1_Change()
If ComboBox1.Value = "" Then
Label3.Caption = ""
Else
Label3.Caption = Worksheets("Currency").Cells.Find(UserForm1.ComboBox1.Value).Offset(0, -1).Value
End If
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = [Currency!C2:C168].Value
Label3.Caption = ""
End Sub
But when you enter something that isn't part of the declared Combobox range it throws up a runtime error 'Object variable not set'. How do I fool proof this combobox and when any irregular entry is made that isn't part of the selection range for it to revert back to "" empty? Or pop up with an error box stating "Invalid Input"?
If Find fails to find anything it returns a null object. That returned object has no methods or properties so you can't take the offset() or value of it. To work around this you need to separate out the returned object and its methods/properties and test the validity of the returned object.
Private Sub ComboBox1_Change()
If ComboBox1.Value = "" Then
Label3.Caption = ""
Else
Dim fndrng As Range
'Get just the find range
Set fndrng = Worksheets("Currency").Cells.Find(UserForm1.ComboBox1.Value)
'Make sure find found something
If Not fndrng Is Nothing Then
'use the methods/properties we want
Label3.Caption = fndrng.Offset(0, -1).Value
Else
MsgBox "Selection not found", vbOKOnly, "Error"
End If
End If
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = [Currency!C2:C168].Value
Label3.Caption = ""
End Sub

Excel Userform to update existing Data

I would like to overwrite data using a Userform, I can call the data to the form based on a Combobox (unique reference from column A in my data sheet). I am failing to send updated data back, and am stuck on a Run-time error '13.
I have looked at a number of posts but cannot pick out a thread to success! Any help appreciated. I have left the code simple, to update the 4 column of that row. Ultimately I will expand from the 2nd column onwards.
Private Sub cmbtrade_Change() - this part works as expected
Dim trade_name As String
If Me.cmbtrade.Value = "" Then
MsgBox "Trade Can Not be Blank!!!", vbExclamation, "Trade"
Exit Sub
End If
trade_name = cmbtrade.Value
On Error Resume Next
Dim trade As Double
trade_name = cmbtrade.Value
TextBox16.Text = Application.WorksheetFunction.VLookup(trade_name,
Sheets("Sheet2").Range("A2:D43"), 4, False)
End Sub
The problem part....
Private Sub cmdupdate_Click()
If Me.cmbtrade.Value = "" Then
MsgBox "Trade Name Can Not be Blank", vbExclamation, "Trade"
Exit Sub
End If
trade_name = cmbtrade.Value
Sheets("sheet2").Select
Dim rowselect As Double
rowselect = Me.cmbtrade.Value (this is where my mismatch error occurs)
rowselect = rowselect + 1
Rows(rowselect).Select
Cells(rowselect, 4) = Me.TextBox16.Value
End Sub
enter image description here
Try this. You don't actually need to convert the combobox to a Long, but it's good practice I think.
Private Sub cmdupdate_Click()
If Me.cmbtrade.Value = "" Then
MsgBox "Trade Name Can Not be Blank", vbExclamation, "Trade"
Exit Sub
End If
Dim rowselect As Long
rowselect = CLng(Me.cmbtrade.Value) + 1
Sheets("sheet2").Cells(rowselect, 4) = Me.TextBox16.Value
End Sub

Vlookup with userform

I'm trying to configure Userform research with 3 textboxes, but I can't make it work and don't know why.
This is my code:
Private Sub TextBox1_AfterUpdate()
On Error GoTo 1
If WorksheetFunction.CountIf(Sheets("Feuil1").Range("A:A"), Me.TextBox1.Value) = 0 Then
MsgBox "introuvable"
End If
With Me
.TextBox2 = Application.WorksheetFunction.VLookup(CLng(Me.TextBox1), Feuil1.Range("A:E"), 2, 0)
End With
1
End Sub
hoping for your help
thanks
Drop the WorksheetFunction and then there will be no run-time error if there's no match:
Private Sub TextBox1_AfterUpdate()
Dim r
r = Application.VLookup(CLng(Me.TextBox1), Feuil1.Range("A:E"), 2, False)
Me.TextBox2 = IIf(IsError(r),"Introuvable", r)
End Sub

Link combobox to textboxt in excel vba - error 1004

I tested the following code in Excel 2016. But I encounter an
error message of 1004
and the code does not work.
Error line:
Me.TextBox1.Text = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, xRg, 2, False))
Private Sub UserForm_Click()
Dim xRg As Range
Private Sub UserForm_Initialize()
Set xRg = Worksheets("Sheet1").Range("A2:B8")
Me.ComboBox1.List = xRg.Columns(1).Value
End Sub
Private Sub ComboBox1_Change()
Me.TextBox1.Text = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value,
xRg, 2, False)
End Sub
It seems that xRg is declared outside of the scope of the ComboBox1_Change event. Thus, the Combobox1_Change() does not access it. Try to declare it within:
Private Sub ComboBox1_Change()
Dim xRg As Range
Set xRg = Worksheets("Sheet1").Range("A2:B8")
Me.TextBox1.Text = Application.WorksheetFunction.VLookup(Me.ComboBox1.Value, _
xRg, 2, False)
End Sub
As mentioned by #Vityata here, you will need to assign the xRg variable within your current code block as it has no reference to it.
As an addition to that though, I would advise ditching the vlookup application function in place of assignment by the combobox index: Me.ComboBox1.ListIndex and use that as the reference for the row in xRg:
Me.TextBox1.Value = xRg.Cells(Me.ComboBox1.ListIndex + 1, 2).Value
The ComboBox.ListIndex property is a 0 based array so I have added 1 on to get the proper row assignment.

Excel VBA: Highlight textbox value after error occurs

I am trying to highlight entered value in TextBox. TextBox value is representing date value in date forma DD-MM-YYYY. I wrote some code to validate if inserted date is ok (in example 31 of April).
Hightlight itself is not a problem, however I want to do this right after an error occurs. So when I insert 31-04-2014, I should get the message "You have inserted wrong date" and the date value should hightlighted. For now it shows me message, highlights value and focus is set to another CommandButton
So far I made something like this:
Private Sub data_faktury_AfterUpdate()
Dim dzien As Byte, miesiac As Byte
Dim rok As Integer
On Error GoTo blad:
dzien = Mid(data_faktury.Value, 1, 2)
miesiac = Mid(data_faktury.Value, 4, 2)
rok = Right(data_faktury.Value, 4)
Call spr_date(dzien, miesiac, rok)
Exit Sub
blad:
If Err.Number = 13 Then
If data_faktury <> "" Then
If Len(data_faktury) < 10 Then: MsgBox ("Źle wpisana data faktury.")
End If
End If
End Sub
And code for 2nd procedure:
Sub zle()
MsgBox ("Wybrałeś zły dzień")
With Faktura.data_faktury
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
This is a bit long for a comment so here goes. The basic principle is to use the exit event and cancel when necessary. To prevent this being fired when you close the form, you need to use a flag variable - example userform code:
Private bSkipEvents As Boolean
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If bSkipEvents Then Exit Sub
With TextBox1
If Not IsValidDate(.Text) Then
Cancel = True
MsgBox "Invalid date"
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
bSkipEvents = True
End Sub

Resources